# Maximum PC

 It is currently Tue Sep 02, 2014 7:50 pm

 All times are UTC - 8 hours

 Page 2 of 3 [ 52 posts ] Go to page Previous  1, 2, 3  Next
 Print view Previous topic | Next topic
Author Message
 Post subject: Posted: Wed Aug 04, 2010 10:38 am
 8086

Joined: Tue Aug 04, 2009 3:42 pm
Posts: 20
I just wrote a C# two-line program that solves this:
Code:
var Say = (string s) => {Console.WriteLine(s);return true;};
for(int i = 1; i <= 100; i++) if(!(i % 3 == 0 && Say("Fizz") && i % 5 == 0 && Say("Buzz"))) Console.WriteLine(i.ToString());

Beat that

Top

 Post subject: Posted: Wed Aug 04, 2010 1:19 pm
 Java Junkie

Joined: Mon Jun 14, 2004 10:23 am
Posts: 24222
Location: Granite Heaven
All of the above solutions beat that.

What value do the Say("xxx") methods return? Do they are return a value? Is it Boolean?

Gadget's solution is O(1). Yours is O(n). You aren't playing in the same ballpark.

Top

 Post subject: Posted: Wed Aug 04, 2010 1:52 pm
 8086

Joined: Tue Aug 04, 2009 3:42 pm
Posts: 20
Quote:
All of the above solutions beat that.

What value do the Say("xxx") methods return? Do they are return a value? Is it Boolean?

Gadget's solution is O(1). Yours is O(n). You aren't playing in the same ballpark.

I meant for line count, not efficiency... and it's pretty obvious what the Say("xxx") methods return, seeing as its definition is on line 1...

Top

 Post subject: Posted: Wed Aug 04, 2010 2:01 pm
 Java Junkie

Joined: Mon Jun 14, 2004 10:23 am
Posts: 24222
Location: Granite Heaven
I admit, I missed that you'd defined it on the first line.

I was too busy laughing at the thought that line count is important.

Did you miss the discussion regarding line count earlier in the thread?

Top

 Post subject: Posted: Wed Aug 04, 2010 2:06 pm
 8086

Joined: Tue Aug 04, 2009 3:42 pm
Posts: 20
There was no discussion, there were two posts. And this is just the optimal line count one. Why do you care so much?

Top

 Post subject: Posted: Wed Aug 04, 2010 2:24 pm
 Java Junkie

Joined: Mon Jun 14, 2004 10:23 am
Posts: 24222
Location: Granite Heaven
I care because you're perpetuating the completely false myth that line count matters. It is annoying.

Top

 Post subject: Posted: Wed Aug 04, 2010 2:40 pm
 8086

Joined: Tue Aug 04, 2009 3:42 pm
Posts: 20
I never said it matters. It doesn't. It's just interesting to find out how short you can make a bit of code. In terms of efficiency, it's usually horrible (though the short-circuiting there is nice).

Top

 Post subject: Posted: Thu Aug 05, 2010 3:49 am
 SON OF A GUN

Joined: Mon Nov 01, 2004 5:41 am
Posts: 11605
Jipstyle wrote:
All of the above solutions beat that.

What value do the Say("xxx") methods return? Do they are return a value? Is it Boolean?

Gadget's solution is O(1). Yours is O(n). You aren't playing in the same ballpark.
They also solved different problems.

Top

 Post subject: Posted: Sat Aug 07, 2010 12:09 am
 Bitchin' Fast 3D Z8000*

Joined: Tue Jun 29, 2004 11:32 pm
Posts: 2555
Location: Somewhere between compilation and linking
minitech wrote:
I never said it matters. It doesn't. It's just interesting to find out how short you can make a bit of code. In terms of efficiency, it's usually horrible (though the short-circuiting there is nice).

Before getting into your solution, I'm Gadget -- nice to meet you!

Alright, I see a few problems with your solution...

1) In order for you to print a numeral, the entire conditional must be true which only occurs when a number is both a multiple of 3 and 5 (ie a multiple of 15). If the current number is a multiple of 5, but not 3, you won't ever print "Buzz" due to short-circuiting of the and statement (note: I believe that C# does short-circuiting... I could be wrong).

2) If a number is a multiple of 3, you will print "Fizz", but you will follow it with a 3 or 6 or ...

3) You're going to have newline errors. For example, you can either print a newline after "fizz" which works for multiples of 3 (minus issue #1 above), but it will print "fizz\nbuzz\n" for a multiple of 15. Some languages will allow you to use a conditional inside of a print statement, but I doubt that C# supports this functional style of programming (see example below).

Just some thoughts...

