## View Poll Results: Who was the best beginner?

Voters
41. You may not vote on this poll
• 3 7.32%
• 2 4.88%
• 1 2.44%
• 2 4.88%
• 5 12.20%
• 1 2.44%
• 3 7.32%
• 8 19.51%
• 0 0%
• Other

16 39.02%

# Thread: [Beginner] Programming Challenge: 1

1. ## Re: [Beginner] Programming Challenge: 1

A little messy, but here's mine. (In Python, obviously.)

Illustrates function recursion, type casting to string, and the string.replace() method. It should be fairly self-explanatory. Also illustrates a form of if-else optimisation: ie. check for the most common and computationally least expensive occurrence first, and "else-if" the least common and computationally most expensive occurrence last.

PHP Code:
``` #! /usr/bin/python# This function prints the constructed verse from form_verse(bottles) function.def write_verse(verse):    print verse# This function constructs each verse (consisting of two lines) to be printed to screen.def form_verse(bottles):    # Cast the integer variable bottles, and integer result from bottles - 1, into strings.    # This allows concatenation of these values with other strings, using the "+" operator.    bwall = str(bottles)    bleft = str(bottles - 1)        # These two lines form the two lines of a verse.  If the bottles on the wall is greater    #   than 2 and smaller than 100, theses can be printed to screen as is.    line1 = bwall + " bottles of beer on the wall, " + bwall + " bottles of beer.\n"    line2 = "Take one down and pass it around, " + bleft + " bottles of beer on the wall.\n"        # Check for special cases! Notice how the most common and simplest occurrence is checked first.    # The last case (bottles == 0) is the least common and involves the most complex action. It also    #   results in the end of this program using the "return" keyword to exit the function.    if bottles > 2:        write_verse(line1 + line2)    elif bottles == 2:        line2 = line2.replace("bottles", "bottle")        write_verse(line1 + line2)    elif bottles == 1:        line1 = line1.replace("bottles", "bottle")        line2 = line2.replace("0", "no more")        write_verse(line1 + line2)    elif bottles == 0:        # Something tricky with the next two lines. :)        # If I use just the first line and not the second, then the sentence is        #   grammatically wrong. Try it.        line1 = line1.replace("0", "no more")        line1 = line1.replace("no more", "No more", 1)        line2 = "Go to the store and buy some more, 99 bottles of beer on the wall."        write_verse(line1 + line2)        return        # Demonstrates recursion: a function invoking itself. Notice that the bottles parameter is    #   is reduced by 1 each time this function runs, until it reaches 0.    form_verse(bottles - 1)# Calls form_verse(bottles) function, which will keep calling itself until bottles = 0.form_verse(99)  ```
Last edited by samjh; August 3rd, 2008 at 08:16 AM.

2. Banned
Join Date
Apr 2007
Beans
14,781

## Re: [Beginner] Programming Challenge: 1

Looking good everyone. Now, I know some of you aren't beginners and this challenge series will be aimed at those starting to learn programming

I will chose the winner soon (after it seems no one else is going to submit) but because this thread is aimed at beginners, it would be nice if people gave their input on other people's code at the end to help.

3. ## Re: [Beginner] Programming Challenge: 1

Originally Posted by LaRoza
Looking good everyone. Now, I know some of you aren't beginners and this challenge series will be aimed at those starting to learn programming
I think those of us who are more experienced should try to produce code which demonstrates concepts or styles that beginners can learn from. It's what I tried to do in my submission.

4. Banned
Join Date
Apr 2007
Beans
14,781

## Re: [Beginner] Programming Challenge: 1

Originally Posted by samjh
I think those of us who are more experienced should try to produce code which demonstrates concepts or styles that beginners can learn from. It's what I tried to do in my submission.
Yes, I know. I just didn't want a new programmer coming in and seeing those tricks and getting depressed

5. ## Re: [Beginner] Programming Challenge: 1

