Two minute challenge

Started by cormullion, December 22, 2008, 05:50:59 AM

Previous topic - Next topic

cormullion

OK, here's a trivial challenge to solve in 2 minutes or less.


QuoteWrite a program that prints the numbers from 1 to 100. But for multiples of three print "Fizz" instead of the number and for the multiples of five print "Buzz". For numbers which are multiples of both three and five print "FizzBuzz".


When you've written your newLISP script, read http://imranontech.com/2007/01/24/using-fizzbuzz-to-find-developers-who-grok-coding/">this blog entry to find out more.

DrDave

#1
Thanks for posting that, Mr. C. I resorted to using COND, plus for ease of reading, defined three other functions that are predicates for  the testing for being a factor of 15, 5, or 3. I accessed the numbers by using  DOTIMES, forgeting that it starts at 0. So I guess I get points off for covering 0-99 instead of 1-100, LOL



After I read through the blog, I noticed that most entries did the test for being a factor of both 3 and 5 by ANDing. I was a bit surprised that checking to be a factor of 15 wasn't the norm for doing that.



It was certainly easy enough to define a function in under 2 minutes by plugging in those predicates, but then it took another minute or so to actually define the three predicates.



I think that is doing well, considering I am just a "casual" programmer having taken only two formal computer science courses at the university. (Well, actually, I have CREDIT for two but only took one. They wouldn't let me in the first course, so I petitioned to get credit for it because they thought my knowledge was too advanced to actually sit in the first class.)
...it is better to first strive for clarity and correctness and to make programs efficient only if really needed.

\"Getting Started with Erlang\"  version 5.6.2

xytroxon

#2

(for (i 1 100)
(print i " ")
(if (= (% i 3) 0)(print "Fizz"))
(if (= (% i 5) 0)(print "Buzz"))
(println)
)
(exit)


-- xytroxon
\"Many computers can print only capital letters, so we shall not use lowercase letters.\"

-- Let\'s Talk Lisp (c) 1976

xytroxon

#3

(for (i 1 100)
(print i " ")
(when (zero? (% i 3))(print "Fizz"))
(when (zero? (% i 5))(print "Buzz"))
(println)
)
(exit)


-- xytroxon



Update: Self documenting "natural language" reading style...

(for (number 1 100)
(print number " ")
(when (zero? (% number 3))(print "Fizz"))
(when (zero? (% number 5))(print "Buzz"))
(println)
)
(exit)


P.S.



Q.E.D.  ;)
\"Many computers can print only capital letters, so we shall not use lowercase letters.\"

-- Let\'s Talk Lisp (c) 1976

DrDave

#4
xytroxon

I like your solution because it takes care of the "FizzBuzz" case without needing a separate test for it! And it is readable, understandable code.
...it is better to first strive for clarity and correctness and to make programs efficient only if really needed.

\"Getting Started with Erlang\"  version 5.6.2

cormullion

#5
I was surprised that the author said:


QuoteWant to know something scary ? - the majority of comp sci graduates can't. I've also seen self-proclaimed senior programmers take more than 10-15 minutes to write a solution.


It does seem a very trivial problem... At least, in newLISP.



The response numbered 404 must have taken a few minutes!

Lutz

#6
But its not correct yet!


Quoteprint "Fizz" instead of the number


emphasis on instead. I believe, it should look like this:


...
Fizz
88
89
FizzBuzz
91
...

DrDave

#7
I read quite a few of the replies on that blog as well as replies on another blog. A couple of comments essentially said that the universities typically are not turning out people that know how to go about solving problems. From my experinece, I have to say that I think in general, most universities at both undergraduate and graduate level do not train students to solve problems, but rather expect nothing more than the students learn how to pass their classroom tests with high scores. When you graduate, it's all about your academic scores and nothing about what you have learned (I suspect this is because you have learned next to nothing LOL). I have seen many students with top academic scores that cannot function at any meaningful level on any problems outside the classroom. And it is no wonder, because from personal experience, thinking outside the box or outside the professor's field of dogma are not only frowned upon, but typically result in academic penalites. I could usually get away with out of the box thinking if I also included the expected dogmatic response, just so the professor could be sure I understood "his" way of thinking.



I've found the same problem in industry. So many incompetent people are in high paying positions without a clue about what their staff are actually doing. I haven't figured out how they get those positions with thier qualifications, but if I do, I'll be sure to emulate it.
...it is better to first strive for clarity and correctness and to make programs efficient only if really needed.

\"Getting Started with Erlang\"  version 5.6.2

xytroxon

#8
Picky, picky... ;)





(define (fizz-buzz max-count, fizz buzz)
(println "Fizz-Buzz:")
(for (number 1 max-count)
(setq fizz (% number 3) buzz (% number 5))
(unless (or (zero? fizz) (zero? buzz))(print number))
(when (zero? fizz)(print "Fizz"))
(when (and (zero? fizz) (zero? buzz))(print "-"))
(when (zero? buzz)(print "Buzz"))
(if (< number max-count)(print ", ")(print "!"))
)
(println)
)
(fizz-buzz 100)
(exit)

