Silly 99 bottles

This is the place to post any code that you want to share with the community. Only completed scripts should be posted here.
Note: posts here are not necessarily endorsed by the community, and may represent amateur or even bad practices.

Silly 99 bottles

Looking at this thread, I suddenly remembered doing this task many-many years ago... and found the script!
The script is unfinished, sorry: the idea was to write 99 different functions for 99 verses, but I got tired after a few. Still, it's fully functional:-).
Besides, it has one peculiarity that might look interesting (at least to some of you I hope): it never mentions strings like "beer", "bottle", or "wall".
The 1st function -- f_99() -- is basically the only 'real hard work':-).

Anyhow, w/o further ado:
Code: Select all
`#!/usr/bin/env pythonimport mathimport sysdef int2bits_clean(n):    sz = 16 # sizeof_int(n)    a = []    for i in eval("range(16%s)"%",".join(map(str,3*[-1]))):        a.append((n >> i) & 0x0001)    return "".join(map(str,a))def get_obscure_numbers():    nnn = [0,0,0,0,0,0,0]    # Let's pick some even digits:    nnn[0] = int("0".join(map(str,[4,6,8])))    # I am almost out of ideas, and it's a good all-even number, so let's reuse it!     nnn[5] = nnn[0]    #  Let's move around a couple of digits:    l = list(str(nnn[0]))    l[1]=l[2];l[3],l[4]=l[4],l[3]    nnn[6] = int("".join(l))    # Show the power of Python in incrementing numbers:    nnn[1] = nnn[6]+1    # Show some math:    nnn[2] = math.floor(10000*math.atan(sum(nnn)) - (sum(map(int,list(str(sum(nnn))))) - len(nnn)/2 - 2*(nnn[0] + nnn[1] - nnn[5] - nnn[6])))    nnn[3] = (nnn[0] >> 2) + (2*2 + 2*1 + 2*2 + 2*1 + 2*2 + 2*1 + 2*2)**2 + 2**2 - 2**1    nnn[4] = nnn[0] - nnn[2] - (nnn[0]%1000 + int((nnn[2] + nnn[3] - nnn[0]%1000) / 1000) + 2./.3) * 3    nnn = map(int,nnn)    put_back = []    for n in nnn:        s = int2bits_clean(n)        for xx in [(x,x+1) for x in [1,0]]:            s = s.replace(*map(str, xx))        put_back.append(s)    return  "".join(put_back)def rr(s, N):    return s.replace("#", "%s"%N).replace("*","%s"%(N-1))def foo_99(N=99):    chars = eval(''.join([chr(154 - ord(c)) for c in '72(rijq']))+''.join([chr(154 - ord(c)) for c in '?zynm986542/.,+*(\'F&%#wp'])[1:]    ndx_obscure  = '22161419191281711491688161141311910812151212302216141919128171149168816201851181141381714211331155171715161420137141023161419191281711491688161141311910812151212200'    ll_obscure = get_obscure_numbers()    ndx = []    offset = 0    for ch in ll_obscure:        l = int(ch)        n = ndx_obscure[offset:offset+l]        ndx.append(int(n))        offset += l    r = ''    for x in ndx:        r += chars[x]    print >> open("99", "w"), r    mod = sys.modules[__name__]    setattr(mod,"Ninety-Nine", r)    r=r.replace("#", str(N))    r=r.replace("*", str(N-1))    print r    return N-1def foo_98(N=98):    # Use the side-effect of foo_99():    sys.stdout.write(open("%d"%(N+1)).read().replace("#", "%s"%N).replace("*","%s"%(N-1)))    return N-1def foo_97(N=97):    # Another side-effect of foo_99():    mod = sys.modules[__name__]    print rr(getattr(mod,"Ninety-Nine"), N)    return N-1def foo_96(N=96):    foo = "foo_%d"%(N+1)    return getattr(sys.modules[__name__], foo)(N)import urllibdef foo_95(N=95):    ll = urllib.urlopen("http://www.99-bottles-of-beer.net/lyrics.html").readlines()    for ln in ll:        if ln.startswith("<p>%d"%N):            ln = ln[ len("<p>"): -1-len("</p>") ]            a = ln.split("<br>")            for x in a:                pp = x.split(",")                for p in pp:                    print p.strip()    print "\n"    return N-1                    def foo_94(N=94):    mod = sys.modules[__name__]    nm = "foo_%d"%(N+5)    hhh = {N: [N]}    getattr(mod,nm)(*hhh[N])    return N-1if __name__ == "__main__":    # F67(F68(F69(F70(F71(F72(F73(F74(F75(F76(F77(F78(F79(F80(F81(F82(F83(F84(F85(F86(F87(F88(F89(F90(F91(F92(F93(F94(F95(F96(F97(F98(F99()))))))))))))))))))))))))))))))))    # F34(F35(F36(F37(F38(F39(F40(F41(F42(F43(F44(F45(F46(F47(F48(F49(F50(F51(F52(F53(F54(F55(F56(F57(F58(F59(F60(F61(F62(F63(F64(F65(F66()))))))))))))))))))))))))))))))))    # F0(F1(F2(F3(F4(F5(F6(F7(F8(F9(F10(F11(F12(F13(F14(F15(F16(F17(F18(F19(F20(F21(F22(F23(F24(F25(F26(F27(F28(F29(F30(F31(F32(F33())))))))))))))))))))))))))))))))))    foo_94(foo_95(foo_96(foo_97(foo_98(foo_99())))))`
ochichinyezaboombwa

