View Poll Results: Who was the best beginner?

Voters
41. You may not vote on this poll
Page 3 of 27 FirstFirst 1234513 ... LastLast
Results 21 to 30 of 268

Thread: [Beginner] Programming Challenge: 1

  1. #21
    Join Date
    Apr 2007
    Location
    (X,Y,Z) = (0,0,0)
    Beans
    3,715

    Re: [Beginner] Programming Challenge: 1

    My first Python program! (Yes, believe me despite the dictionary with a tuple inside...)

    Update: As I'm a C guy, I felt forced to refactor the code a bit and make it even more structured as I could!

    PHP Code:
    #!/usr/bin/python

    def choose(i):
        
    special = {: ("no more""bottles"), : ("1""bottle")}
        if 
    i not in special:
            
    modulo = (i"bottles")
        else:
            
    modulo special[i]
        return 
    "%s %s" modulo
        
    99
    for i in range(b, -1, -1):
        print 
    "%s of beer on the wall, %s of beer." % (choose(i).capitalize(), 
        
    choose(i))
        if 
    0:
            print 
    "Take one down and pass it around,"choose(1),
        else:
            print 
    "Go to the store and buy some more,"choose(b),
        print 
    "of beer on the wall.\n" 
    Last edited by nvteighen; August 1st, 2008 at 06:55 PM. Reason: punctuation

  2. #22
    Join Date
    Jul 2008
    Beans
    1,491

    Re: [Beginner] Programming Challenge: 1

    I like the Haskell version they have... a lot.
    Last edited by Reiger; August 1st, 2008 at 02:06 PM.

  3. #23
    Join Date
    Mar 2008
    Beans
    1,755

    Re: [Beginner] Programming Challenge: 1

    Thanks for the challenge. Here is what I got:

    PHP Code:
    #!/usr/bin/python

    beerCount 99

    while beerCount > -1:
        if 
    beerCount == 0:
            print 
    "No more bottles of beer on the wall.\n" "No more bottles of beer.\n" "Go to the store and buy some more, 99 bottles of beer on the wall.\n"
        
    elif beerCount == 1:
            print 
    beerCount"bottle of beer on the wall.\n"beerCount"bottle of beer.\n""Take one down, pass it around,"beerCount"bottle of beer on the wall.\n"
        
    else:    
            print 
    beerCount"bottles of beer on the wall.\n"beerCount"bottles of beer.\n""Take one down, pass it around,"beerCount"bottles of beer on the wall.\n"
        
    beerCount beerCount 
    I took a a little formating advice from Reiger. I don't think I was missing any spaces, just capitalization and periods. Also, each verse did begin seperated by a blank line.

    * You should decrement the beerCount before 'passing it around'
    I don't understand what you mean here so I am just going to post my output:

    Code:
    jordan@einstein:~$ ./UbuntuChallenge
    99 bottles of beer on the wall.
    99 bottles of beer.
    Take one down, pass it around, 99 bottles of beer on the wall.
    
    98 bottles of beer on the wall.
    98 bottles of beer.
    Take one down, pass it around, 98 bottles of beer on the wall.
    
    97 bottles of beer on the wall.
    97 bottles of beer.
    Take one down, pass it around, 97 bottles of beer on the wall.
    
    96 bottles of beer on the wall.
    96 bottles of beer.
    Take one down, pass it around, 96 bottles of beer on the wall.
    
    95 bottles of beer on the wall.
    95 bottles of beer.
    Take one down, pass it around, 95 bottles of beer on the wall.
    
    94 bottles of beer on the wall.
    94 bottles of beer.
    Take one down, pass it around, 94 bottles of beer on the wall.
    
    93 bottles of beer on the wall.
    93 bottles of beer.
    Take one down, pass it around, 93 bottles of beer on the wall.
    
    92 bottles of beer on the wall.
    92 bottles of beer.
    Take one down, pass it around, 92 bottles of beer on the wall.
    
    91 bottles of beer on the wall.
    91 bottles of beer.
    Take one down, pass it around, 91 bottles of beer on the wall.
    
    90 bottles of beer on the wall.
    90 bottles of beer.
    Take one down, pass it around, 90 bottles of beer on the wall.
    
    89 bottles of beer on the wall.
    89 bottles of beer.
    Take one down, pass it around, 89 bottles of beer on the wall.
    
    88 bottles of beer on the wall.
    88 bottles of beer.
    Take one down, pass it around, 88 bottles of beer on the wall.
    
    87 bottles of beer on the wall.
    87 bottles of beer.
    Take one down, pass it around, 87 bottles of beer on the wall.
    
    86 bottles of beer on the wall.
    86 bottles of beer.
    Take one down, pass it around, 86 bottles of beer on the wall.
    
    85 bottles of beer on the wall.
    85 bottles of beer.
    Take one down, pass it around, 85 bottles of beer on the wall.
    
    84 bottles of beer on the wall.
    84 bottles of beer.
    Take one down, pass it around, 84 bottles of beer on the wall.
    
    83 bottles of beer on the wall.
    83 bottles of beer.
    Take one down, pass it around, 83 bottles of beer on the wall.
    
    82 bottles of beer on the wall.
    82 bottles of beer.
    Take one down, pass it around, 82 bottles of beer on the wall.
    
    81 bottles of beer on the wall.
    81 bottles of beer.
    Take one down, pass it around, 81 bottles of beer on the wall.
    
    80 bottles of beer on the wall.
    80 bottles of beer.
    Take one down, pass it around, 80 bottles of beer on the wall.
    
    79 bottles of beer on the wall.
    79 bottles of beer.
    Take one down, pass it around, 79 bottles of beer on the wall.
    
    78 bottles of beer on the wall.
    78 bottles of beer.
    Take one down, pass it around, 78 bottles of beer on the wall.
    
    77 bottles of beer on the wall.
    77 bottles of beer.
    Take one down, pass it around, 77 bottles of beer on the wall.
    
    76 bottles of beer on the wall.
    76 bottles of beer.
    Take one down, pass it around, 76 bottles of beer on the wall.
    
    75 bottles of beer on the wall.
    75 bottles of beer.
    Take one down, pass it around, 75 bottles of beer on the wall.
    
    74 bottles of beer on the wall.
    74 bottles of beer.
    Take one down, pass it around, 74 bottles of beer on the wall.
    
    73 bottles of beer on the wall.
    73 bottles of beer.
    Take one down, pass it around, 73 bottles of beer on the wall.
    
    72 bottles of beer on the wall.
    72 bottles of beer.
    Take one down, pass it around, 72 bottles of beer on the wall.
    
    71 bottles of beer on the wall.
    71 bottles of beer.
    Take one down, pass it around, 71 bottles of beer on the wall.
    
    70 bottles of beer on the wall.
    70 bottles of beer.
    Take one down, pass it around, 70 bottles of beer on the wall.
    
    69 bottles of beer on the wall.
    69 bottles of beer.
    Take one down, pass it around, 69 bottles of beer on the wall.
    
    68 bottles of beer on the wall.
    68 bottles of beer.
    Take one down, pass it around, 68 bottles of beer on the wall.
    
    67 bottles of beer on the wall.
    67 bottles of beer.
    Take one down, pass it around, 67 bottles of beer on the wall.
    
    66 bottles of beer on the wall.
    66 bottles of beer.
    Take one down, pass it around, 66 bottles of beer on the wall.
    
    65 bottles of beer on the wall.
    65 bottles of beer.
    Take one down, pass it around, 65 bottles of beer on the wall.
    
    64 bottles of beer on the wall.
    64 bottles of beer.
    Take one down, pass it around, 64 bottles of beer on the wall.
    
    63 bottles of beer on the wall.
    63 bottles of beer.
    Take one down, pass it around, 63 bottles of beer on the wall.
    
    62 bottles of beer on the wall.
    62 bottles of beer.
    Take one down, pass it around, 62 bottles of beer on the wall.
    
    61 bottles of beer on the wall.
    61 bottles of beer.
    Take one down, pass it around, 61 bottles of beer on the wall.
    
    60 bottles of beer on the wall.
    60 bottles of beer.
    Take one down, pass it around, 60 bottles of beer on the wall.
    
    59 bottles of beer on the wall.
    59 bottles of beer.
    Take one down, pass it around, 59 bottles of beer on the wall.
    
    58 bottles of beer on the wall.
    58 bottles of beer.
    Take one down, pass it around, 58 bottles of beer on the wall.
    
    57 bottles of beer on the wall.
    57 bottles of beer.
    Take one down, pass it around, 57 bottles of beer on the wall.
    
    56 bottles of beer on the wall.
    56 bottles of beer.
    Take one down, pass it around, 56 bottles of beer on the wall.
    
    55 bottles of beer on the wall.
    55 bottles of beer.
    Take one down, pass it around, 55 bottles of beer on the wall.
    
    54 bottles of beer on the wall.
    54 bottles of beer.
    Take one down, pass it around, 54 bottles of beer on the wall.
    
    53 bottles of beer on the wall.
    53 bottles of beer.
    Take one down, pass it around, 53 bottles of beer on the wall.
    
    52 bottles of beer on the wall.
    52 bottles of beer.
    Take one down, pass it around, 52 bottles of beer on the wall.
    
    51 bottles of beer on the wall.
    51 bottles of beer.
    Take one down, pass it around, 51 bottles of beer on the wall.
    
    50 bottles of beer on the wall.
    50 bottles of beer.
    Take one down, pass it around, 50 bottles of beer on the wall.
    
    49 bottles of beer on the wall.
    49 bottles of beer.
    Take one down, pass it around, 49 bottles of beer on the wall.
    
    48 bottles of beer on the wall.
    48 bottles of beer.
    Take one down, pass it around, 48 bottles of beer on the wall.
    
    47 bottles of beer on the wall.
    47 bottles of beer.
    Take one down, pass it around, 47 bottles of beer on the wall.
    
    46 bottles of beer on the wall.
    46 bottles of beer.
    Take one down, pass it around, 46 bottles of beer on the wall.
    
    45 bottles of beer on the wall.
    45 bottles of beer.
    Take one down, pass it around, 45 bottles of beer on the wall.
    
    44 bottles of beer on the wall.
    44 bottles of beer.
    Take one down, pass it around, 44 bottles of beer on the wall.
    
    43 bottles of beer on the wall.
    43 bottles of beer.
    Take one down, pass it around, 43 bottles of beer on the wall.
    
    42 bottles of beer on the wall.
    42 bottles of beer.
    Take one down, pass it around, 42 bottles of beer on the wall.
    
    41 bottles of beer on the wall.
    41 bottles of beer.
    Take one down, pass it around, 41 bottles of beer on the wall.
    
    40 bottles of beer on the wall.
    40 bottles of beer.
    Take one down, pass it around, 40 bottles of beer on the wall.
    
    39 bottles of beer on the wall.
    39 bottles of beer.
    Take one down, pass it around, 39 bottles of beer on the wall.
    
    38 bottles of beer on the wall.
    38 bottles of beer.
    Take one down, pass it around, 38 bottles of beer on the wall.
    
    37 bottles of beer on the wall.
    37 bottles of beer.
    Take one down, pass it around, 37 bottles of beer on the wall.
    
    36 bottles of beer on the wall.
    36 bottles of beer.
    Take one down, pass it around, 36 bottles of beer on the wall.
    
    35 bottles of beer on the wall.
    35 bottles of beer.
    Take one down, pass it around, 35 bottles of beer on the wall.
    
    34 bottles of beer on the wall.
    34 bottles of beer.
    Take one down, pass it around, 34 bottles of beer on the wall.
    
    33 bottles of beer on the wall.
    33 bottles of beer.
    Take one down, pass it around, 33 bottles of beer on the wall.
    
    32 bottles of beer on the wall.
    32 bottles of beer.
    Take one down, pass it around, 32 bottles of beer on the wall.
    
    31 bottles of beer on the wall.
    31 bottles of beer.
    Take one down, pass it around, 31 bottles of beer on the wall.
    
    30 bottles of beer on the wall.
    30 bottles of beer.
    Take one down, pass it around, 30 bottles of beer on the wall.
    
    29 bottles of beer on the wall.
    29 bottles of beer.
    Take one down, pass it around, 29 bottles of beer on the wall.
    
    28 bottles of beer on the wall.
    28 bottles of beer.
    Take one down, pass it around, 28 bottles of beer on the wall.
    
    27 bottles of beer on the wall.
    27 bottles of beer.
    Take one down, pass it around, 27 bottles of beer on the wall.
    
    26 bottles of beer on the wall.
    26 bottles of beer.
    Take one down, pass it around, 26 bottles of beer on the wall.
    
    25 bottles of beer on the wall.
    25 bottles of beer.
    Take one down, pass it around, 25 bottles of beer on the wall.
    
    24 bottles of beer on the wall.
    24 bottles of beer.
    Take one down, pass it around, 24 bottles of beer on the wall.
    
    23 bottles of beer on the wall.
    23 bottles of beer.
    Take one down, pass it around, 23 bottles of beer on the wall.
    
    22 bottles of beer on the wall.
    22 bottles of beer.
    Take one down, pass it around, 22 bottles of beer on the wall.
    
    21 bottles of beer on the wall.
    21 bottles of beer.
    Take one down, pass it around, 21 bottles of beer on the wall.
    
    20 bottles of beer on the wall.
    20 bottles of beer.
    Take one down, pass it around, 20 bottles of beer on the wall.
    
    19 bottles of beer on the wall.
    19 bottles of beer.
    Take one down, pass it around, 19 bottles of beer on the wall.
    
    18 bottles of beer on the wall.
    18 bottles of beer.
    Take one down, pass it around, 18 bottles of beer on the wall.
    
    17 bottles of beer on the wall.
    17 bottles of beer.
    Take one down, pass it around, 17 bottles of beer on the wall.
    
    16 bottles of beer on the wall.
    16 bottles of beer.
    Take one down, pass it around, 16 bottles of beer on the wall.
    
    15 bottles of beer on the wall.
    15 bottles of beer.
    Take one down, pass it around, 15 bottles of beer on the wall.
    
    14 bottles of beer on the wall.
    14 bottles of beer.
    Take one down, pass it around, 14 bottles of beer on the wall.
    
    13 bottles of beer on the wall.
    13 bottles of beer.
    Take one down, pass it around, 13 bottles of beer on the wall.
    
    12 bottles of beer on the wall.
    12 bottles of beer.
    Take one down, pass it around, 12 bottles of beer on the wall.
    
    11 bottles of beer on the wall.
    11 bottles of beer.
    Take one down, pass it around, 11 bottles of beer on the wall.
    
    10 bottles of beer on the wall.
    10 bottles of beer.
    Take one down, pass it around, 10 bottles of beer on the wall.
    
    9 bottles of beer on the wall.
    9 bottles of beer.
    Take one down, pass it around, 9 bottles of beer on the wall.
    
    8 bottles of beer on the wall.
    8 bottles of beer.
    Take one down, pass it around, 8 bottles of beer on the wall.
    
    7 bottles of beer on the wall.
    7 bottles of beer.
    Take one down, pass it around, 7 bottles of beer on the wall.
    
    6 bottles of beer on the wall.
    6 bottles of beer.
    Take one down, pass it around, 6 bottles of beer on the wall.
    
    5 bottles of beer on the wall.
    5 bottles of beer.
    Take one down, pass it around, 5 bottles of beer on the wall.
    
    4 bottles of beer on the wall.
    4 bottles of beer.
    Take one down, pass it around, 4 bottles of beer on the wall.
    
    3 bottles of beer on the wall.
    3 bottles of beer.
    Take one down, pass it around, 3 bottles of beer on the wall.
    
    2 bottles of beer on the wall.
    2 bottles of beer.
    Take one down, pass it around, 2 bottles of beer on the wall.
    
    1 bottle of beer on the wall.
    1 bottle of beer.
    Take one down, pass it around, 1 bottle of beer on the wall.
    
    No more bottles of beer on the wall.
    No more bottles of beer.
    Go to the store and buy some more, 99 bottles of beer on the wall.
    Last edited by Titan8990; August 1st, 2008 at 05:05 PM.

  4. #24
    Join Date
    Jul 2008
    Beans
    1,491

    Re: [Beginner] Programming Challenge: 1

    I'm not sure; as I don't know Python but intuitively I'd say your program has a few subtle 'formatting' issues?

    * Missing spaces (but that could be my lack of Python, as I see others with the same thing?)
    * Missing dots (.) at the end of a setence
    * Missing comma's and newlines instead of comma's
    * 'take' should be 'Take'
    * You should decrement the beerCount before 'passing it around'
    * Each verse is separated by a blank line

  5. #25
    Join Date
    Oct 2006
    Location
    Austin, Texas
    Beans
    2,712
    Distro
    Ubuntu 7.10 Gutsy Gibbon

    Re: [Beginner] Programming Challenge: 1

    Generates the song line-by-line

    Code:
    def song(n):
        bob = lambda x: "%(x)s%(bottles)s of beer" % {
            "x": x if x > 0 else "no more",
            "bottles": "bottle" if x == 1 else "bottles"}
        otw = lambda x: "%(b)s on the wall, %(b)s." % dict(b=bob(x))
        tod = lambda x: "Take one down, pass it around, %s on the wall." % bob(x)
        for i in xrange(n, 0, -1):
            yield otw(i)
            yield tod(i - 1)
    
    for line in song(99):
        print line
    The end of the song is really actually really sad though...
    Last edited by Wybiral; August 4th, 2008 at 02:52 PM.

  6. #26
    Join Date
    Mar 2008
    Beans
    9

    Re: [Beginner] Programming Challenge: 1

    Code:
    for (b=99;b>0;b--) {
    	word = 'bottle'
    	if (b>1) {
    		word += "s"
    	}
    	document.write(b+" "+word+" of beer on the wall, "+b+" "+word+" of beer.<br />Take one down and pass it around, "+(b-1)+" "+word+" of beer on the wall.<br /><br />");
    }
    
    document.write("No more bottles of beer on the wall, no more bottles of beer.<br />Go to the store and buy some more, 99 bottles of beer on the wall.")
    EDIT
    Code:
    for (b=99;b>0;b--) {
    	word = 'bottle'
    	if (b>1) {
    		word += "s"
    		b2 = b-1
    		if (b2>0) {
    			phrase = b2 + " bottle"
    			if (b2 > 1) {
    				phrase += "s"
    			}
    		}
    	} else {
    		phrase = "no more bottles"
    	}
    	document.write(b+" "+word+" of beer on the wall, "+b+" "+word+" of beer.<br />Take one down and pass it around, "+phrase+" of beer on the wall.<br /><br />");
    }
    
    document.write("No more bottles of beer on the wall, no more bottles of beer.<br />Go to the store and buy some more, 99 bottles of beer on the wall.")
    Last edited by the7erm; August 1st, 2008 at 04:56 PM.

  7. #27
    Join Date
    Aug 2006
    Beans
    11

    Re: [Beginner] Programming Challenge: 1

    Extra self-imposed challenge: use generator-expressions ; no if/then ;no real loops.

    PHP Code:
    #!/usr/bin/python 
    #extra self-imposed challenge: use generators ; no if/then ;no real loops.
    #

    def bottles(i):
        return {
    0:"No more bottles",1:"1 bottle",-1:"99 bottles"}.get(i,"%s bottles" i)

    template """
    %s of beer on the wall, %s of beer.
    %s, %s of beer on the wall.
    """

    print "".join((template %
        ( 
    bottles(i), bottles(i).lower(),
         {
    0:"Go to the store and buy some more"}.get(i,"Take one down and pass it around"),
         
    bottles(1).lower()
        ))
        for 
    i in xrange(99,-1,-1)) 
    @aktiwers : Please give your variables real names, a,b,c is not very descriptive ,exception : i as the default counter is a convention.

    Edit : bugfix for a special case I missed.

    Edit 2: Special cases are hard..

    This was my 1st version and it didn't need any bugfixes , much more readable, but i guess it's against the spirit of the challenge.
    PHP Code:
    #!/usr/bin/python 

    template """
    %(num)s bottles of beer on the wall, %(num)s bottles of beer.
    Take one down and pass it around, %(nextnum)s bottles of beer on the wall.
    """

    print "".join((template % {"num":i,"nextnum":1}) for i in xrange(99,2,-1))

    #special cases? easy:
    print """2 bottles of beer on the wall, 2 bottles of beer.
    Take one down and pass it around, 1 bottle of beer on the wall.

    1 bottle of beer on the wall, 1 bottle of beer.
    Take one down and pass it around, no more bottles of beer on the wall.

    No more bottles of beer on the wall, no more bottles of beer.
    Go to the store and buy some more, 99 bottles of beer on the wall.""" 
    Last edited by mvoncken; August 1st, 2008 at 05:57 PM. Reason: final edit (i hope) : fix #!usr/bin/python and other stuff.)

  8. #28
    Join Date
    Apr 2006
    Beans
    569
    Distro
    Xubuntu 12.04 Precise Pangolin

    Re: [Beginner] Programming Challenge: 1

    Hahaha -this doesn't work right! What's wrong with me. Fixed version on down the thread.

    Code:
    #!/usr/bin/env python
    
    beers=99
    wall="on the wall"
    while beers >= 0:
    	if beers == 0:
    		print "We ain't got no beer."
    		break
    	if beers > 1: bottles=str(beers)+" bottles of beer"
    	elif beers <= 1: bottles=str(beers)+" bottle of beer"
    	out= "%s %s. %s. Take one down pass it around, " %(bottles,wall,bottles)
    	beers-=1
    	out+= "%s bottles of beer %s." %(str(beers),wall)
    	print out
    Last edited by arsenic23; August 1st, 2008 at 08:59 PM. Reason: #/usr/bin/python to #!/usr/bin/env python

  9. #29
    Join Date
    Mar 2008
    Beans
    1,755

    Re: [Beginner] Programming Challenge: 1

    I thought yours looked interesting arsenic but I get a syntax error trying to run it.

    Edit: mvoncken your forgot the "/" for root directory on line 1:

    #!usr/bin/python

  10. #30
    Join Date
    Feb 2007
    Location
    Edinburgh, Scotland
    Beans
    391

    Re: [Beginner] Programming Challenge: 1

    Boring one in Python:

    Code:
    bottles = range(0, 99)
    bottles.reverse() #Reverse it otherwise we will be counting up
    buffer = "" #Where we store the song up until we print it
    
    for count in bottles:
        if count > 2: #'Bottles' is plural
            buffer += "%s bottles of beer on the wall, %s bottles of beer\n" % (count, count)
            buffer += "Take one down and pass it around, %s bottles of beer on the wall.\n\n" % (count - 1)
        elif count == 2: #Last bottle is singular
            buffer += "%s bottles of beer on the wall, %s bottles of beer\n" % (count, count)
            buffer += "Take one down and pass it around, %s bottle of beer on the wall.\n\n" % (count - 1)
        elif count == 1: #'Bottle' is singular
            buffer += "%s bottle of beer on the wall, %s bottle of beer\n" % (count, count)
            buffer += "Take one down and pass it around, no more bottles of beer on the wall.\n\n"
        elif count == 0: #No bottles left
            buffer += "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."
    
    #Dump the buffer onto the screen
    print buffer
    Today we have 15 minutes of fame, tomorrow 15 minutes of anonymity.
    My Blog | Veza - Opensource TinyURL clone with sensible URLs

Page 3 of 27 FirstFirst 1234513 ... LastLast

Bookmarks

Posting Permissions

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