Jump to content


Photo

Challenge: loops and 99 bottles of beer


  • Please log in to reply
17 replies to this topic

#1 bcrscahh198987

bcrscahh198987

    Mack Daddy 31337

  • Members
  • 211 posts
  • Location:Ur rektumm

Posted 04 November 2009 - 08:11 AM

So, somehow using loops of anykind, you will output lyrics of 99 bottles of beer on the wall, down right when the bottles is 0.

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 bcrscahh198987

bcrscahh198987

    Mack Daddy 31337

  • Members
  • 211 posts
  • Location:Ur rektumm

Posted 04 November 2009 - 08:14 AM

Spoiler


It's fairly easy to do in ruby.

#3 Swerve

Swerve

    Dangerous free thinker

  • Members
  • 809 posts
  • Country:
  • Gender:Male

Posted 05 November 2009 - 01:40 PM

Hehe.

C++:
Spoiler


[/quote]

#4 frogbong

frogbong

    mad 1337

  • Members
  • 134 posts
  • Country:
  • Gender:Male

Posted 05 November 2009 - 04:25 PM

Spoiler


Spoiler


Spoiler

Edited by frogbong, 05 November 2009 - 04:31 PM.


#5 PurpleJesus

PurpleJesus

    Dangerous free thinker

  • Members
  • 1,578 posts
  • Gender:Male
  • Location:800

Posted 05 November 2009 - 07:30 PM

Here's mine... in C.
Spoiler


#6 seabass

seabass

    DDP Fan club member

  • Members
  • 44 posts
  • Country:
  • Gender:Male
  • Location:All up in your Kool-aid

Posted 05 November 2009 - 07:56 PM

Spoiler

Edited by seabass, 05 November 2009 - 08:04 PM.


#7 Seal

Seal

    Not a fan of clubs.

  • Agents of the Revolution
  • 2,440 posts
  • Country:
  • Gender:Male
  • Location:Canada

Posted 06 November 2009 - 12:42 AM

Here's mine, also in C.

edit: Weird - Invisionboard automatically capitalizes the "b" in the "int b" of my function. Odd.

Spoiler

Edited by Seal, 06 November 2009 - 10:02 AM.


#8 PurpleJesus

PurpleJesus

    Dangerous free thinker

  • Members
  • 1,578 posts
  • Gender:Male
  • Location:800

Posted 06 November 2009 - 12:57 AM

Seal beat me to it..
I added words too, it should work up to a million or so bottles.


Spoiler


edit: I screwed up the spoiler tags.

Edited by PurpleJesus, 06 November 2009 - 01:01 AM.


#9 jabzor

jabzor

    hax?

  • Agents of the Revolution
  • 1,146 posts
  • Country:
  • Gender:Male
  • Location:Northern Elbonia, fighting the lefties

Posted 06 November 2009 - 02:08 AM

As a grammatically correct one-liner, though it's not pretty:
Spoiler

>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.

I do like the solutions with the numbers as words, perhaps I'll work on one later.

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 SigFLUP

SigFLUP

    SUPR3M3 31337 Mack Daddy P1MP

  • Members
  • 367 posts
  • Gender:Not Telling

Posted 06 November 2009 - 03:49 AM

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

Edited by SigFLUP, 06 November 2009 - 01:58 PM.


#11 trevelyn

trevelyn

    mad 1337

  • Members
  • 125 posts
  • Location:Pittsburgh, Pa

Posted 06 November 2009 - 12:00 PM

Spoiler

Don't hurt meh!
It's mah first try with memory pointers in Perl. :) I luff Perl and Ruby. <3

#12 bcrscahh198987

bcrscahh198987

    Mack Daddy 31337

  • Members
  • 211 posts
  • Location:Ur rektumm

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 bcrscahh198987

bcrscahh198987

    Mack Daddy 31337

  • Members
  • 211 posts
  • Location:Ur rektumm

Posted 13 November 2009 - 04:58 AM

So nobody used while() loops?

Or it's pointless?

#14 Seal

Seal

    Not a fan of clubs.

  • Agents of the Revolution
  • 2,440 posts
  • Country:
  • Gender:Male
  • Location:Canada

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 Seal

Seal

    Not a fan of clubs.

  • Agents of the Revolution
  • 2,440 posts
  • Country:
  • Gender:Male
  • Location:Canada

Posted 15 November 2009 - 04:21 PM

So nobody used while() loops?

Or it's pointless?

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.

#16 JBu92

JBu92

    HACK THE PLANET!

  • Members
  • 58 posts
  • Country:
  • Gender:Male

Posted 12 January 2010 - 04:28 PM

Python, naturally:
Spoiler

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 Fixion

Fixion

    Will I break 10 posts?

  • Members
  • 4 posts
  • Country:
  • Gender:Male

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 heisenbug

heisenbug

    Gibson Hacker

  • Members
  • 79 posts
  • Country:
  • Gender:Male

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.

Spoiler





BinRev is hosted by the great people at Lunarpages!