Follow

I want to propose a programming challenge. This could be fun for beginners and experienced programmers as well. It is language agnostic. It might even be more about community than the programming part itself. The challenge itself should not take more than an hour. But it shouldn't be so simple that you don't have to put in a little effort. I will propose the first one, and those who participate are welcome to propose the next and as we can agree on it we can go off and knock it out.

Once you see other people's results you can modify your own, or even propose something to make someone else's better or faster or fix a bug that you find.

These can become toy programs for you to have around for testing concepts, and helping to try out other languages.

If you are interested or know someone else who might want to play too share this with them.

I guess we can use this thread to get started, and I guess i am supposed to use a hashtag for something like this so how about

I will try to work in Python at least in the beginning but you are welcome to work in whatever you are comfortable with.


First challenge:

"Ninety-nine bottles of beer on the wall"

Generate the whole song from "Ninety-nine" to "No". The output should look like :

Ninety-nine bottles of beer on the wall.
Ninety-nine bottles of beer. Take one down,
pass it around... Ninety-eight bottles of beer.

(You know what the middle looks like)

Two bottles of beer on the wall.
Two bottles of beer. Take one down,
pass it around... one bottle of beer.

One bottle of beer on the wall.
One bottle of beer. Take one down,
pass it around... No bottles of beer.

I won't put too many specifics on how you get there, but the output should be words, not numerals :) Try to write clean, maintainable and visually understandable code.

Let see if I am alone or anyone wants to play along. :)

Here is my first attempt total time 48 minutes.

git.qoto.org/Absinthe/ninety_n

@Absinthe

Common Lisp. 14 minutes.

(defun beer (&optional (start 99)) (loop for num = start then next-num for next-num = (1- num) for numstr = (if (eql 0 num) "no" (format nil "~r" num)) then next-numstr for next-numstr = (if (eql 0 next-num) "no" (format nil "~r" next-num)) for bottles = (format nil "bottle~p" num) for next-bottles = (format nil "bottle~p" next-num) while (> num 0) do (unless (eql num start) (terpri)) (format t "~@(~a~) ~a of beer on the wall,~%~ ~a ~a of beer. Take one down,~%~ pass it around... ~@(~a~) ~a of beer.~%" numstr bottles numstr bottles next-numstr next-bottles)))

@billstclair now I have to install the clisp interpretter. :) I was just hoping I could run it in emacs :) Looks good, but I will have to play with it later!

@Absinthe

Lisp FORMAT's "~r" option makes the problem much easier than in most languages, since it converts 99 to "ninety-nine" in one line of code. The code would have been 2 or 3 times as long, had I had to do that myself.

I used LispWorks to test it, but it should work in clisp.
@Absinthe

That's what "~@(...~)" does.

I would have been quicker, but I don't use "~r", "~p" (pluralize), and "~@(...~)" enough to remember them, so had to look them up.

@billstclair installed gcl and clisp, and apparently still I am not smart enough to run it :)

@billstclair Yay, that did it. Is the bottom NIL stylistic or an abberation?

One bottle of beer on the wall,
one bottle of beer. Take one down,
pass it around... No bottles of beer.
NIL <------- That thing right there ?

@Absinthe

The function prints its output, to *standard-output*. That's what the first argument, T, to FORMAT does. NIL is the value returned by the function. I suppose I could have instead consed up a string, and returned that.

@billstclair No matter, just trying to understand what I was looking at. I don't run much Lisp (can you tell :) ) So I was just trying to understand what I was looking at.

@Absinthe

Here's mine. I ended up adapting your solution for converting numbers to words, because it's more lightweight than the known solutions in my language of choice. Cheers.

github.com/patchcali/toy-progr

#toyprogrammingchallenge

@bii Wonderful! Visual fox pro? I haven't seen that in a while. Glad I could contribute .. :) Unfortunately there is absolutely no way I could run that here :)

@Absinthe

Haha, yeah, yeah :)

Heck, with support for Windows 7 and 8 ending soon, there'll soon enough come a time when *I* won't be able to run it either. [stage whisper] "that's why I'm learning python..."

But VFP served my career well for twenty-some years, and it does still run on a lot of machines.

It's the language & dev environment I'm still most familiar with, though I hadn't touched it in a while, so I saw this as an opportunity to fire up the ol' IDE again.

@bii In that case I would challenge you to try it in python, or at least the next challenge should be python. :)

@Absinthe Hang on a moment while I rummage around in my meme archive for that "Why not both?" gif ;) But, yeah!

Sign in to participate in the conversation
Qoto Mastodon

QOTO: Question Others to Teach Ourselves
An inclusive, Academic Freedom, instance
All cultures welcome.
Hate speech and harassment strictly forbidden.