## 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

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. ## Re: [Beginner] Programming Challenge: 1

PHP Code:
``` #include <stdio.h>#include <stdlib.h>char* itoa(int n){char* _n = malloc(3); sprintf(_n, "%i", n); return _n;};int main(){    int i;    for(i = 99; i >= 0; i--)    {        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. A Carafe of Ubuntu
Join Date
Mar 2007
Beans
125

## Re: [Beginner] Programming Challenge: 1

First program using Python.

PHP Code:
``` #!/usr/bin/pythondef 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. First Cup of Ubuntu
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. ## 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 lyricsa = "bottles of beer on the wall, "b = "bottles of beer."c = "Take one down and pass it around," d = "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 bottlea1 = "bottle of beer on the wall, "b1 = "bottle of beer."e = "Take one down and pass it around, no more bottles of beer on the wall. \n"f = "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 ###########################################n = 99while n >= 3:    n = n-1    print n, a, n, b, "\n" , c, n-1, d, "\n\n"if n == 2:    print n-1, a1, n-1, b1, "\n" , e    print f  ```
Last edited by aktiwers; August 1st, 2008 at 10:01 AM. Reason: typo

6. ## 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,"     i = i - 1     if i > 1 :         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.

7. ## 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. I Ubuntu, Therefore, I Am
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. ## 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. ## Re: [Beginner] Programming Challenge: 1

Originally Posted by ghostdog74
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 Permissions

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