View Poll Results: Who was the best beginner?

Voters
41. You may not vote on this poll
Page 1 of 27 12311 ... LastLast
Results 1 to 10 of 268

Thread: [Beginner] Programming Challenge: 1

  1. #1
    Join Date
    Apr 2007
    Beans
    14,781

    [Beginner] Programming Challenge: 1

    We have at other times had weekly programming challenges. They, when I read them, seemed to get more advanced as they went because the winner got to choose the next challenge, each one was the idea of a somewhat experienced programmer and sometimes very experienced.

    I am not sure how we will have someone choose the next challenge, but we'll figure it out when we get there

    There is no time limit either, but I am thinking any interest in this should be apparent within this week.

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

    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.

    Rules:
    Do not copy code, but you can obviously read it

    Try not to comment on other submissions. It will be hard to judge changing entries. If you edit your code, post the new version and make it clear there is a new code.

    How it will be judged
    Obviously, this is a task that is trivial (one big output statement would work) so the winner will be judged on the following:

    • 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.
    • Logical code. Be a programmer, not a code monkey
    • Commented code. If something is possibly unclear (like an obscure use of math), let the readers know. Do not over comment, and let the code speak for itself whenever you can. Have logical variable names and function names. (Excessive commenting is a minus factor)
    • Working code. It has to work as it is posted. The codw will be read on the forum, so remember to use this guide to paste code if you don't know how: http://ubuntuforums.org/showthread.php?t=606009


    Hints:
    • When you get to 1 bottle, it is a singular "bottle", whereas all other instances are plural.
    • Using this challenge to try out a new language brings you bonus points, so please state when you are using the language for the first time if you are.
    Last edited by LaRoza; August 1st, 2008 at 02:51 AM.

  2. #2
    Join Date
    Jun 2006
    Location
    Gwangju, Korea
    Beans
    3,479

    Re: [Beginner] Programming Challenge: 1

    Another hint (or two):

    • Don't rely too heavily on the programs on that site. Many are poorly-written and a surprising number are incorrect.
    • The text to print contains another special case that LaRoza didn't mention and that many programs on that website miss. I'll leave you to figure that special case out on your own.

  3. #3
    Join Date
    Nov 2007
    Beans
    273
    Distro
    Xubuntu 8.10 Intrepid Ibex

    Re: [Beginner] Programming Challenge: 1

    Do criticize. I probably could have just had one string and then modified it as I went.

    PHP Code:
    #!/usr/bin/python
    #
    # Throat Saver ver. 1
    # Sings the song "99 Bottles of Beer" for you.

    beers 99

    for x in range(beers 1):
        
        
    # Grammatical Specifications
        
    if beers == 0:
            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.\n"
            
        
    elif beers == 1:
            print 
    "1 bottle of beer on the wall, 1 bottle of beer.\nTake one down and pass it around, no more bottles of beer on the wall.\n"
        
        
    elif beers == 2:
            print 
    "2 bottles of beer on the wall, 2 bottles of beer.\nTake one down and pass it around, 1 bottle of beer on the wall.\n"
            
        
    # Normal Use
        
    else:
            print 
    beers" bottles of beer on the wall, "beers" bottles of beer.\nTake one down and pass it around, ", (beers 1), " bottles of beer on the wall.\n"
        
        
    # Take it down
        
    beers beers 
    Last edited by Yuki_Nagato; August 1st, 2008 at 02:23 AM.
    Angel's Egg: Go watch this, and then the movie. You have never seen anything like it.
    A shout-out to the Linux Mint team for their excellent port of Ubuntu.
    Use any OS worry-free with Derek's Virtual Box.

  4. #4
    Join Date
    Aug 2007
    Location
    Manchester, UK
    Beans
    10,279
    Distro
    Ubuntu

    Re: [Beginner] Programming Challenge: 1

    I thought I'd include the C version

    It's probably rubbish, it's been a while since I did any programming
    PHP Code:
    #include <stdio.h>

    main()
    {
            
    int i 99;

            for(
    i1i--)
            {
                    
    printf("%d bottles of beer on the wall, %d bottles of beer.\nTake one down and pass it around, %d bottles of beer on the wall.\n\n"iii-1);

                    if(
    == 2)
                    {
                            
    printf("%d bottles of beer on the wall, %d bottles of beer.\nTake one down and pass it around, %d bottle of beer on the wall.\n\n"iii-1);
                    }
            }

            
    printf("1 bottle of beer on the wall, 1 bottle of beer.\nTake one down and pass it around, no more bottles of beer on the wall.\n\n");
            
    printf("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.\n\n");

            
    //I was lazy with the last 2. Sorry :)

            
    return 0;


  5. #5
    Join Date
    Jan 2008
    Location
    Raytown, MO
    Beans
    460
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: [Beginner] Programming Challenge: 1

    LaRoza,

    Nice choice for a first beginner's challenge!

    Steve
    The Tcler's Wiki http://wiki.tcl.tk Tcl Developer Exchange http://www.tcl.tk SQLite http://www.sqlite.org

  6. #6
    Join Date
    Jul 2008
    Location
    India
    Beans
    62
    Distro
    Ubuntu 11.10 Oneiric Ocelot

    Re: [Beginner] Programming Challenge: 1

    I'm new to this forum.. I saw this today morning.. and it was great to break the ice

    PHP Code:
    // Code done in C++
    // Bottles of Beer :)

    #include<iostream.h>

    int main()
    {
     
     
    int beers=99;                       //Thats the number of beer bottles on the wall

     
    for(beers=99;beers>=0;beers--)     //Keep decrememnting the number of beer bottles we have, till it equals zero
     
    {
      
    //number-specific corrections to be made:
      
      
    if(beers==1)
      {
       
    cout<<"1 bottle of beer on the wall, 1 bottle of beer.\n";
       
    cout<<"Take one down and pass it around, no more bottles of beer on the wall.\n\n";
      }
      
      else if(
    beers==2)
      {
       
    cout<<"2 bottles of beer on the wall, 2 bottles of beer.\n";
       
    cout<<"Take one down and pass it around, 1 more bottle of beer on the wall.\n\n";
      }
      
      else if(
    beers==0)
      {
       
    cout<<"No more bottles of beer on the wall, no more bottles of beer.\n";
       
    cout<<"Go to the store and buy some more, 99 bottles of beer on the wall.\n\n";
      }
      
      
    // for all other numbers:
      
    else
      {  
       
    cout<<beers<<" bottles of beer on the wall, "<<beers<<" bottles of beer.\n";
       
    cout<<"Take one down and pass it around, "<<(beers-1)<<" bottles of beer on the wall.\n\n";
      }
     }                                   
    // end of the 'for' loop
    return 0;                           //Since main was declared 'int'


  7. #7
    Join Date
    Nov 2006
    Location
    Pennsylvania
    Beans
    423

    Re: [Beginner] Programming Challenge: 1

    I'll submit my answer tomorrow. I am pretty sure I can do it
    Your Ubuntu User number is # 15355

    A must Read for anyone interested in Computer Programming.

  8. #8
    Join Date
    Apr 2006
    Location
    Sheffield - England
    Beans
    Hidden!

    Re: [Beginner] Programming Challenge: 1

    Here's my submission. It features gratuitous abuse of the format macro. There is a similar submission on the site that shows some really crazy use of the format macro which I don't understand.

    Code:
    (defun bottles ()
      (format t
    	       "~:{~A bottl~
                    es of beer ~
                    on the wall~
                    , ~A bottle~
                   s of beer.~%~
                  Take one down a~
                nd pass it around, ~
             ~A bottles of beer on the ~
           wall.~%~%~}2 bottles of beer o~
          n 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 do~
          wn and pass it around, no more b~
          ottles of beer on the wall.~%~%~
          No more bottles of beer on the w~
          all, no more bottles of beer.~%~
          Go to the store and buy some mor~
          e, 99 bottles of beer on the wall."
          ;-------------------------------;
          ;       __   __  __  __         ;
          ;      |__| |_  |_  |__|        ;
          ;      |__| |__ |__ |  \        ;
          ;                               ;
          ;-------------------------------;
          ;  +-------+---------+-------+  ;
          ;  |       |         |       |  ;
          ;  |       |         |       |  ;
          ;  |       |         |       |  ;
          ;  +-------+---------+-------+  ;
          ;_______________________________;
          (loop for i from 99 downto 3
    	 collect (list i i (1- i)))))
    Last edited by Alasdair; August 1st, 2008 at 02:38 PM.

  9. #9
    Join Date
    Nov 2007
    Beans
    706
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Re: [Beginner] Programming Challenge: 1

    LaRoza, you're a wonderful person. You know that? I've hoped someone would do something like this. It's perfect for me, 'cause when the first challenges were posted on this forum, I wasn't good enough to do them. And now that I am, I don't want to because the competition is gone. So, here it goes:

    EDIT: Updated code to fix a formatting problem
    EDIT2: Updated code to fix another formatting problem

    PHP Code:
    /* Prints the lyrics to "99 Bottles of Beer on The Wall" */

    #include <stdio.h>

    /* FUNCTIONS PROTOTYPES */
    void give_lyrics(int);

    int main(){
        
    int bottles;

        for (
    bottles 99bottles >= 1bottles--){
            
    give_lyrics(bottles);
        }

        
    printf("No more bottles of beer on the wall, no more bottles of beer.\n"
               "Go to the store and buy some more, 99 bottles of beer on the wall.\n"
    );
        
        
    getchar();       
        return 
    0;
    }

    /* FUNCTIONS */
    void give_lyrics(int bottles){
         
    char *arry malloc(8);
         
    arry "bottles";
         if (
    bottles == 2){
            
    arry "bottle";
         }
         if (
    bottles 1){
            
    printf("%i bottles of beer on the wall, %i bottles of beer.\n"
                   "Take one down, pass it around, %i %s of beer on the wall\n\n"

                   
    bottlesbottlesbottles 1arry);
         }
         else{
            
    printf("%i bottle of beer on the wall, %i bottle of beer.\n"
                   "Take one down, pass it around, no more bottles of beer on the wall\n\n"

                   
    bottlesbottles);
         }

    Last edited by Sinkingships7; August 4th, 2008 at 08:57 PM.
    Programming is an art. Learn it. Live it. Love it.

  10. #10
    Join Date
    Apr 2006
    Location
    Phoenix, AZ
    Beans
    251
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: [Beginner] Programming Challenge: 1

    Compact version (with no comments) in ruby I did years ago (code that won had to be smallest in size).

    Code:
    b=" bottles of beer"
    w=" on the wall"
    99.downto 1 do|x|
    puts"#{x}#{b}#{w}, #{x}#{b}."
    b=b.delete"s"if x<3
    print"Take one down and pass it around, #{x-1}#{b}#{w}.\n\n"if x>1
    end 
    puts"Go to the store and buy some more, 99 bottles of beer#{w}."
    -Skeeterbug

Page 1 of 27 12311 ... 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
  •