Please login or register.

Login with username, password and session length
Advanced search  

Author Topic: [Beginner] Programming Challenges I  (Read 3126 times)

nukpana

  • Hero Member
  • *****
  • Karma: 71
  • Posts: 663
[Beginner] Programming Challenges I
« on: July 22, 2010, 03:58:04 AM »

Cool idea I saw at Ubuntuforums.org where beginner programmers are challenged with some situations.  

So, who wants to try the first one?

Quote
The Challenge:
Write a program, in any free language, that prints to the terminal the lyrics to this song, 99 Bottles of Beer.

http://99-bottles-of-beer.net/lyrics.html

If you copy anything from that site, it will be obvious, so don't try it, although you may find that site useful for seeing code of many different languages.

Obviously I would expect to see PHP and JS code from here...
« Last Edit: July 22, 2010, 04:01:17 AM by nukpana »
Logged

nukpana

  • Hero Member
  • *****
  • Karma: 71
  • Posts: 663
Re: [Beginner] Programming Challenges I
« Reply #1 on: July 22, 2010, 03:59:03 AM »

Here is my (un-whatever) try:

Code: [Select]
<?php

function 
descCount($num) {
for ($i $num$i 0$i--) {
$array[] = $i;
}
return $array;
}

$num descCount(99);
array_push($num'no more');

foreach($num as $n) :

// Decending number
$d $n != 
$n 1
99;

// Last descender
$d $d == 0
'no more'
$d;

// Plural Code
$npl $n == 1
''
's';
$dpl $d == 1
''
's';

$secndStmnt $n == 0
'Go to the store and buy some more'
'Take one down and pass it around';

?>

<?php echo ucfirst($n); ?> bottle<?php echo $npl?> of beer on the wall, <?php echo $n?> bottle<?php echo $npl?> of beer.
<?php echo $secndStmnt?>, <?php echo $d?> bottle<?php echo $dpl?> of beer on the wall.
<br />
<?php endforeach; ?>
Logged