Originally Posted by aktiwers
Edit: Why does my first output line only show 98 and not the second verse?
Hint: Where are you decrementing your counter, and/or where are you starting your range?

6. 5 Cups of Ubuntu
Join Date
May 2008
Location
Australia
Beans
33
Distro
Ubuntu 9.04 Jaunty Jackalope

## Re: [Beginner] Programming Challenge: 1

First, I'm a huge fan of these challenges so thanks to LaRoza and everyone else that's made one. I've learnt alot from these, Project Euler, Challenge-You and Python Challenge that never quite stuck when read in a tutorial or book.

Here's my effort, as with life, if you give the drinking task to an object when it's all over you'll only be left with one thing

Code:
```#!/usr/bin/env python

def drink(n):
stock = zip(range(n, 0, -1), range(n-1, 0, -1) + ['no more'])
case = {1: 'bottle'}
for before, after in stock:
print '%s %s of beer on the wall, %s %s of beer.' % (before, case.get(before, 'bottles'), before, case.get(before, 'bottles'))
print 'Take one down and pass it around, %s %s of beer on the wall.\n' % (after, case.get(after, 'bottles'))
print 'No more bottles of beer on the wall, no more bottles of beer.\nGo to the store and buy some more, 99 bottles of beer on the wall.'
return 'Hangover'

drink(99)```

7. Just Give Me the Beans!
Join Date
Apr 2008
Beans
54

## Re: [Beginner] Programming Challenge: 1

Mono (basic, straightforward, sledgehammer method):

Code:
```using System;

class Program
{
public static void Main(string[] args)
{
int beer;           // a counter variable to keep up with how much beer I have
for (beer = 99; beer > -1; beer--) // make sure you decrement instead of increment, or else you'll have infinite imaginary beer. . .
{
switch (beer) // an if alternative
{
case 0:
Console.WriteLine("No more bottles of beer on the wall, no more bottles of beer.");
Console.WriteLine("Go to the store and buy some more, 99 bottles of beer on the wall.");
Console.WriteLine("");
break;  // don't forget the breaks, it'll print too many extra sentences
case 1:
Console.WriteLine(beer + " bottle of beer on the wall, " + beer + " bottle of beer.");
Console.WriteLine("Take one down and pass it around, no more bottles of beer on the wall.");
Console.WriteLine("");
break;
case 2:
Console.WriteLine(beer + " bottles of beer on the wall, " + beer + " bottles of beer.");
Console.WriteLine("Take one down and pass it around, " + (beer - 1) + " bottle of beer on the wall.");      // don't forget parenthesis, it's a compile time syntax error
Console.WriteLine("");
break;
default:
Console.WriteLine(beer + " bottles of beer on the wall, " + beer + " bottles of beer.");
Console.WriteLine("Take one down and pass it around, " + (beer - 1) + " bottles of beer on the wall.");
Console.WriteLine("");
break;
}
}
}
}```
I considered a foreach loop, but I didn't want to have to hand type the array. ("99", "98", "97", "96", ..., "3", "2", "1", "No more")

edit: Hidden from those who might want to complete the challenge without reading my code.
Last edited by descendency; August 2nd, 2008 at 05:06 AM.

8. ## Re: [Beginner] Programming Challenge: 1

Originally Posted by descendency
edit: Hidden from those who might want to complete the challenge without reading my code.
For a minute, I thought you'd decided to write it in Whitespace.

9. ## Re: [Beginner] Programming Challenge: 1

Originally Posted by LaRoza
Clean code. Readable code is a must for being a programmer who wishes to possibly interact with others. Some languages can be more readable than others, so it will be judged on the effort of writing clean code, not which language is easier to read by design.
Why do you discriminate against me?

10. Banned
Join Date
Apr 2007
Beans
14,781

## Re: [Beginner] Programming Challenge: 1

Originally Posted by slavik
Why do you discriminate against me?
I specifically said the criteria would be based on effort, not structure of the language:
so it will be judged on the effort of writing clean code, not which language is easier to read by design.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•