Posts: 203
Joined: Tue Jun 04, 2013 7:53 pm

Re: Silly 99 bottles

AUGH OH GOD WHY
JUST DO IT LIKE THIS PLEASE MY SOUL IS IN PAIN
Code: Select all
`import timebeer = int(raw_input("How many bottles?\n> "))#-----def bottles(beer):   print beer, " bottles of beer on the wall, ", beer, " bottles of beer! Take one down, pass it around. "   beer -= 1   print beer, " bottles of beer on the wall!\n"   time.sleep(1)#- - -def bottle(beer):   print beer, " bottle of beer on the wall, ", beer, " bottle of beer! Take it down, pass it around. ",    beer -= 1   print beer, " bottles of beer on the wall!\n"   time.sleep(1)def em_nuht_draunk(beer):   print beer, " bottles of beer on the wall, ", beer, " bottles of beer. Go to the store and buy some more, 99 bottles of beer on the wall.\n"   time.sleep(1)   print "Sorry, guys."   time.sleep(4)   exit()#-----while beer >= 0:   if beer > 1:      bottles(beer)   elif beer == 1:      bottle(beer)   elif beer == 0:      em_nuht_draunk(beer)   beer -= 1`
Last edited by Mekire on Sat Nov 30, 2013 7:25 am, edited 1 time in total.
Reason: Locked.
Python 2.7
Mac Mini (Desktop/server)
Macbook Air
Laptop w/ Ubuntu 14.04
mckryall

Posts: 116
Joined: Sat Nov 23, 2013 6:13 pm

Re: Silly 99 bottles

mckryall wrote:JUST DO IT LIKE THIS PLEASE MY SOUL IS IN PAIN
Ochichinyezaboombwa's is intentionally ridiculous and obtuse and meant to be humorous. Yours on the other hand relies on modifying a global variable; something which, as previously discussed, is not acceptable.

-Mek
New Users, Read This
• Use code tags when posting code.
• Include any errors with your post (in code tags).
• Describe your problem; not your chosen solution.
• Make examples the minimum length to demonstrate your issue.

Mekire

Posts: 1711
Joined: Thu Feb 07, 2013 11:33 pm
Location: Tucson, Arizona

Re: Silly 99 bottles

mckryall wrote:AUGH OH GOD WHY
JUST DO IT LIKE THIS PLEASE MY SOUL IS IN PAIN
Code: Select all
`def bottles(beer):   print beer, " bottles of beer on the wall, ", beer, " bottles of beer! Take one down, pass it around. "`

-- As I pointed out above, there are no strings like "beer", "bottle", or "wall" in my solution. In fact, no strings at all. That's the whole point. Doesn't this amuse you maybe just a little bit?

Mekire wrote:Ochichinyezaboombwa's ... is meant to be humorous.
Well, thanks for the "is meant to" at least:-).
ochichinyezaboombwa

Posts: 203
Joined: Tue Jun 04, 2013 7:53 pm

Re: Silly 99 bottles

Well, there are quite a few strings in there.
And also bottles and beer:
Code: Select all
`    ll = urllib.urlopen("http://www.99-bottles-of-beer.net/lyrics.html").readlines()`
Friendship is magic!

R.I.P. Tracy M. You will be missed.

stranac

Posts: 1790
Joined: Thu Feb 07, 2013 3:42 pm

Re: Silly 99 bottles

C'mon, you know what I mean. "No strings" means no strings (words) from the verse; the url just happens to have "beer" in it but that fact is never used (e.g. I don't say
Code: Select all
`"http://www.99-bottles-of-beer.net/lyrics.html"[25:29]`
. Anyway, -- it's just a silly exercise and it was done many–many years ago.
I'm curious though: how would you approach this task if there's the limitation "no strings from the verse"?
ochichinyezaboombwa

Posts: 203
Joined: Tue Jun 04, 2013 7:53 pm

Re: Silly 99 bottles

ochichinyezaboombwa wrote:I'm curious though: how would you approach this task if there's the limitation "no strings from the verse"?

Here is the lame answer:
Code: Select all
`from __future__ import print_functionno_strings = [57, 57, 32, 98, 111, 116, 116, 108, 101, 115, 32, 111, 102, 32,              98, 101, 101, 114, 32, 111, 110, 32, 116, 104, 101, 32, 119, 97,              108, 108, 46]for char in no_strings:    print(chr(char),end="")`
Code: Select all
`99 bottles of beer on the wall.`

