TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 05-04-2009, 02:20 AM   #1 (permalink)
The Wanderer
 
foobarph's Avatar
 
Join Date: May 2009
Posts: 23
Thanks: 4
foobarph is on a distinguished road
Default 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! :)
foobarph is offline  
Reply With Quote
Old 05-04-2009, 03:50 AM   #2 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

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

Village Idiot is offline  
Reply With Quote
Old 05-04-2009, 03:51 AM   #3 (permalink)
The Addict
 
CoryMathews's Avatar
 
Join Date: Nov 2007
Location: USA
Posts: 256
Thanks: 7
CoryMathews is on a distinguished road
Default

Write it with recursion. That would remove some of those lines, but run about the same speed wise.
CoryMathews is offline  
Reply With Quote
Old 05-04-2009, 03:52 AM   #4 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default

Quote:
Originally Posted by Village Idiot View Post
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
Orc is offline  
Reply With Quote
Old 05-04-2009, 03:52 AM   #5 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

Quote:
Originally Posted by Orc View Post
What the crap? I didn't know "until" was part of the flow control.
Sorry, I'm not thinking PHP, its do while.
__________________

Village Idiot is offline  
Reply With Quote
Old 05-04-2009, 02:02 PM   #6 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

Quote:
Originally Posted by Village Idiot View Post
Sorry, I'm not thinking PHP, its do while.
haha, too many languages in your head!!!
allworknoplay is offline  
Reply With Quote
Old 05-06-2009, 02:38 AM   #7 (permalink)
The Wanderer
 
foobarph's Avatar
 
Join Date: May 2009
Posts: 23
Thanks: 4
foobarph is on a distinguished road
Default

haha! thanks for the thoughts guys. :)
foobarph is offline  
Reply With Quote
Old 05-06-2009, 11:48 AM   #8 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

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?
Salathe is offline  
Reply With Quote
Old 05-09-2009, 04:28 PM   #9 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

Quote:
Originally Posted by allworknoplay View Post
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).
__________________

Village Idiot is offline  
Reply With Quote
Old 05-09-2009, 04:34 PM   #10 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

Quote:
Originally Posted by Village Idiot View Post
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?
allworknoplay is offline  
Reply With Quote
Old 05-11-2009, 04:01 AM   #11 (permalink)
The Wanderer
 
foobarph's Avatar
 
Join Date: May 2009
Posts: 23
Thanks: 4
foobarph is on a distinguished road
Default

well, any hard-core LAMP devs here?

seriously, i envy allworknoplay for his mac-mini. huhuhu
foobarph is offline  
Reply With Quote
Old 05-11-2009, 07:24 AM   #12 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

Quote:
Originally Posted by allworknoplay View Post
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.
Salathe is offline  
Reply With Quote
The Following User Says Thank You to Salathe For This Useful Post:
foobarph (05-13-2009)
Old 05-11-2009, 07:31 PM   #13 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

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

Village Idiot is offline  
Reply With Quote
The Following User Says Thank You to Village Idiot For This Useful Post:
foobarph (05-13-2009)
Old 05-13-2009, 10:22 AM   #14 (permalink)
The Wanderer
 
foobarph's Avatar
 
Join Date: May 2009
Posts: 23
Thanks: 4
foobarph is on a distinguished road
Default

Quote:
Originally Posted by Village Idiot View Post
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?
foobarph is offline  
Reply With Quote
Old 05-13-2009, 09:55 PM   #15 (permalink)
The Contributor
 
ioan1k's Avatar
 
Join Date: Mar 2009
Location: US
Posts: 76
Thanks: 0
ioan1k is on a distinguished road
Default

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 :)
__________________
My Portfolio - Work - Need freelance Work?
I've been developing 5 years now, and I learn something new everyday
ioan1k is offline  
Reply With Quote
Old 05-13-2009, 11:03 PM   #16 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

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.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 05-14-2009, 02:02 PM   #17 (permalink)
The Contributor
 
ioan1k's Avatar
 
Join Date: Mar 2009
Location: US
Posts: 76
Thanks: 0
ioan1k is on a distinguished road
Default

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

__________________
My Portfolio - Work - Need freelance Work?
I've been developing 5 years now, and I learn something new everyday
ioan1k is offline  
Reply With Quote
Old 05-14-2009, 02:32 PM   #18 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

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
Salathe is offline  
Reply With Quote
Old 05-14-2009, 02:39 PM   #19 (permalink)
The Contributor
 
ioan1k's Avatar
 
Join Date: Mar 2009
Location: US
Posts: 76
Thanks: 0
ioan1k is on a distinguished road
Default

Quote:
Originally Posted by ioan1k View Post
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.'
__________________
My Portfolio - Work - Need freelance Work?
I've been developing 5 years now, and I learn something new everyday
ioan1k is offline  
Reply With Quote
Old 05-15-2009, 12:27 PM   #20 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

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";
Salathe is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

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


All times are GMT. The time now is 01:17 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design