Code:
(loop for i from 1 to 20 do
(format t "~a~%"
(cond ((and (= (mod i 3) 0) (= (mod i 5) 0)) "FizzBuzz")
((= (mod i 3) 0) "Fizz")
((= (mod i 5) 0) "Buzz")
(t i))))

1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
16
17
Fizz
19
Buzz

Top

 Post subject: Posted: Sat Aug 07, 2010 12:26 am
 Bitchin' Fast 3D Z8000*

Joined: Tue Jun 29, 2004 11:32 pm
Posts: 2555
Location: Somewhere between compilation and linking
minitech wrote:
I never said it matters. It doesn't. It's just interesting to find out how short you can make a bit of code. In terms of efficiency, it's usually horrible (though the short-circuiting there is nice).

I just hope that you know if you do this in real-life that someone is probably going to throw a reference book at your head!

Hey guys -- I just noticed an interesting property of multiples of 15. I'm going to start a new FizzBuzz thread to discuss it. Check it out!

Top

 Post subject: Posted: Sat Aug 07, 2010 11:26 am
 8086

Joined: Tue Aug 04, 2009 3:42 pm
Posts: 20
I just hope that you know if you do this in real-life that someone is probably going to throw a reference book at your head!

I'd never do it in real life, so I guess there's little chance of anyone doing that .

You're right - I put an && where I should have changed a bit of logic and put a ||. Here's the new one:
Code:
var Say = (string s) => {Console.WriteLine(s);return false;};
for(int i = 1; i <= 100; i++) if(((i % 3 == 0 && Say("Fizz")) || (i % 5 == 0 && Say("Buzz"))) || (i % 3 == 0 && i % 5 == 0)) Console.WriteLine(i.ToString());

Top

 Post subject: Posted: Sat Aug 07, 2010 11:38 am
 Bitchin' Fast 3D Z8000*

Joined: Tue Jun 29, 2004 11:32 pm
Posts: 2555
Location: Somewhere between compilation and linking
minitech wrote:
Here's the new one:
Code:
var Say = (string s) => {Console.WriteLine(s);return false;};
for(int i = 1; i <= 100; i++) if(((i % 3 == 0 && Say("Fizz")) || (i % 5 == 0 && Say("Buzz"))) || (i % 3 == 0 && i % 5 == 0)) Console.WriteLine(i.ToString());

Around number 15, you'll print...

14
Fizz
Buzz
16

14
FizzBuzz
15

Just curious, what is this line of code doing? Is this some kind of macro system in C#?
Code:
var Say = (string s) => {Console.WriteLine(s);return false;};

It looks like you're creating a macro named Say, that accepts an input string s, which is then expanded into the console writeline call... is that about right?

Top

 Post subject: Posted: Sat Aug 07, 2010 11:57 am
 8086

Joined: Tue Aug 04, 2009 3:42 pm
Posts: 20
Quote:
It looks like you're creating a macro named Say, that accepts an input string s, which is then expanded into the console writeline call... is that about right?
Yup, lambdas are new to C# and they're really useful. You're right, I should change it to .Write() instead of .WriteLine()... and that makes the logic easier, too Here's a new one:
Code:
Func<string, bool> Say = (string s) => {Console.Write(s);return true;};
for(int i = 1; i <= 100; i++) if(!((i % 3 == 0 && Say("Fizz")) | (i % 5 == 0 && Say("Buzz"))) || Say(i.ToString())) Console.WriteLine();

Last edited by minitech on Sat Aug 07, 2010 12:48 pm, edited 1 time in total.

Top

 Post subject: Posted: Sat Aug 07, 2010 12:21 pm
 Bitchin' Fast 3D Z8000*

Joined: Tue Jun 29, 2004 11:32 pm
Posts: 2555
Location: Somewhere between compilation and linking
minitech wrote:
Quote:
It looks like you're creating a macro named Say, that accepts an input string s, which is then expanded into the console writeline call... is that about right?
Yup, lambdas are new to C# and they're really useful.

That's a lambda function? This seems a bit strange because in most languages a lambda function is a "nameless functions", but here, you've clearly given it the name Say().

