View Poll Results: Who was the best beginner?

Voters
41. You may not vote on this poll
Page 2 of 27 FirstFirst 123412 ... LastLast
Results 11 to 20 of 268

Thread: [Beginner] Programming Challenge: 1

  1. #11
    Join Date
    Aug 2006
    Location
    60°27'48"N 24°48'18"E
    Beans
    3,452

    Re: [Beginner] Programming Challenge: 1

    An infinite lazy list in Clojure:

    Code:
    (defn beer [n]
      
      (defn beerstring [x, initcap]
        (cond (= x 0) (str (if initcap "No" "no") " more bottles")
    	  (= x 1) "1 bottle"
    	  true (str x " bottles")))
      
      (defn beer-internal [y]
        (lazy-cons
         (str (beerstring y true) " of beer on the wall, " (beerstring y false) " of beer\n"
    	  (if (= y 0)
    	    (str "Go to the store and buy some more, " (beerstring n false) " of beer on the wall\n")
    	    (str "Take one down and pass it around, " (beerstring (- y 1) false) " of beer on the wall\n")))
         (if (> y 0) 
           (beer-internal (- y 1))
           (beer-internal n))))
    
      (beer-internal n))
    (The newlines don't work right but what the heck)

    With this list you can (take) as many bottles as you like, pass around the lazy list, and there's as much beer as anybody can drink...

  2. #12
    Join Date
    Mar 2007
    Beans
    172

    Re: [Beginner] Programming Challenge: 1

    PHP Code:
    #include <stdio.h>
    #include <stdlib.h>

    charitoa(int n){char_n malloc(3); sprintf(_n"%i"n); return _n;};
    int main()
    {
        
    int i;
        for(
    99>= 0i--)
        {
            
    printf("%s %s of beer on the wall, %s %s of beer. \n%s %s %s of beer on the wall.\n\n",
                    
    i==0?"No more":itoa(i), i==1?"bottle":"bottles",
                    
    i==0?"no more":itoa(i), i==1?"bottle":"bottles",
                    
    i==0?"Go to the store and buy some more,":"Take one down and pass it around,",
                    
    i==0?"99":i==1?"no more":itoa(i-1), i==2?"bottle":"bottles");
        }
        return 
    0;


  3. #13
    Join Date
    Mar 2007
    Beans
    125

    Re: [Beginner] Programming Challenge: 1

    First program using Python.

    PHP Code:
    #!/usr/bin/python

    def main():
        
    bottles 99
        
        
    # Let's keep singing forever!
        
    while 1:
        
            if 
    bottles 2:
                print 
    str(bottles) + " bottles of beer on the wall, " str(bottles) + " bottles of beer."
                
    bottles -= 1
                
    print "Take one down and pass it around, " str(bottles) + " bottles of beer on the wall."
                
            
    elif bottles == 2:
                print 
    str(bottles) + " bottles of beer on the wall, " str(bottles) + " bottles of beer."
                
    bottles -= 1
                
    print "Take one down and pass it around, " str(bottles) + " bottle of beer on the wall."
                
            
    elif bottles == 1:
                print 
    str(bottles) + " bottle of beer on the wall, " str(bottles) + " bottle of beer."
                
    bottles -= 1
                
    print "Take one down and pass it around, no more bottles of beer on the wall."
                
            
    elif bottles == 0:
                print 
    "No more bottles of beer on the wall, no more bottles of beer."
                
    bottles 99
                
    print "Go to the store and buy some more, " str(bottles) + " bottles of beer on the wall."

    if __name__ == '__main__':
        
    main() 

  4. #14
    Join Date
    Jul 2008
    Beans
    9

    Re: [Beginner] Programming Challenge: 1

    Hi all, a really simple version done in Common Lisp. I tried to make it as clear as possible, which I think I achieved. However, It's probably not very Lisp like.

    It works on my computer (SBCL + Slime), so hopefully shouldn't be a problem on yours.

    Code:
    ;;;Common Lisp version by Maveric
    ;;;;I've tried to make this as simple, and easy to follow as possible.
    ;;;;I decided to tac the last sentence (Go to the store...) in the main loop, 
    ;;;;As I think it looks slightly neater.
    
    (defun beer-print (num)
      (if (>= num 2)
          (format t "~R bottles of beer on the wall,~
                     ~:*~r bottles of beer. ~1% Take one down and pass it around,~
                     ~r bottles of beer on the wall.~2% " num (- num 1))
          (format t "~r bottle of beer on the wall, ~:*~r bottle of beer.~
                     ~1% Take one down and pass it around,~
                     no bottles of beer on the wall ~2%" num)))
    
    (defun main-loop ()
      (loop :for x :from 99 :downto 1 :do (beer-print x))
      (format t "No more bottles of beer on the wall, no more bottles of beer.~
                 ~1%Go to the store and buy some more, 99 bottles of beer on the wall."))

  5. #15
    Join Date
    Mar 2006
    Location
    Denmark - Copenhagen
    Beans
    2,162

    Re: [Beginner] Programming Challenge: 1

    I am just starting to learn python, thanks for a fun challenge.
    (have no idea if this is the correct "style of writing python code, but it works)

    PHP Code:
    #!/usr/bin/python
    #
    # Drink drink! ver. 0.1
    # "99 Bottles of Beer". 

    ###################################
    # This is the lyrics of the song #
    #################################

    # This is the general used lyrics
    "bottles of beer on the wall, "
    "bottles of beer."
    "Take one down and pass it around," 
    "bottles of beer on the wall."

    # This is the lyrics used at the end of the song
    # Note that a1 and b1 is == to a and b just that we are singing about 1 bottle
    a1 "bottle of beer on the wall, "
    b1 "bottle of beer."
    "Take one down and pass it around, no more bottles of beer on the wall. \n"
    "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"

    ############################################
    # This is the loop that produces the song #
    ##########################################
    99

    while >= 3:
        
    n-1
        
    print nanb"\n" cn-1d"\n\n"
    if == 2:
        print 
    n-1a1n-1b1"\n" e
        
    print 
    Last edited by aktiwers; August 1st, 2008 at 10:01 AM. Reason: typo

  6. #16
    Join Date
    Apr 2007
    Location
    NorCal
    Beans
    1,149
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: [Beginner] Programming Challenge: 1

    for loops were about as far as I got at my last attempt learning to program, so this is a good first challenge for me.


    PHP Code:
    #/usr/bin/python

    for i in range(99,1,-1) :
        print 
    i"bottles of beer on the wall,\n"i"bottles of beer,\nTake one down, pass it around,"
        
    1
        
    if :
            print 
    i"bottles of beer on the wall.\n"
        
    else :
            print 
    i"bottle of beer on the wall.\n"

    # Kind of cheated here, but oh well. Assigning parts of the sentence to variables would have eliminated the verbosity of this last line as the cost of readability (to me, at least)
    print "1 bottle of beer on the wall,\n1 bottle of beer,\nTake one down, pass it around,\nno more bottles of beer on the wall.\n"
    print "No more bottles of beer on the wall,\nno more bottles of beer.\nGo to the store and buy some more,\n99 bottles of beer on the wall." 
    Last edited by schauerlich; August 1st, 2008 at 10:48 AM.
    Posting code? Use the [code] or [php] tags.
    I don't care, I'm still free. You can't take the sky from me.

  7. #17
    Join Date
    Jun 2008
    Beans
    204
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: [Beginner] Programming Challenge: 1

    Here it is in Fortran90, though im not with my ubuntu comp at the mo (But cant wait either) so havnt compiled it, I really should have it working first time, but you never know there may be that annoying spelling error or something.
    I like this whole idea of programming challenges I've not come across (or have turned up too late) for any previous ones. I'll try and keep up with these

    Code:
    PROGRAM 99_Bottles_Of_Beer
    
    	IMPLICIT NONE
    
    	INTEGER	:: Bottles = 99
    
    
    	!Do loop to print the bulk of lines where grammar is constant
    	DO WHILE (Bottles > 2)
    		PRINT *, Bottles, "Bottles Of Beer On The Wall", Bottles, "Bottles Of Beer"
    		Bottles = Bottles - 1
    		PRINT *, "Take One Down And Pass It Around", Bottles, "Bottles Of Beer On The Wall"
    	END DO 
    
    	!If statement to print the other grammatical cases
    	IF (Bottles == 2) THEN
    		PRINT *, Bottles, "Bottles Of Beer On The Wall", Bottles, "Bottles Of Beer"
    		Bottles = Bottles - 1
    		PRINT *, "Take One Down And Pass It Around", Bottles, "Bottle Of Beer On The Wall"
    
    	ELSE IF ( Bottles == 1) THEN	
    		PRINT *, Bottles, "Bottle Of Beer On The Wall", Bottles, "Bottle Of Beer"
    		Bottles = Bottles - 1
    		PRINT *, "Take One Down And Pass It Around No More Bottles Of Beer On The Wall"
    	
    	ELSE IF (Bottles == 0) THEN
    		PRINT *, "No more bottles of beer on the wall, no more bottles of beer"
    		Bottles = 99
    		PRINT *, "Go To The Store And Buy Some More", Bottles, "Bottles Of Beer On The Wall"
    	END IF
    
    END PROGRAM

  8. #18
    Join Date
    Sep 2006
    Beans
    2,914

    Re: [Beginner] Programming Challenge: 1

    Code:
    wget -q -O - http://99-bottles-of-beer.net/lyrics.html | awk '/<p>/{gsub("<br>","\n");gsub(" *<[^>]+>","");print}'

  9. #19
    Join Date
    Jun 2008
    Beans
    204
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: [Beginner] Programming Challenge: 1

    wget -q -O - http://99-bottles-of-beer.net/lyrics.html | awk '/<p>/{gsub("<br>","\n");gsub(" *<[^>]+>","");print}'
    Lols - Very Good!

  10. #20
    Join Date
    Apr 2007
    Location
    NorCal
    Beans
    1,149
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: [Beginner] Programming Challenge: 1

    Quote Originally Posted by ghostdog74 View Post
    Code:
    wget -q -O - http://99-bottles-of-beer.net/lyrics.html | awk '/<p>/{gsub("<br>","\n");gsub(" *<[^>]+>","");print}'
    +1 for cleverness, whether you're eligible in the competition or not.
    Posting code? Use the [code] or [php] tags.
    I don't care, I'm still free. You can't take the sky from me.

Page 2 of 27 FirstFirst 123412 ... 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
  •