bcrscahh198987

Challenge: loops and 99 bottles of beer

18 posts in this topic

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.org/wiki/99_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.

0

Share this post


Link to post
Share on other sites

a=100

99.times do
a -= 1
puts a.to_s + " bottles of beer on the wall, " + a.to_s + " bottles of beer."
puts "Take one down, pass it around, " + (a-1).to_s + " bottles of beer on the wall."
end

It's fairly easy to do in ruby.

0

Share this post


Link to post
Share on other sites

Hehe.

C++:


#include <iostream>
using namespace std;
int main()
{
for(int a = 99; a>=0;)
{
cout << a << "bottles of beer on the wall, " << a << "bottles of beer." << endl;
a--;
cout << "Take one down, pass it around, " << a << " bottles of beer on the wall." << endl << endl;
if(a == 0)
break;
}
system ("PAUSE");
return 0;
}

0

Share this post


Link to post
Share on other sites


#!/usr/bin/python
num=99
while num >= 1:
print "%d bottles of beer on the wall, " % num + "%d bottles of beer, " % num
num=num-1
print "Take one down, pass it around, %d bottles of beer on the wall.\n" % num


#!/usr/bin/perl
my $num=99;

while($num >= 1) {
print "$num bottles of beer on the wall, $num bottles of beer,\n";
print "Take one down, pass it around,";
$num--;
print " $num bottles of beer on the wall.\n\n";
}


public class Bottles99 {
public static void main(String args[]) {
for(int num=99; num >= 1;) {
System.out.print(num + " bottles of beer on the wall, " + num + " bottles of beer, ");
num--;
System.out.print("Take one down, pass it around, " + num + " bottles of beer on the wall.\n");
}
}
}

Edited by frogbong
0

Share this post


Link to post
Share on other sites

Here's mine... in C.

//99 bottles of beer on the wall
//PurpleJesus
#include <stdio.h>

int main (void)
{
int x;

for (x = 99; x; x--)
{
if (x >1)
printf("%i bottles of beer on the wall, %i bottles of beer. ",x,x);
else
printf("%i bottle of beer on the wall, %i bottle of beer. ",x,x);

printf("Take 1 down, pass it around. ");

if (x > 2)
printf("%i bottles ",x-1);

if (x == 2)
printf("1 bottle ");

if (x== 1)
printf("No more bottles ");

printf("of beer on the wall.\n");
}

return 0;
}

0

Share this post


Link to post
Share on other sites

#!/usr/bin/env python
num0 = input("How many bottles are on the wall? ")
num = num0 + 1
while num > 2:
num = num - 1
print num, "bottles of beer on the wall,\n", num, "bottles of beer.\nTake one down, and pass it around,\n", num - 1, "bottles of beer on the wall!\n"
print "1 bottle of beer on the wall,\n1 bottle of beer.\nTake one down, and pass it around,\nno more bottles of beer on the wall!"

Edited by seabass
0

Share this post


Link to post
Share on other sites

Here's mine, also in C.

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

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

#include <stdio.h>

char *arr1[] = {"Twen","Thir","Four","Fif",
"Six","Seven","Eigh","Nine"};
char *arr2[] = {"No more","One","Two","Three",
"Four","Five","Six","Seven","Eight",
"Nine","Ten","Eleven","Twelve","Thir",
"Four","Fif","Six","Seven","Eight",
"Nine"};
char *arr3[] = {" bottles of beer",
".\n"," on the wall",
"Take one down, pass it around, ",
", "};

