 |
Account Login
|
 |
 |
Latest Articles
|
 |
 |
IRC Channel
|
 |
 |
Associates
|
 |
 |
Associates
|
 |
|
 |
|
 |
|
 |
05-04-2009, 02:20 AM
|
#1 (permalink)
|
|
The Wanderer
Join Date: May 2009
Posts: 23
Thanks: 4
|
99 beers of bottle challenge
just to have some fun and tried to beat the 99 beer bottle challenge...
visit http://99-bottles-of-beer.net/language-php-1825.html for more information.
Code:
<?php
/* 99 bottles of beer by foobarph (http://rllqph.wordpress.com) */
for ($i = 99; $i >= 0; $i--) {
$bottles = $i;
$usedBottles = $i-1;
if ($i == 0) {
$action = "Go to the store and buy some more, ";
$sBottles = "bottle";
$bottles = "no more";
}
if ($i == 1) {
$sBottles = "bottle";
}
if ($i > 1) {
$action = "Take one down and pass it around, ";
$sBottles = "bottles";
}
if ($usedBottles == 0) {
$usedBottles = "no more";
}
if ($usedBottles < 0) {
$usedBottles = "99";
$sBottles = "bottles";
}
echo ucfirst($bottles) . " ". $sBottles ." of beer on the wall, ". $bottles ." ". $sBottles ." of beer.<br />";
echo $action . $usedBottles ." ". $sBottles ." of beer on the wall.<br /><br />";
}
?>
any comments on how i can improve this code? feel free to post them here. thanks! :)
|
|
|
|
05-04-2009, 03:50 AM
|
#2 (permalink)
|
|
Wizard
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
|
So what exactly is the challenge? To have your program sing 99 bottles of beer on the wall in the fewest lines of code? If so, try this
PHP Code:
function beer($beerCount =99) { do{ echo "$beerCount bottles of beer on the wall bla bla"; $beercount--; } while($beerCount > 1) echo "One bottle of beer on the wall bla bla"; echo "There none left, go sober up and buy some more"; beer(99); }
If its sheer lines of code:
PHP Code:
function beer($beerCount =99){do{echo "$beerCount bottles of beer on the wall bla bla";$beercount--;} while($beerCount > 1)echo "One bottle of beer on the wall bla bla";echo"There none left, go sober up and buy some more";beer(99);}
|
|
|
|
05-04-2009, 03:51 AM
|
#3 (permalink)
|
|
The Addict
Join Date: Nov 2007
Location: USA
Posts: 256
Thanks: 7
|
Write it with recursion. That would remove some of those lines, but run about the same speed wise.
|
|
|
|
05-04-2009, 03:52 AM
|
#4 (permalink)
|
|
The Prestige
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
|
Quote:
Originally Posted by Village Idiot
So what exactly is the challenge? To have your program sing 99 bottles of beer on the wall in the fewest lines of code? If so, try this
PHP Code:
function beer($beerCount =99)
{
do{
echo "$beerCount bottles of beer on the wall bla bla";
$beercount--;
} until($beerCount == 1)
echo "One bottle of beer on the wall bla bla";
echo "There none left, go sober up and buy some more";
beer(99);
}
|
What the crap? I didn't know "until" was part of the flow control. 
__________________
VillageIdiot can have my babbies ;d
|
|
|
|
05-04-2009, 03:52 AM
|
#5 (permalink)
|
|
Wizard
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
|
Quote:
Originally Posted by Orc
What the crap? I didn't know "until" was part of the flow control. 
|
Sorry, I'm not thinking PHP, its do while.
|
|
|
|
05-04-2009, 02:02 PM
|
#6 (permalink)
|
|
The Gregarious
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
|
Quote:
Originally Posted by Village Idiot
Sorry, I'm not thinking PHP, its do while.
|
haha, too many languages in your head!!!
|
|
|
|
05-06-2009, 02:38 AM
|
#7 (permalink)
|
|
The Wanderer
Join Date: May 2009
Posts: 23
Thanks: 4
|
haha! thanks for the thoughts guys. :)
|
|
|
|
05-06-2009, 11:48 AM
|
#8 (permalink)
|
|
Moderateur
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
|
Topic title "99 beers of bottle challenge"—someone's been on the bottles! Is anyone going to write their own script which sings the song?
|
|
|
|
05-09-2009, 04:28 PM
|
#9 (permalink)
|
|
Wizard
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
|
Quote:
Originally Posted by allworknoplay
haha, too many languages in your head!!!
|
No doubt. I program VB, ASP and SQL as a job, I help people with PHP and MySql here and I program in C++ for fun (I've been getting familliar with linux's source to get into OS programming).
|
|
|
|
05-09-2009, 04:34 PM
|
#10 (permalink)
|
|
The Gregarious
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
|
Quote:
Originally Posted by Village Idiot
No doubt. I program VB, ASP and SQL as a job, I help people with PHP and MySql here and I program in C++ for fun (I've been getting familliar with linux's source to get into OS programming).
|
Geez! I mainly focus on PHP, MYSQL, I am looking to get certified in Redhat linux though. And of course all the other side languages that I need to pick up like javascript, jquery, ajax...I'm sure some will take offense to me calling them side languages haha..ok..client side languages...that's better...
In any event, how about objective-C? are you interested?
I am...I just bought a Mac-mini and I'd like to learn a little iphone programming....maybe Salathe?
|
|
|
|
05-11-2009, 04:01 AM
|
#11 (permalink)
|
|
The Wanderer
Join Date: May 2009
Posts: 23
Thanks: 4
|
well, any hard-core LAMP devs here?
seriously, i envy allworknoplay for his mac-mini. huhuhu
|
|
|
|
05-11-2009, 07:24 AM
|
#12 (permalink)
|
|
Moderateur
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
|
Quote:
Originally Posted by allworknoplay
I am...I just bought a Mac-mini and I'd like to learn a little iphone programming....maybe Salathe?
|
Nothing would interest me less.
|
|
|
|
|
The Following User Says Thank You to Salathe For This Useful Post:
|
|
05-11-2009, 07:31 PM
|
#13 (permalink)
|
|
Wizard
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
|
Never done objective C since I don't have a mac or iPhone.
|
|
|
|
|
The Following User Says Thank You to Village Idiot For This Useful Post:
|
|
05-13-2009, 10:22 AM
|
#14 (permalink)
|
|
The Wanderer
Join Date: May 2009
Posts: 23
Thanks: 4
|
Quote:
Originally Posted by Village Idiot
Never done objective C since I don't have a mac or iPhone.
|
well, me too. but hey, what is the programming language i should use so i can create a program for iphone?
|
|
|
|
05-13-2009, 09:55 PM
|
#15 (permalink)
|
|
The Contributor
Join Date: Mar 2009
Location: US
Posts: 76
Thanks: 0
|
8 Lines
PHP Code:
for ($i = 1; $i <= 99; $i++) { $first = 99 - ($i - 1); echo sprintf('%s bottles of beer on the wall %s bottles of beer <br />', $first, $first); $last = ($i == 99 ) ? 'no more' : 99 - $i; echo sprintf('Take one down pass it around %s bottles of beer on the wall <br />', $last); if ($i == 99) { echo 'You are all out of beer...good game'; } }
By the time u get to one youl be to drunk to pronounce bottles as bottle :)
|
|
|
|
05-13-2009, 11:03 PM
|
#16 (permalink)
|
|
La Vida es Sueño
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
|
Bravo! Good work, ioan1k 
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
|
|
|
05-14-2009, 02:02 PM
|
#17 (permalink)
|
|
The Contributor
Join Date: Mar 2009
Location: US
Posts: 76
Thanks: 0
|
Even Shorter...... and gives the correct ending :)
PHP Code:
for ($i = 1; $i <= 99; $i++) { $first = ($i != 99) ? 99 - ($i - 1).' bottles of beer ' : ' 1 bottle of beer '; $last = ($i == 99) ? ' no more bottles ' : (($i == 98) ? '1 bottle ' : 99 - $i.' bottles '); echo sprintf('%s on the wall %s <br /> Take one down pass it around of %s beer on the wall <br />', $first, $first, $last); }
|
|
|
|
05-14-2009, 02:39 PM
|
#19 (permalink)
|
|
The Contributor
Join Date: Mar 2009
Location: US
Posts: 76
Thanks: 0
|
Quote:
Originally Posted by ioan1k
Even Shorter...... and gives the correct ending :)
PHP Code:
for ($i = 1; $i <= 99; $i++) { $first = ($i != 99) ? 99 - ($i - 1).' bottles of beer ' : ' 1 bottle of beer '; $last = ($i == 99) ? ' no more bottles ' : (($i == 98) ? '1 bottle ' : 99 - $i.' bottles '); echo sprintf('%s on the wall %s <br /> Take one down pass it around of %s beer on the wall <br />', $first, $first, $last); }
|
:)
PHP Code:
echo 'Go to the store and buy some more, 99 bottles of beer on the wall.';
|
|
|
|
05-15-2009, 12:27 PM
|
#20 (permalink)
|
|
Moderateur
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
|
Just passed a little time during lunch.
PHP Code:
<?php
function a($n){$a=$n%100or$a='no more';return
sprintf("%s bottle%s of beer",$a,(--$n)?'s':'');}
for ($w=' on the wall',$n=100;1+$s=a($n);)
echo ucfirst($s),$w,', ',$s,".\n",$n?
'Take one down and pass it around, '
:'Go to the store and buy some more, ',
a($n--?$n:99),$w,'.',"\n\n";
|
|
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
Similar Threads
|
| Thread |
Thread Starter |
Forum |
Replies |
Last Post |
| New php challenge |
sarmenhb |
General |
7 |
01-30-2008 08:03 AM |
| new challenge!! |
sarmenhb |
General |
6 |
01-27-2008 03:44 AM |
|