Two minute challenge

Notices and updates
DrDave
Posts: 126
Joined: Wed May 21, 2008 2:47 pm

Re: Buggy thinking ;)))))

Post by DrDave »

unixtechie wrote:
Damm!

Code: Select all

(cond ((= 0 (% i 15)) "fizzbuzz") 
Looks like cormullion beat me too it!!!
Actually, I didn't post the code, but you'll see I already covered using COND with three predicates in the second post of this thread:
DrDave wrote: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
...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

DrDave
Posts: 126
Joined: Wed May 21, 2008 2:47 pm

Post by DrDave »

Lutz,

This does not give the correct output on my system. Instead of FizzBuzz in the appropriate places, it gives Buzzizz. In other words, it has the Fizz and Buzz pieces reversed, plus it chops off the F from Fizz.
Lutz wrote:

Code: Select all

(for (i 1 100)
  (let (buff " ")
    (if (zero? (% i 3)) (setf (last buff) "Fizz "))
    (if (zero? (% i 5)) (setf (last buff) "Buzz"))
    (if (= buff " ") (println i) (println buff))))

I discovered that if you replace the next-to-last line with this:

Code: Select all

      (if (zero? (% i 5)) (setf (nth -1 buff) "Buzz"))
then it displays correctly. Some strange bug with LAST?
...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

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

Thanks for catching this. There is a problem of 'last' on strings in combination with 'setf' and on non-UTF-8 versions. When using (buff -1) instead of (last buff), it will work:

Code: Select all

(for (i 1 100)
  (let (buff " ")
    (if (zero? (% i 3)) (setf (buff -1) "Fizz "))
    (if (zero? (% i 5)) (setf (buff -1) "Buzz"))
    (if (= buff " ") (println i) (println buff)))) 
I will do a 10.0.1 maintenance release fixing this beginning January.

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

here is another interesting one:

Code: Select all

(for (i 1 100)
  (let (buff "")
    (if (zero? (% i 5)) (push "Buzz" buff))
    (if (zero? (% i 3)) (push "Fizz" buff))
    (println (if (empty? buff) i buff))))
Initializing 'buff' as a string tells 'push' to do string prepending instead of list pushing when 'buff' would be 'nil' or a list. The 'empty?' predicate works on strings and lists alike.

ale870
Posts: 297
Joined: Mon Nov 26, 2007 8:01 pm
Location: Italy

Post by ale870 »

I'm sorry but I saw this topic only now!
I wish to post my solution. It is neither the fastest nor the shortest, but it uses an original method :-)

See here:

Code: Select all

(dotimes (x 100)
  (setq fac (factor x))

	(setq f1 (find 3 fac))
	(setq f2 (find 5 fac))

	(if (and f1 f2) (println "Fizz Buzz")
		(if f1 (println "Fizz")
			(if f2 (println "Buzz") (println x) )
		)
	)
)
I use factorial function, since every number that can be divided for 3 or 5 have last item list as 3 or five. More: if one number can be divided either for three and five last two numbers will be (3 5)

Use the following line for "debugging" purposes:

Code: Select all

(print x ": " fac " --> ")
Just after setq functions:

Code: Select all

(setq f1 (find 3 fac))
(setq f2 (find 5 fac))

(print x ": " fac " --> ")
Here first 20 numbers...

Code: Select all

1: nil --> 1
2: (2) --> 2
3: (3) --> Fizz
4: (2 2) --> 4
5: (5) --> Buzz
6: (2 3) --> Fizz
7: (7) --> 7
8: (2 2 2) --> 8
9: (3 3) --> Fizz
10: (2 5) --> Buzz
11: (11) --> 11
12: (2 2 3) --> Fizz
13: (13) --> 13
14: (2 7) --> 14
15: (3 5) --> Fizz Buzz
16: (2 2 2 2) --> 16
17: (17) --> 17
18: (2 3 3) --> Fizz
19: (19) --> 19
20: (2 2 5) --> Buzz
--

m i c h a e l
Posts: 394
Joined: Wed Apr 26, 2006 3:37 am
Location: Oregon, USA
Contact:

Post by m i c h a e l »

Here comes my unavoidable FOOP version:

Code: Select all

(define (FizzBuzz:FizzBuzz n) (when (zero? (% n 15)) "FizzBuzz"))
(define (Fizz:Fizz n) (when (zero? (% n 3)) "Fizz"))
(define (Buzz:Buzz n) (when (zero? (% n 5)) "Buzz"))

((define (fizz-buzz) 
	(for (n 1 100) 
		(println (or (FizzBuzz n) (Fizz n) (Buzz n) n))
	)
))
How long did it take me? My initial try took about eight minutes. I guess that means I’m not much of a programmer ;-)

m i c h a e l

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Post by cormullion »

Very clever, Alessandro!

m i c h a e l (aka FOOP-erman): an elegant masterpiece is bound to take a few more minutes. I've never seen that ((define ..)) form before... ingenious!

ale870
Posts: 297
Joined: Mon Nov 26, 2007 8:01 pm
Location: Italy

Post by ale870 »

michel your solution is great! I like it (and I'm studying it)
Thank you for your contribution!
--

Kazimir Majorinc
Posts: 388
Joined: Thu May 08, 2008 1:24 am
Location: Croatia
Contact:

Post by Kazimir Majorinc »

I offer to Newlisp users and sympathizers the program named "Parasite." It works for me this moment.

Code: Select all

(println (slice (read-file (append "http://www.alh.net/newlisp/phpbb/"
                                   "viewtopic.php?t=2582&postdays=0"
                                   "&postorder=asc&start=0"))
                105417 
                (pow 2 9)))
If it doesn't work, this one should:

Code: Select all

(set 'z (read-file (append "http://www.alh.net/newlisp/phpbb/"
                           "viewtopic.php?t=2582&postdays=0"
                           "&postorder=asc&start=0")))

(println (slice z
                (find "1, 2, Fizz, 4" z)
                (pow 2 9)))

Happy new year and good luck to everyone.

m i c h a e l
Posts: 394
Joined: Wed Apr 26, 2006 3:37 am
Location: Oregon, USA
Contact:

Post by m i c h a e l »

Thank you, cormullion and Alessandro. Your kind words always inspire.

Kazimir, thanks for another great, unconventional solution. That’s one of the things I like best about newLISP and this community: many different ways of looking at programming—and newLISP gracefully accommodating them all.
cormullion wrote: I've never seen that ((define ..)) form before... ingenious!
I’m sure code like this would be discouraged in more formal settings, but I find its pure elegance hard to resist ;-)
Alessandro wrote: I like it (and I'm studying it)
This could also be de-FOOPed by replacing the constructors with functions:

Code: Select all

(define (fizzbuzz n) (when (zero? (% n 15)) "FizzBuzz"))
(define (fizz n) (when (zero? (% n 3)) "Fizz"))
(define (buzz n) (when (zero? (% n 5)) "Buzz"))

((define (fizz-buzz) 
	(for (n 1 100) 
		(println (or (fizzbuzz n) (fizz n) (buzz n) n))
	)
))
m i c h a e l

Locked