Work

For stuff that is general.
GORDON
Site Admin
Posts: 56735
Joined: Sun Jun 06, 2004 10:43 pm
Location: DTManistan
Contact:

Post by GORDON »

My personal assessment of my own code, once I had the eyes to make that kind of analysis, is that my code tends to be more utilitarian than artistic. I've seen things I have done redone now and again to be prettier, but not with any better functionality.
"Be bold, and mighty forces will come to your aid."
TPRJones
Posts: 13418
Joined: Fri May 21, 2004 2:05 pm
Location: Houston
Contact:

Post by TPRJones »

Not sure of the exact syntax of the language you are using, but I think this would be close to maximum efficiency (please excuse my pseudocode):

public function FizzBuzzLoop(int i)
{
str thistime = null;
if (i % 3 == 0) { thistime = "Fizz"; }
if (i % 5 == 0) { thistime = thistime + "Buzz"; }
if (thistime == null) { Console.WriteLine(i.ToString()); }
else { Console.WriteLine(thistime); }
if (i < 101) { FizzBuzzLoop(i+1); }
}


One less evaluation and it swaps in recursion in place of a while loop.
"ATTENTION: Customers browsing porn must hold magazines with both hands at all times!"
Malcolm
Posts: 32040
Joined: Fri May 21, 2004 1:04 pm
Location: Minneapolis

Post by Malcolm »

GORDON wrote:My personal assessment of my own code, once I had the eyes to make that kind of analysis, is that my code tends to be more utilitarian than artistic. I've seen things I have done redone now and again to be prettier, but not with any better functionality.
After debugging other people's code enough, I place readability on a very high level.
Diogenes of Sinope: "It is not that I am mad, it is only that my head is different from yours."
Arnold Judas Rimmer, BSC, SSC: "Better dead than smeg."
TheCatt
Site Admin
Posts: 58739
Joined: Thu May 20, 2004 11:15 pm
Location: Cary, NC

Post by TheCatt »

TPRJones wrote:Not sure of the exact syntax of the language you are using, but I think this would be close to maximum efficiency (please excuse my pseudocode):

public function FizzBuzzLoop(int i)
{
str thistime = null;
if (i % 3 == 0) { thistime = "Fizz"; }
if (i % 5 == 0) { thistime = thistime + "Buzz"; }
if (thistime == null) { Console.WriteLine(i.ToString()); }
else { Console.WriteLine(thistime); }
if (i < 101) { FizzBuzzLoop(i+1); }
}


One less evaluation and it swaps in recursion in place of a while loop.
The recursion is unnecessary. And inefficient, as you will be increasing your memory allocation for the variables you create with each call, that won't go out of scope since your calls are now nested.
It's not me, it's someone else.
TPRJones
Posts: 13418
Joined: Fri May 21, 2004 2:05 pm
Location: Houston
Contact:

Post by TPRJones »

Oh. Well, nevermind then.

Need a better language. Recursion is too awesome to sacrifice to bad memory management.
"ATTENTION: Customers browsing porn must hold magazines with both hands at all times!"
TheCatt
Site Admin
Posts: 58739
Joined: Thu May 20, 2004 11:15 pm
Location: Cary, NC

Post by TheCatt »

TPRJones wrote:Oh. Well, nevermind then.

Need a better language. Recursion is too awesome to sacrifice to bad memory management.
I'm not sure what language that would be possible in.

That Fibonacci solution for recursion is nice.
It's not me, it's someone else.
thibodeaux
Posts: 8121
Joined: Thu May 20, 2004 7:32 pm

Post by thibodeaux »

TPRJones wrote:Oh. Well, nevermind then.

Need a better language. Recursion is too awesome to sacrifice to bad memory management.
Some LISPs have "tail recursion" which can be implemented as a GOTO. No memory problems.
TPRJones
Posts: 13418
Joined: Fri May 21, 2004 2:05 pm
Location: Houston
Contact:

Post by TPRJones »

I could do it in Forth. Or declare the variables as globals instead (Pascal, Fortran, many others), although that's bad for other reasons.
"ATTENTION: Customers browsing porn must hold magazines with both hands at all times!"
Malcolm
Posts: 32040
Joined: Fri May 21, 2004 1:04 pm
Location: Minneapolis

Post by Malcolm »

TheCatt wrote:
TPRJones wrote:Oh. Well, nevermind then.

Need a better language. Recursion is too awesome to sacrifice to bad memory management.

I'm not sure what language that would be possible in.

That Fibonacci solution for recursion is nice.

Inefficient as hell, too.

Some LISPs have "tail recursion" which can be implemented as a GOTO. No memory problems.

TPR's solution looks tail-recursive (iterative). Run-time is still big-omega(N). In fact, outside of butchering the logic inside the function, it'd be hard to make this recursive in a dangerous way. It's the opposite of the Fibonacci, which is (relatively) difficult to do non-recursively. Someone today, with some hints, finally got the good solution. First one this round.




Edited By Malcolm on 1407195179
Diogenes of Sinope: "It is not that I am mad, it is only that my head is different from yours."
Arnold Judas Rimmer, BSC, SSC: "Better dead than smeg."
GORDON
Site Admin
Posts: 56735
Joined: Sun Jun 06, 2004 10:43 pm
Location: DTManistan
Contact:

Post by GORDON »

So is there some rule in fizzbuzz that says I can't just write a single line of code: print " 1 2 fizz 4 buzz..." Etc?

I mean, as long as we are thinking outside the box. Did anyone say it had to be overly complicated?
"Be bold, and mighty forces will come to your aid."
Malcolm
Posts: 32040
Joined: Fri May 21, 2004 1:04 pm
Location: Minneapolis

Post by Malcolm »

GORDON wrote:So is there some rule in fizzbuzz that says I can't just write a single line of code: print " 1 2 fizz 4 buzz..." Etc?

I mean, as long as we are thinking outside the box. Did anyone say it had to be overly complicated?

Works fine until you print from 1 to N.

EDIT: Contractor managed to renege on his acceptance. Fucking recruiting firms and their visa bullshit.




Edited By Malcolm on 1407517648
Diogenes of Sinope: "It is not that I am mad, it is only that my head is different from yours."
Arnold Judas Rimmer, BSC, SSC: "Better dead than smeg."
Post Reply