TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Show Off (http://www.talkphp.com/show-off/)
-   -   99 beers of bottle challenge (http://www.talkphp.com/show-off/4219-99-beers-bottle-challenge.html)

foobarph 05-04-2009 02:20 AM

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! :)

Village Idiot 05-04-2009 03:50 AM

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);} 


CoryMathews 05-04-2009 03:51 AM

Write it with recursion. That would remove some of those lines, but run about the same speed wise.

Orc 05-04-2009 03:52 AM

Quote:

Originally Posted by Village Idiot (Post 23623)
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. :-!

Village Idiot 05-04-2009 03:52 AM

Quote:

Originally Posted by Orc (Post 23625)
What the crap? I didn't know "until" was part of the flow control. :-!

Sorry, I'm not thinking PHP, its do while.

allworknoplay 05-04-2009 02:02 PM

Quote:

Originally Posted by Village Idiot (Post 23626)
Sorry, I'm not thinking PHP, its do while.

haha, too many languages in your head!!!

foobarph 05-06-2009 02:38 AM

haha! thanks for the thoughts guys. :)

Salathe 05-06-2009 11:48 AM

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?

Village Idiot 05-09-2009 04:28 PM

Quote:

Originally Posted by allworknoplay (Post 23635)
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).

allworknoplay 05-09-2009 04:34 PM

Quote:

Originally Posted by Village Idiot (Post 23906)
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?

foobarph 05-11-2009 04:01 AM

well, any hard-core LAMP devs here?

seriously, i envy allworknoplay for his mac-mini. huhuhu

Salathe 05-11-2009 07:24 AM

Quote:

Originally Posted by allworknoplay (Post 23907)
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.

Village Idiot 05-11-2009 07:31 PM

Never done objective C since I don't have a mac or iPhone.

foobarph 05-13-2009 10:22 AM

Quote:

Originally Posted by Village Idiot (Post 23972)
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?

ioan1k 05-13-2009 09:55 PM

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 :)

Wildhoney 05-13-2009 11:03 PM

Bravo! Good work, ioan1k :-)

ioan1k 05-14-2009 02:02 PM

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);



Salathe 05-14-2009 02:32 PM

The song should end with "Go to the store and buy some more, 99 bottles of beer on the wall." Full lyrics are available: http://99-bottles-of-beer.net/lyrics.html

ioan1k 05-14-2009 02:39 PM

Quote:

Originally Posted by ioan1k (Post 24034)
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.'


Salathe 05-15-2009 12:27 PM

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";



All times are GMT. The time now is 08:58 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0