void bottles_of_beer(int a, int {

if (a == 0 && b == 3) return;

if (b != 1) {
if (a >= 20) printf("%sty", arr1[(a / 10) - 2]);
if (a >= 20 && a % 10 != 0) printf("-%s", arr2[a % 10]);
if (a < 20) printf("%s", arr2[a]);
if (a < 20 && a > 12) printf("teen");
}

if (b == 3) {
printf("%s%s%s",arr3[0],arr3[2],arr3[4]);
bottles_of_beer(a, 2);
} else if (b == 2) {
printf("%s%s",arr3[0],arr3[1]);
bottles_of_beer(a-1, 1);
} else if (b == 1) {
printf("%s",arr3[3]);
bottles_of_beer(a, 0);
} else {
printf("%s%s%s",arr3[0],arr3[2],arr3[1]);
bottles_of_beer(a, 3);
}
}

int main(int argc, char **argv) {

bottles_of_beer(99,3);
return 0;
}

Edited by Seal
0

Share this post


Link to post
Share on other sites

Seal beat me to it..

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

//PurpleJesus
//wordy bottles of beer on the wall
//spells out the names of the numbers, like ninety-nine bottles of beer up to < 1000 trillion.
//no failsafe, too big a number will crash it. Thinking of making it spell out x million million's or something.




#include <stdio.h>
char powers[][20] =
{
{"nadda"},
{"nadda2"},
{"thousand"},
{"million"},
{"billion"},
{"trillion"},
};

char ones[][20] =
{
{"zero"},
{"one"},
{"two"},
{"three"},
{"four"},
{"five"},
{"six"},
{"seven"},
{"eight"},
{"nine"},
{"ten"},
{"eleven"},
{"twelve"},
{"thirteen"},
{"fourteen"},
{"fifteen"},
{"sixteen"},
{"seventeen"},
{"eightteen"},
{"nineteen"}
};

char tens[][20] =
{
{"filler0"},
{"filler1"},
{"twenty"},
{"thirty"},
{"fourty"},
{"fifty"},
{"sixty"},
{"seventy"},
{"eighty"},
{"ninety"},
{"filler100"},
};


int numberstowords (int x, int power)
{
if (!x)
printf("zero");

int z;
power++;
int space = 0;

if (x >= 1000) //see if we need to print a seperating comma for thousands, millions etc.
{
numberstowords (x / 1000,power);
if (x%1000) printf(", ");
}

z = x - x / 1000 * 1000; //yank the last three digits

if (z >= 100)
printf("%s-hundred",ones[z/100]);
space = z;

z = z - z / 100 * 100;

if (z)
{
if (space >= 100)
printf(" ");
if ( (z < 20) )
{
printf("%s",ones[z]);
}
else
{
printf("%s",tens[z/10]);

if (z - z / 10*10)
printf("-%s",ones[z - z/10*10]);
}
}

if (z)
if (power > 1)
printf("-%s",powers[power]);

return power;
}

int main (void)
{
int x;
int y;
printf("How many bottles of beer?");
scanf("%i",&y);

for (x = y; x; x--)
{
numberstowords(x,0);
if (x >1)
{
printf(" bottles of beer on the wall, ");
numberstowords(x,0);
printf(" bottles of beer. ");
}
else
{
printf(" bottle of beer on the wall, ");
numberstowords(x,0);
printf(" bottle of beer. ");
}

printf("Take one down, pass it around. ");

if (x > 1)
numberstowords(x-1,0);
if (x > 2)
printf(" bottles ");

if (x == 2)
printf(" bottle ");

if (x== 1)
printf("No more bottles ");

printf("of beer on the wall.\n");

}

return 0;
}

edit: I screwed up the spoiler tags.

Edited by PurpleJesus
0

Share this post


Link to post
Share on other sites

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

#!/usr/bin/perl

$x=$_==1?'':'s',$y=$_==2?'':'s',printf"$_ bottle$x of beer on the wall, $_ bottle$x of beer.

Take one down, pass it around, %d bottle$y of beer on the wall.

",$_-1 for reverse 1 .. pop||99

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

Share this post


Link to post
Share on other sites

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
1

Share this post


Link to post
Share on other sites


#!/usr/bin/perl -w
# don't you people care about germs? who the hell passes a beer around?
use strict;
my @beer = (0..99);
my @forbidden = (chr(80), chr(50));
my $e = " one down, " . $forbidden[0] . $forbidden[1] . $forbidden[0] . " it around";
my $beer0 = \@beer;
my $c = \"bottles"; #BBBBBBB. EEEEEEE EEEEEEE RRRRRRRR. ?????
my $b = \"beer"; #` B E` E` R` R ? ?
my $d = \"on the wall"; # B E E R R ?
my $r = ''; #BBBBBBB EEEEE EEEEE RRRRRRRR ?
my $in; #` B E` E` R` R ??
my @wget = (0..50); # B E E R R
@beer = reverse(@beer); # B E E R R. ?
foreach $_ (@$beer0) { #BBBBBBB` EEEEEEE EEEEEEE R RR ? sike.
$a = $_;
print "[$_] $$c$r of $$b $$d, [$_] $$c$r of $$b,\n"; $a--; if ($a == 1) {$r = "\b";} else {$r = '';}
print "./wget ["; if($a < 0) {print " ERROR 509. Bandwidth Limit Exceeded. ]\n\n";exit;} foreach $in (@wget) { print "="; }
print ">]\n$e [$a] $$c$r of $$b $$d.\n";
}exit;

Don't hurt meh!

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

0

Share this post


Link to post
Share on other sites


#!/usr/bin/perl -w
# don't you people care about germs? who the hell passes a beer around?
use strict;
my @beer = (0..99);
my @forbidden = (chr(80), chr(50));
my $e = " one down, " . $forbidden[0] . $forbidden[1] . $forbidden[0] . " it around";
my $beer0 = \@beer;
my $c = \"bottles"; #BBBBBBB. EEEEEEE EEEEEEE RRRRRRRR. ?????
my $b = \"beer"; #` B E` E` R` R ? ?
my $d = \"on the wall"; # B E E R R ?
my $r = ''; #BBBBBBB EEEEE EEEEE RRRRRRRR ?
my $in; #` B E` E` R` R ??
my @wget = (0..50); # B E E R R
@beer = reverse(@beer); # B E E R R. ?
foreach $_ (@$beer0) { #BBBBBBB` EEEEEEE EEEEEEE R RR ? sike.
$a = $_;
print "[$_] $$c$r of $$b $$d, [$_] $$c$r of $$b,\n"; $a--; if ($a == 1) {$r = "\b";} else {$r = '';}
print "./wget ["; if($a < 0) {print " ERROR 509. Bandwidth Limit Exceeded. ]\n\n";exit;} foreach $in (@wget) { print "="; }
print ">]\n$e [$a] $$c$r of $$b $$d.\n";
}exit;

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
0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

Python, naturally:

b = 99
while b > 1:
print b, "bottles of beer on the wall,", b, "bottles of beer! Take one down, pass it around,", b-1, "bottles of beer on the wall!"
b = b-1
print "one bottle of beer on the wall, one bottle of beer, take one down, pass it around,one bottle of beer on the wall"
print "no bottles of beer on the wall, no bottles of eber, go to the store, buy some more, 99 bottles of beer on the wall"

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
0

Share this post


Link to post
Share on other sites
/* 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...")

0

Share this post


Link to post
Share on other sites

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.


#!/usr/bin/perl

for ($bottles = 99; $bottles > 0; $bottles--) {
print "$bottles bottles of beer on the wall,\n";
print "$bottles bottles of beer.\n";
print "Take one down, and pass it around,\n";
$bottles_left = $bottles-1;
print "$bottles_left bottles of beer on the wall.\n\n";
}

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now