Although I admit it is boring.
-Mek
New Users, Read This
• Use code tags when posting code.
• Include any errors with your post (in code tags).
• Describe your problem; not your chosen solution.
• Make examples the minimum length to demonstrate your issue.

Mekire

Posts: 1711
Joined: Thu Feb 07, 2013 11:33 pm
Location: Tucson, Arizona

Re: Silly 99 bottles

Yes, a bit too obvious.... Note my usage of the ord() function...
ochichinyezaboombwa

Posts: 203
Joined: Tue Jun 04, 2013 7:53 pm

Re: Silly 99 bottles

I don't think python can ever compete with the classic PERL version. Of course perl has an advantage in that pretty much all perl code is somewhat obfuscated.

Code: Select all
`    ''=~(        '(?{'        .('`'        |'%')        .('['        ^'-')    .('`'        |'!')        .('`'        |',')        .'"'.        '\\\$'    .'=='        .('['        ^'+')        .('`'        |'/')        .('['    ^'+')        .'||'        .(';'        &'=')        .(';'        &'=')    .';-'        .'-'.        '\\\$'        .'=;'        .('['        ^'(')    .('['        ^'.')        .('`'        |'"')        .('!'        ^'+')   .'_\\{'      .'(\\\$'      .';=('.      '\\\$=|'      ."\|".(      '`'^'.'  ).(('`')|    '/').').'    .'\\"'.+(    '{'^'[').    ('`'|'"')    .('`'|'/' ).('['^'/')  .('['^'/').  ('`'|',').(  '`'|('%')).  '\\".\\"'.(  '['^('(')). '\\"'.('['^  '#').'!!--'  .'\\\$=.\\"'  .('{'^'[').  ('`'|'/').(  '`'|"\&").( '{'^"\[").(  '`'|"\"").(  '`'|"\%").(  '`'|"\%").(  '['^(')')).  '\\").\\"'. ('{'^'[').(  '`'|"\/").(  '`'|"\.").(  '{'^"\[").(  '['^"\/").(  '`'|"\(").( '`'|"\%").(  '{'^"\[").(  '['^"\,").(  '`'|"\!").(  '`'|"\,").(  '`'|(',')). '\\"\\}'.+(  '['^"\+").(  '['^"\)").(  '`'|"\)").(  '`'|"\.").(  '['^('/')). '+_,\\",'.(  '{'^('[')).  ('\\\$;!').(  '!'^"\+").(  '{'^"\/").(  '`'|"\!").( '`'|"\+").(  '`'|"\%").(  '{'^"\[").(  '`'|"\/").(  '`'|"\.").(  '`'|"\%").( '{'^"\[").(  '`'|"\\$").(  '`'|"\/").(  '['^"\,").(  '`'|('.')).  ','.(('{')^ '[').("\["^  '+').("\`"|  '!').("\["^  '(').("\["^  '(').("\{"^  '[').("\`"| ')').("\["^  '/').("\{"^  '[').("\`"|  '!').("\["^  ')').("\`"|  '/').("\["^ '.').("\`"|  '.').("\`"|  '\$')."\,".(  '!'^('+')).  '\\",_,\\"'  .'!'.("\!"^ '+').("\!"^  '+').'\\"'.  ('['^',').(  '`'|"\(").(  '`'|"\)").(  '`'|"\,").( '`'|('%')).  '++\\\$="})'  );\$:=('.')^  '~';\$~='@'|  '(';\$^=')'^  '[';\$/='`';`

Marbelous

Posts: 401
Joined: Fri May 31, 2013 8:12 pm

Re: Silly 99 bottles

Does that code actually work?
Due to the reasons discussed here we are moving to python-forum.net on October 1, 2016.

This forum will be closed. Please create an account at the new site to continue discussion.

IRC://irc.freenode.net/python-forum
Kebap

Posts: 689
Joined: Thu Apr 04, 2013 1:17 pm
Location: Germany, Europe

Re: Silly 99 bottles

Kebap wrote:Does that code actually work?

I tried it and it actually worked. Even though it looks like noise, the perl interpreter somehow managed to turn it into something meaningful.
That code seems to originate from https://metacpan.org/pod/Acme::EyeDrops#Bottles-of-Beer
Join the #python-forum IRC channel on irc.freenode.net!
Somelauw

Posts: 82
Joined: Tue Feb 12, 2013 8:30 pm

Re: Silly 99 bottles

Perl is famous with this; and there are more (to me) amazing things , e.g.:
language= Shakespeare.

Still... I think it could be challenging for some to write his/her own new version in Python; no strings like "bottle" or "wall" allowed.
ochichinyezaboombwa

Posts: 203
Joined: Tue Jun 04, 2013 7:53 pm