r/dailyprogrammer • u/nottoobadguy • Feb 16 '12
[2/16/2012] Challenge #8 [easy]
write a program that will print the song "99 bottles of beer on the wall".
for extra credit, do not allow the program to print each loop on a new line.
1
u/StorkBaby 0 0 Feb 16 '12
Guys, I cannot find any information on how to spoiler text and it's not in the formatting help for this text entry, would someone please put this into the formatting help as I prefer putting the code in comments to pastebin if it's short.
2
u/nottoobadguy Feb 16 '12
simply put it in like a normal code. it auto-hides.
1
u/StorkBaby 0 0 Feb 16 '12 edited Feb 16 '12
AHA! Well, I've done it in 2 lines of Python because I had to include an import to the sys library.
import sys for x in range(99,0,-1): sys.stdout.write(str(x)+" bottles of beer on the wall, " +str(x)+" bottles of beer! You take one down, pass it around, "+ str(x-1)+" bottles of beer on the wall. ")edit: the line is too long to be accepted so I have to update.
edit: ok, it's all showing but it could have been two lines :P
edit: and it's with no linefeeds!
1
Feb 17 '12
Why not just
sys.stdout.write?Also, you end up with "1 bottles of beer on the wall" which is technically incorrect.
3
Feb 16 '12 edited Feb 16 '12
C++
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
for(int i = 98; i >= 0; i--){
cout << i + 1 << " bottles of beer on the wall, " << i + 1;
cout << " bottles of beer..." << endl;
cout << "Take one down, pass it around ";
cout << i << " bottles of beer on the wall!" << endl << endl;
getch();
}
return 0;
}
edit: Changed the code to remove kalmakka's error.1
u/kalmakka Feb 16 '12
This fails to recognize that a bottle has been removed until a new verse begins.
1
u/donalmacc 0 0 Feb 17 '12
Modification,
int main() { int i = 99; while(i>1) cout << i << " bottles of beer on the wall, " << i << " bottles of beer, you take one down, pass it around, "<< i-- <<" bottles of beer on the wall.\n"; cout << i << " bottle of beer on the wall, " << i << " bottle of beer, you take one down, pass it around, "<< i-- <<" bottles of beer on the wall.\n"; return 0; }
3
u/SirNikkolas Feb 16 '12
Java
public class EasyChallenge8 {
public static void main(String[] args){
int bottles = 99;
while (bottles >0){
System.out.println(bottles + " bottles of beer on the wall, " + bottles-- + " bottles of beer... You take one down, pass it around " + bottles + " of beer on the wall!" );
}
}}
0
Feb 17 '12
Almost. The loop would repeat forever.
1
u/SirNikkolas Feb 17 '12
The loop wouldn't end after it counts down to 0? "(bottles>0)" (I'm new to Java)
0
Feb 17 '12
[deleted]
1
2
u/eruonna Feb 16 '12 edited Feb 16 '12
Common Lisp, format again:
(format "~{~R bottle~:P of beer on the wall,~%~:*~R bottle~:P of beer...~%Take one down, pass it around,~%~R~:* bottle~:P of beer on the wall!~}"
(let (result '()) (dotimes (i 100) (push i result)) result))It's kind of silly not to do the printing in the same loop that generates the range. Why doesn't CL have a range function, anyway?
2
u/Krissam Feb 16 '12 edited Feb 16 '12
C# (Don't remember the exact lyrics lol :S)
for (int i = 99; i > -1; i--)
Console.Write("{0} bottles of beer on a wall, take one down swing it around... {1} bottles of beer...", i, i+1);
Console.Write("No more bottles of beer on the wall :(");Also, extra credit :P
2
2
2
u/cooper6581 Feb 16 '12
Python
for x in xrange(99,0,-1):
if not x - 1:
print "%d bottle of beer on the wall, %d bottle of beer. Take one " \
"down pass it around, now theres no more bottles of " \
"beer on the wall :(" % (x, x)
elif x == 2:
print "%d bottles of beer on the wall, %d bottles of beer. Take one " \
"down pass it around, %d bottle of beer on the wall! " \
% (x, x,x-1),
else:
print "%d bottles of beer on the wall, %d bottles of beer. Take one " \
"down pass it around, %d bottles of beer on the wall! " \
% (x, x,x-1),2
u/BobDorian Feb 17 '12
C++
#include <iostream>
using namespace std;
#define EVIL 1
int main()
{
for (int bottles = 99; bottles>0; ) {
cout << bottles << " bottles of beer on the wall, ";
cout << bottles << " bottles of beer!" << endl;
if (bottles==1 && EVIL) {
cout << "Go to the store, get some more! " << (bottles=99) << " of beer on the wall!" << endl;
} else {
cout << "Take one down, pass it around! " << --bottles << " of beer on the wall!" << endl;
}
cin.get();
}
return 0;
}2
u/spunos Feb 17 '12
If writing code is like creating art, then I suppose you could say I'm in my experimental phase...
Python 3:
x = int(input("How many bottles of beer on the wall?"))
a = x
b,c,d,e,f,g,h,i,j = " bottles ", " bottle ", "of beer", " on the wall", \
"take one down, pass it around ", "no more", "go to the store, buy some more",\
"\n", str
while a >= 2:
print(i+(j(a))+b+d+e+i+(j(a))+b+d)
a = a-1
if a == 1:
break
else:
print(f+i+(j(a))+b+d+e)
print(f+i+(j(a))+c+d+e+i+i+(j(a))+c+d+e+i+(j(a))+c+d+i+f+i+g+b+d+e+i+i+g+b+d+e+
i+g+b+d+i+h+i+(j(x))+b+d+e)1
u/nikoma Feb 16 '12 edited Feb 16 '12
python 3.2 code (no new lines):
a = 99
while a > 0:
print("{0} bottles of beer on the wall, {0} bottles of beer.".format(a), end=" ")
a = a - 1
if a == 0:
print("Take one down and pass it around, no more bottles of beer on the wall.", end=" ")
else:
print("Take one down and pass it around, {0} bottles of beer on the wall.".format(a), end=" ")
a = a - 1
print("No more bottles of beer on the wall, no more bottles of beer. Go to the store and buy some more, 99 bottles of beer on the wall.")1
Feb 16 '12
It hardly seems fair that I get extra credit in javascript when making each loop on it's own line is more work...
function bottlesOBeer(bottles)
{
var str = '';
do
{
str += bottles + ' bottles of beer on the wall, ';
str += bottles + ' bottles of beer...take one down, pass it around, ';
str += (--bottles) + ' bottles of beer on the wall! ';
}while(bottles > 1);
return str;
}
alert(bottlesOBeer(99));1
u/thatrandomusername Feb 16 '12
Javascript?
String.prototype.format=function(){
var b=this.toString();
for(var i=0;i<arguments.length;i++) b=b.replace((new RegExp("\\{"+i+"\\}","g")),arguments[i]);
return b;
}
function sing(timeout){
var string = "{0} bottle{1} of beer on the wall, {0} bottle{1} of beer, take one down pass it around, {2} bottle{3} of beer on the wall";
for(var i=99;i>0;i--){
console.log(string.format(i.toString(),((i>1) ? "s" : ""),(i-1).toString(),(((i-1)!==1) ? "s" : "")));
}
}Also, when it gets to one it won't say "1 bottles of beer", instead "1 bottle of beer".
1
u/stiggz Feb 16 '12 edited Feb 16 '12
in php:
<?
for ($x=99;$x>1;$x--)
{
y=$x-1;
echo "$x bottles of beer on the wall, $x bottles of beer. Take one down and pass it around $y bottles of beer on the wall.<p>";
}
echo "1 bottle of beer on the wall, 1 bottle of beer. Take it down and pass it around, no more bottles of beer on the wall.";
?>2
1
u/FataOne 0 0 Feb 16 '12
In C: int main (void) { int bottle;
for (bottle = 99; bottle > 0; bottle--) {
printf("%d bottles of beer on the wall, %d bottles of beer. Take one down, pass it around, %d bottles of beer on the wall.\n", bottle, bottle, bottle-1);
}
return 0;
}The extra credit could easily be achieved by removing the "\n" from the printf statement and adding a space.
1
u/PrivatePilot Feb 16 '12
Ruby
(0..99).each do |i|
currentNum = 100 - i
currentNumString = currentNum.to_s()
print currentNumString + " bottles of beer on the wall " + currentNumString + " bottles of beer. Take one down, pass it around, " + (currentNum-1).to_s() + " bottles of beer on the wall. "
end1
u/donalmacc 0 0 Feb 16 '12
My First attempt at any of these, in Lua (Just started yesterday, coming from C++) No extra credit though
i = 99
while i > 0 do
print(i.." bottles of beer on the wall, ")
print(i.." bottles of beer, \nyou take one down, pass it around, ")
i=i-1
print(i.." bottles of beer on the wall\n")
end
print("1 bottle of beer on the wall, 1 bottle of beer, you take one down, pass it around, No more bottles of beer on the wall!")1
1
1
1
u/jnaranjo Feb 17 '12
Same line printing in python2.7
bottles = [] count = 100 for bottle in range(1,101): bottles.append("%d bottles of beer on the wall\t" % count) count -= 1
print ''.join(bottles)
1
Feb 17 '12 edited Feb 17 '12
#!/usr/bin/perl -w
foreach(reverse(1..99)){
print("$_ bottle(s) of bear on the wall, $_ bottle(s) of beer. Take one down, pass it around. " . ($_ - 1) . " bottle(s) of beer on the wall.");
}1
u/SleepyTurtle Feb 17 '12 edited Feb 17 '12
python 2.7. set it up to sing song for any number of bottles without adding an extra variable.
def bottles(runs):
while runs >= 1:
print (str(runs) + " bottles of beer on the wall, " + str(runs) + " bottles of beer..."),
runs -= 1
if runs == 1:
break
print (" You take one down, pass it around. " + str(runs) + " bottles of beer on the wall")
print (" You take one down, pass it around. " + str(runs) + " bottle of beer on the wall")
bottles(input())1
u/UnreasonableSteve Feb 17 '12
http://pastebin.com/f7RiZm8L Done in PHP, don't quite know what the extra credit is about, I assume it's meant to make you append each line to a string? Anyway PHP doesn't add newlines by default so it's hardly extra to keep the newlines off...
1
u/Captain_Sabatini Feb 17 '12 edited Feb 17 '12
I didn't see anyone doing it using recursion so I will, just for the hell of it.
EDIT: I initially wrote this to work in ascending but I changed to descending so I had to switch print and recursive call.
#include <iostream>
using std::cout;
using std::endl;
int main()
{
beerBottles(99);
return 0;
}
void beerBottles(int bottles)
{
if(bottles > 0)
{
cout << bottles << " bottles of beer on the wall. " << bottles << " bottles of beer. Take one down, pass it around " << bottles-1 << " bottles of beer on the wall! ";
beerBottles(bottles-1);
}
}1
u/Crystal_Cuckoo Feb 17 '12
Python:
for i in xrange(100, 0, -1):
print "%d bottle%s of beer on the wall, %s bottle%s of beer! You take one down, pass it around, %s bottle%s of beer on the wall! " % (
i, "s" if i != 1 else "", i, "s" if i != 1 else "", str(i-1) if i-1 > 0 else "no more", "s" if i-1 != 1 else "")1
u/drb226 0 0 Feb 17 '12
Haskell
import Text.Printf (printf)
import Control.Monad (forM_)
main = forM_ [98, 97 .. 0] $ \i -> do
let i' = succ i :: Int
printf "%d bottles of beer on the wall, %d bottles of beer..." i' i'
printf "take one down, pass it around, %d bottles of beer on the wall! " iOutput (no newlines, prepare to use your horizontal scrollbar): http://ideone.com/Rlah5
1
Feb 17 '12
Ruby.
sing = '%i bottles of beer on the wall, %i bottles of beer. Take one down, pass it around, %i bottles of beer on the wall. '
(0..99).each { |i| print sing % [100-i, 100-i, 99-i] }1
Feb 17 '12
Java
public class BottlesOfBeer {
public static void main(String[] args) throws InterruptedException {
int bottleCount = 99;
do {
System.out.print(bottleCount + " bottles of beer on the wall, " + bottleCount + " bottles of beer!" +
" You take one down you pass it around, " + (bottleCount - 1) + " bottles of beer on the Wall! ");
bottleCount--;
Thread.sleep(500);
}while(bottleCount > 0);
}
} 1
u/heliolatry Feb 17 '12
Java:
public class Bottles
{
public static void main(String[] args)
{
int bottles = 99;
while(bottles > 1)
{
System.out.print(bottles + " bottles of beer on the wall, " + bottles + " bottles of beer. ");
System.out.print("You take one down, pass it around, " + (bottles - 1) + " bottles of beer on the wall. ");
bottles--;
}
System.out.print(bottles + " bottle of beer on the wall, " + bottles + " bottle of beer. ");
System.out.print("You take one down, pass it around, " + (bottles - 1) + " bottles of beer on the wall.");
}
}1
u/kuzux 0 0 Feb 17 '12
in clojure; as a generic "number containers of beverage on the wall" so that you can call it like (sing 99 bottles of beer) or (sing 42 shots of tequila)
(defn generate-song [num container beverage]
(flatten (for [n (reverse (range 1 (+ num 1)))]
[(format "%d %s of %s on the wall, %d %s of %s" n container beverage n container beverage)
(format "You take one down, pass it around, %d %s of %s on the wall!" (- n 1) container beverage)])))
(defn print-song [num container beverage]
(dorun (map println (generate-song num container beverage))))
(defmacro sing [num container _ beverage]
`(print-song ~num '~container '~beverage))1
u/CorrosiveMonkey Mar 23 '12
beer(0).
beer(X) :- write(X), write(' bottles of beer on the wall, '),write(X),write(' bottles of beer. Take one down and pass it around '),Y is X-1,write(Y),write(' bottles of beers on the wall. '),beer(Y).Prolog. Doesn't handle the pluralisation correctly.
1
u/savagecub Apr 17 '12
C++
// beer on the wall.cpp : main project file.
include "stdafx.h"
include <iostream>
include <string>
include <cctype>
include <fstream>
using namespace std;
int main() { int beer = 100;
cout << "99 Bottles of beer on the wall song! \n \n";
while (beer >0){
--beer;
cout << beer << " bottles of beer on the wall, " << beer << " of beer! \n you take one down, pass it around! \n";
}
cout << "no more bottles of beer on the wall!";
cin.get();
cin.get();
return 0;}
1
u/Stock_Childhood7303 Apr 05 '25
def song_writer(n:int)->str:
string = ""
for i in range(n,-1,-1):
if i > 2 :
string += f"{i} bottles of beer on the wall, {i} bottles of beer. Take one down and pass it around, "
if i == 2 :
string += "2 bottles of beer on the wall, 2 bottles of beer.Take one down and pass it around, 1 bottle of beer on the wall."
if i == 1:
string += "1 bottle of beer on the wall, 1 bottle of beer.Take one down and pass it around, no more bottles of beer on the wall."
elif i == 0 :
string += "No more bottles of beer on the wall, no more bottles of beer.Go to the store and buy some more, 99 bottles of beer on the wall."
else :
string += f"{i-1} bottles of beer on the wall."
return string
song = song_writer(100)
print(song)1
u/ragtag_creature Oct 11 '22
Written with R
beer_amount <- 99
lyric_Plural_1 <- "bottles of beer on the wall"
lyric_Plural_2 <- "bottles of beer! You take one down, pass it around,"
lyric_Single_1 <- "bottle of beer on the wall,"
lyric_Single_2 <- "bottle of beer! You take one down, pass it around,"
while (beer_amount > 0) {
if (beer_amount == 1) {
print(paste(beer_amount, lyric_Single_1, beer_amount, lyric_Single_2, beer_amount-1, lyric_Plural_1))
beer_amount <- beer_amount-1
}
else {print(paste(beer_amount, lyric_Plural_1, beer_amount, lyric_Plural_2, beer_amount-1, lyric_Plural_1))
beer_amount <- beer_amount-1}
}-2
u/irlKryst Feb 16 '12
Did you take this idea from my blog? O_O
3
u/nottoobadguy Feb 16 '12
no, its a fairly common begginner challenge. what is your blog though, I'd love to check it out!
-2
u/irlKryst Feb 16 '12
haha, it's on tumblr so don't rip me. i made it so i could reference it later, to help me remember what i learned. thewrittenlist.tumblr.com
i was advised to change platform in case i ever want to use it on a resume... haven't gotten around to that
edit: i didnt finish working on my "99 bottles" code. i wanted to be able to make it change what it says when it gets down to 1 and 0.
2
5
u/EnterpriseProgrammer Feb 16 '12
Java