Fizz-Buzz:

1, 2, Fizz, 4, Buzz, Fizz, 7, 8, Fizz, Buzz, 11, Fizz, 13, 14, Fizz-Buzz, 16, 17, Fizz, 19, Buzz, Fizz, 22, 23, Fizz, Buzz, 26, Fizz, 28, 29, Fizz-Buzz, 31, 32, Fizz, 34, Buzz, Fizz, 37, 38, Fizz, Buzz, 41, Fizz, 43, 44, Fizz-Buzz, 46, 47, Fizz, 49, Buzz, Fizz, 52, 53, Fizz, Buzz, 56, Fizz, 58, 59, Fizz-Buzz, 61, 62, Fizz, 64, Buzz, Fizz, 67, 68, Fizz, Buzz, 71, Fizz, 73, 74, Fizz-Buzz, 76, 77, Fizz, 79, Buzz, Fizz, 82, 83, Fizz, Buzz, 86, Fizz, 88, 89, Fizz-Buzz, 91, 92, Fizz, 94, Buzz, Fizz, 97, 98, Fizz, Buzz!



According to Wikipedia, the name of the game is "Bizz Buzz"...

http://en.wikipedia.org/wiki/Bizz_buzz">//http://en.wikipedia.org/wiki/Bizz_buzz



(define (bizz-buzz max-count, bizz buzz)
(println "Bizz-Buzz:")
(for (number 1 max-count)
(setq bizz (% number 3) buzz (% number 5))
(unless (or (zero? bizz) (zero? buzz))(print number))
(when (zero? bizz)(print "Bizz"))
(when (and (zero? bizz) (zero? buzz))(print "-"))
(when (zero? buzz)(print "Buzz"))
(if (< number max-count)(print ", ")(print "!"))
)
(println)
)
(bizz-buzz 100)
(exit)


Bizz-Buzz:

1, 2, Bizz, 4, Buzz, Bizz, 7, 8, Bizz, Buzz, 11, Bizz, 13, 14, Bizz-Buzz, 16, 17, Bizz, 19, Buzz, Bizz, 22, 23, Bizz, Buzz, 26, Bizz, 28, 29, Bizz-Buzz, 31, 32, Bizz, 34, Buzz, Bizz, 37, 38, Bizz, Buzz, 41, Bizz, 43, 44, Bizz-Buzz, 46, 47, Bizz, 49, Buzz, Bizz, 52, 53, Bizz, Buzz, 56, Bizz, 58, 59, Bizz-Buzz, 61, 62, Bizz, 64, Buzz, Bizz, 67, 68, Bizz, Buzz, 71, Bizz, 73, 74, Bizz-Buzz, 76, 77, Bizz, 79, Buzz, Bizz, 82, 83, Bizz, Buzz, 86, Bizz, 88, 89, Bizz-Buzz, 91, 92, Bizz, 94, Buzz, Bizz, 97, 98, Bizz, Buzz!



Which sounds much harder to play, with "Bizz Buzz" being a better tongue twister than "Fizz Buzz"...



-- xytroxon
\"Many computers can print only capital letters, so we shall not use lowercase letters.\"

-- Let\'s Talk Lisp (c) 1976

DrDave

#9
Because I liked xytroxon's elimination of explicit testing for factor of 15, I looked for a solution that eliminates this test but also gives only the requested output, but without resorting to OR and still being reasonably simple (for us simple-minded folk).



(define (reset-vars)
         (setf FIZZ ""  BUZZ "")
)

(define (FIZZER? n)
         (zero? (% n 3))
)

(define (BUZZER? n)
         (zero? (% n 5))
)

(for (number 1 100)
         (setf NUM number)
         (if (FIZZER? number) (setf FIZZ "Fizz" NUM "")
         )
         (if (BUZZER? number) (setf BUZZ "Buzz" NUM "")
         )
         (println (string NUM FIZZ BUZZ))
         (reset-vars)
)
...it is better to first strive for clarity and correctness and to make programs efficient only if really needed.

\"Getting Started with Erlang\"  version 5.6.2

unixtechie

#10
Cormullion - thanks, laughed my head off



However I did not see immediately how to do it in a roundabout and convoluted way while using some intricate functional programming techniques (that's what this challenge was about, right?)



Because otherwise, in procedural and straightforward stle it looks almost indecently naked:


(for (x 1 100 1)

 (set 'three (= 0 (% x 3)))
 (set 'five (= 0 (% x 5)))

 (or
  (and three (not five)(println  "Fizz"))
  (and  five (not three) (println "Buzz"))
  (and three five (println "FizzBuzz"))
  (println x)
 )
)
(exit)


So I'm waiting eagerly (not lazily) for improvements of this solution.

Actually, I could write an interesting one if a non-lazy OR (i.e. the one that evaluates ALL its parts in any case) was constructed

unixtechie

#11
1. you know what? I have just made it marginally more convoluted, like this:
(for (x 1 100 1)

  (or
   (and (set 'three (= 0 (% x 3))) (println "Fizz"))
   (and (= 0 (% x 5)) (or
      (and three (println "FizzBuzz"))
      (println "Buzz")  ))
   (println x)
  )

)
(exit)


Piece'o'cake ;)))))))))





