Challenge: loops and 99 bottles of beer
#1
Posted 04 November 2009 - 08:11 AM
You do not want the beer bottle count to go to -1, which would be silly.
Here is the lyrics at this link
http://en.wikipedia....Bottles_of_Beer
or
Ninety-nine bottles of beer on the wall, Ninety-nine bottles of beer.
Take one down, pass it around, Ninety-eight bottles of beer on the wall.
Ninety-eight bottles of beer on the wall, ninety-nine bottles of beer.
Take one down, pass it around, Ninety-eight bottles of beer on the wall.
.........
and so on.
But replace it with numbers like, 99 bottles of beer.
#2
Posted 04 November 2009 - 08:14 AM
It's fairly easy to do in ruby.
#3
Posted 05 November 2009 - 01:40 PM
C++:
[/quote]
#4
Posted 05 November 2009 - 04:25 PM
Edited by frogbong, 05 November 2009 - 04:31 PM.
#5
Posted 05 November 2009 - 07:30 PM
#6
Posted 05 November 2009 - 07:56 PM
Edited by seabass, 05 November 2009 - 08:04 PM.
#7
Posted 06 November 2009 - 12:42 AM
edit: Weird - Invisionboard automatically capitalizes the "b" in the "int b" of my function. Odd.
Edited by Seal, 06 November 2009 - 10:02 AM.
#8
Posted 06 November 2009 - 12:57 AM
I added words too, it should work up to a million or so bottles.
edit: I screwed up the spoiler tags.
Edited by PurpleJesus, 06 November 2009 - 01:01 AM.
#9
Posted 06 November 2009 - 02:08 AM
I do like the solutions with the numbers as words, perhaps I'll work on one later.>beer.pl 3
3 bottles of beer on the wall, 3 bottles of beer.
Take one down, pass it around, 2 bottles of beer on the wall.
2 bottles of beer on the wall, 2 bottles of beer.
Take one down, pass it around, 1 bottle of beer on the wall.
1 bottle of beer on the wall, 1 bottle of beer.
Take one down, pass it around, 0 bottles of beer on the wall.
edit: hrmm.. mine looks similar to this one, though this is the first time I have seen it.
Guess perl hackers use the same cheap-tricks everywhere
Edited by jabzor, 06 November 2009 - 02:41 AM.
#10
Posted 06 November 2009 - 03:49 AM
My contribution sings. A least It should sing for you, it does for me. I've tested it on openbsd/32bit but not linux so I'm unsure of it works for linux.
use like this:
make
./beer
and if you have openbsd run
make -f Makefile.openbsd
./beer
It depends on libmad and assumes it's in /usr/local or ~/local or /usr
Should work, let me know if it doesn't or if it plays too fast. If it does work sit back and relax because in about five minutes you'll want to punch yourself in the face.
If you can't play it through your speaker or it doesn't sound right you can run ./beer -d and that'll dump the song to beer.wav
Here are some binaries (again only my openbsd system do I have access to sound so I've only tested it on that):
beer_x86_openbsd beer_ppc32_linux beer_x86_64_linux beer_ultrasparc_linux
Edited by SigFLUP, 06 November 2009 - 01:58 PM.
#11
Posted 06 November 2009 - 12:00 PM
Don't hurt meh!
It's mah first try with memory pointers in Perl.
#12
Posted 13 November 2009 - 04:54 AM
Spoiler
Don't hurt meh!
It's mah first try with memory pointers in Perl.I luff Perl and Ruby. <3
Um wow, perl is wtf.
Jabzor's code is hideous
Edited by bcrscahh198987, 13 November 2009 - 11:10 PM.
#13
Posted 13 November 2009 - 04:58 AM
Or it's pointless?
#14
Posted 15 November 2009 - 04:12 PM
Here's mine: beer.tgz
My contribution sings. A least It should sing for you, it does for me. I've tested it on openbsd/32bit but not linux so I'm unsure of it works for linux.
use like this:
make
./beer
and if you have openbsd run
make -f Makefile.openbsd
./beer
It depends on libmad and assumes it's in /usr/local or ~/local or /usr
Should work, let me know if it doesn't or if it plays too fast. If it does work sit back and relax because in about five minutes you'll want to punch yourself in the face.
If you can't play it through your speaker or it doesn't sound right you can run ./beer -d and that'll dump the song to beer.wav
Here are some binaries (again only my openbsd system do I have access to sound so I've only tested it on that):
beer_x86_openbsd beer_ppc32_linux beer_x86_64_linux beer_ultrasparc_linux
No one commented on this, which is unfortunate. That's fucking cool - you put together a program that decodes MP3s on-the-fly and plays audio that you embedded within the program. Major props.
#15
Posted 15 November 2009 - 04:21 PM
You could use while loops, sure. Seabass had one for his example. In most cases, the use of alternatives (ie. for loops) likely diminished the amount of code required to implement the same thing.So nobody used while() loops?
Or it's pointless?
#16
Posted 12 January 2010 - 04:28 PM
Doing the numbers in text would take a bit more work, in a production scenario, I'd give them this then work on a module to do numbers as text :-).
Edited by JBu92, 12 January 2010 - 05:50 PM.
#17
Posted 08 November 2010 - 10:49 PM
/* In C. */
#include <stdio.h>
int main(void) {
for (int i = 99; i > 0; i--) {
printf("%d bottles of beer on the wall, %d bottles of beer.\n", i);
printf("Take one down, pass it around...\n");
}
return 0;
}
# In Python.
for i in xrange(99, 0, -1):
print("{0} bottles of beer on the wall, {0} bottles of beer.".format(i))
print("Take one down, pass it around...")
#18
Posted 19 December 2010 - 06:02 PM
Um wow, perl is wtf.
Jabzor's code is hideous
Not really. Perl, like any language, can be as cryptic as you make it. For example, below is a script that is very readable.
BinRev is hosted by the great people at Lunarpages!