Can you do something like the code below (ie write the lambda function at any place that will accept a function)?
Code:
CL-USER> (setf foo '(1 2 3 4 5 6))
(1 2 3 4 5 6)
CL-USER> (sort foo #'<)
(1 2 3 4 5 6)
CL-USER> (sort foo #'>)
(6 5 4 3 2 1)
CL-USER> (sort foo (lambda (x y)
(cond ((and (= (mod x 2) 0)
(= (mod y 2) 0))
(< x y))
((= (mod x 2) 0) t)
((= (mod y 2) 0) nil)
(t (< x y)))))
(2 4 6 1 3 5)
CL-USER>

Top

 Post subject: Posted: Sat Aug 07, 2010 12:47 pm
 8086

Joined: Tue Aug 04, 2009 3:42 pm
Posts: 20
No, you have to assign them to something unless they're being used in an expression. So this:
Code:
myList.Where((x) => x == 4);

is valid, but:
Code:
((x) => { return x == 4; })(myValue);

is not.

Top

 Post subject: Posted: Mon Aug 09, 2010 4:52 am
 Bitchin' Fast 3D Z8000

Joined: Mon Jun 14, 2004 4:04 pm
Posts: 985
Location: Earth
minitech wrote:
No, you have to assign them to something unless they're being used in an expression. So this:
Code:
myList.Where((x) => x == 4);

is valid, but:
Code:
((x) => { return x == 4; })(myValue);

is not.

Gadget's correct, lambda's are nameless functions. Hence the code:

Code:
Func<string, bool> Say = ...;

Is not a lambda in true form. Func<TResult> functions more as a delegate (IIRC), so this looks more like an anonymous function call, similar to .NET 2.0.

Top

 Post subject: Posted: Mon Aug 09, 2010 9:06 am
 8086

Joined: Tue Aug 04, 2009 3:42 pm
Posts: 20
It is a lambda, it's just assigned to a variable in that case. I could do this:
Code:
myFunction((arguments) => { code });

But having this:
Code:
((arguments) => { code })(variable);

doesn't work because a constructor is being called in the background, and C# doesn't allow that:
Code:
(new Form()).Show() // Doesn't work

EDIT:
Actually, I guess it works now!

Top

 Post subject: Posted: Mon Aug 09, 2010 3:50 pm
 Thunderbird

Joined: Sat Jul 09, 2005 12:17 pm
Posts: 897
Location: New Orleans, and all that Jazz!
On the topic of the article I would agree.

Im a double major Computer/Electrical Engineering and Computer Science

and we sit in class and learn that material and then are left to our own devices for programming projects.

In ECE they actually have discussed optimization but thats low level(MIPS,SPARC,IA32,BASIC), and programming projects for some of those classes stipulate solving a problem in a certain number of instructions, which does directly correlate with efficiency most of the time.

CSC courses you only get direct assistance with code if you cant get it working, no regards to optimizing.

If thats the state of the industry i feel a good deal better although real optimization in anything high level is on the list of things i wish i knew.

Top

 Post subject: Posted: Tue Aug 10, 2010 9:17 am
 Java Junkie

Joined: Mon Jun 14, 2004 10:23 am
Posts: 24222
Location: Granite Heaven
When I was in school, we learned the math behind CS. This included optimization, formal proofs of completeness and complexity, etc..

These classes were required when I earned my Bachelor's but by the time I'd moved on to grad school, most of them had become 'optional' for non-honours students.

Top

 Post subject: Posted: Tue Aug 10, 2010 10:09 am
 Thunderbird

Joined: Sat Jul 09, 2005 12:17 pm
Posts: 897
Location: New Orleans, and all that Jazz!
We did a bit of algorithmic analysis(Big O, Big Theta) etc and when I took the programming languages course we did talk in decent detail about how language designs play into efficiency(Eager vs Lazy evaluation). I think we might have a course named Optimization but i dont think it is even requisite for the Software Engineering concentration. I have yet to take the Compiler course which I imagine will include some of notions.

Ill have to dig up some supplemental instruction(a book) on the topic.

Top

 Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending
 Page 2 of 3 [ 52 posts ] Go to page Previous  1, 2, 3  Next

 All times are UTC - 8 hours

#### Who is online

Users browsing this forum: No registered users and 1 guest

 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot post attachments in this forum

Search for:
 Jump to:  Select a forum ------------------ Maximum FAQs    Forum Rules, Posting Guidelines & Announcements    The Good, The Bad & The Banned    FAQs Help/Do It Yourself    PC Building Lab    The Help Desk    PC Modding    Education & Certification Hardware    Nuts & Bolts    Portable Talk    Appraisals, Deals & Bargains (oh my!) OS/Software/Programming    Windows Parlor    Alt.OS.Abode    Games Arena    Programmers' Paradise Networking/Internet    Internet Truckstop    Network Nook In/Out    Magazine and Book Feedback    Forum & Website Feedback    Dog Pound Team Maximum PC Folding at Home    Team Maximum PC - Folding at Home - FIND CURES TO DISEASES    Team MPC - Folding Gauntlets