2. .. but the true corporate solution would be this:


(println 1)
(println 2)
(println "Fizz")
(println 4)
(println "Buzz")
(println "Fizz")
(println 7)


Note to the team leader: "please forward this code to the junior member of the team to complete typing"



Note to the manager: "I have submited perfectly working code which should meet all deadlines. The team will be working on the next version as time permits to improve satisfaction in case of customer requests for number of items other than one hundred"





3. But by the time work on the second version started, it was mandated that all code followed the Object Oriented Paradigm.

So, in accordance with the principle that objects reflect real life entities, the program now generates 100 "Child_player" objects, which are passed request_messages from the Game_Master object (the supervisor pattern?). Each of the objects' reply to the Fizz-Buzz question is then collected and formatted centrally for dispatch to the expernal requesting entity.



..mmm working on it.





4. Just got an e-mail from the management. Version 3 is supposed to be a network distributed  solution. Each of the 100 children objects will be converted to a freely-flying persistent network agent, which maintain their internal state and are able to change their chairs so to say, while still engaged in the game. Results are to be sent to a company overseas branch and support integrity checking and strong encryption



Help!

unixtechie

#12
.. but there was in this company in the farthest corner of the room where all programmers sat a tiny office, and it was occupied by an old grumpy unix geek, who was actually the company system administrator.

As his servers buzzed on for the third year without a single reboot and his monitoring scripts notified him of the problems about 15 minutes before they happened, he had all the time on his hands to play with his favourite geeky toys. And one of such toys was functional programming.



So in half the time it took the company testing department to discover that typo in the customer report which turned an innocuous phrase into a grossly indecent one (slashing the company revenues and resulting in a couple of lawsuits by enraged budgerigar owners, all luckily settled out of court), he produced his own truly functional solution to the Fizz-Buzz problem.



It ran like this:
Quote
The input stream (potentially infinite, but reduced to the first 100 numbers for the sake of testing) is processed lazily, i.e. one item at a time.

The program runs 2 lazy generators in two coroutines, one spits numbers which are consequtive multiples of 3, the other one bakes  multiples of 5 (or any other given numbers). So, once the generator is kicked, a new member of the virtual "list" is generated, the next multiple to check against.



The main program therefore checks each input value from that (infinite) input stream for _equivalence_ with the current 3-list and  5-list members by ping-ponging to the coroutine.

Once the equivalence is found, the 3-list or 5-list generator(s) lazily produce a next number for comparison.



But, to speed things up and save on the coroutine invocation, the numbers from those lazy generators are of course memoized, thus cutting up the needed time and improving performance


Everyone marvelled at the sight of the sysadmin prog crunching Fizz-Buzzer player numbers in the gazillions and the generality of the solution allowing it not to slow down even if 312345912358 and 512309482093481209358340 were the needed as multiples, but no one understood a thing, and management turned it down as totally unmantainable.



The geek then sligtly changed some output parameters of his script and posted it on the Internet, where it became popular as a computer game called "Squash the Bill".



Has anyone  seen it recently? Could you post the source code here?

cormullion

#13
:) Sounds like the voice of experience there...!





I didn't think the two-minute challenge was a major one - I was more intrigued with the idea of people considering themselves to be programmers yet finding it difficult...



I also like watching people produce a seemingly unlimited amount of solutions in every language under the sun.



With newLISP you can't really play code golf ("59 bytes - beat that" kind of thing). Better perhaps is to search for elegance, simplicity, and also for speed (if your first solution is particularly lumbering...).

DrDave

#14
Quote from: "cormullion"I also like watching people produce a seemingly unlimited amount of solutions in every language under the sun.

And I am particularly interested in seeing just how horribly undreadable code is in some of the languages. (Unreadable because of the syntax and symbols of the language, not becasue of purposeful obfuscation or because of some dolt of a coder.)




Quote from: "cormullion"With newLISP you can't really play code golf ("59 bytes - beat that" kind of thing). Better perhaps is to search for elegance, simplicity, and also for speed (if your first solution is particularly lumbering...).

But with newLISP, we also have the "FUN" factor, so sometimes we get to see some fun solutions that are of no practical value.



In addition, being a functional language that is heavily into list processing, we get to see solutions that are not even considered by coders of imperative languages.
...it is better to first strive for clarity and correctness and to make programs efficient only if really needed.

\"Getting Started with Erlang\"  version 5.6.2