#Digital

Python Challenge: How short can you write the python-code needed to generate the entire lyrics of “Twelve days of Christmas”?

About Python

The Python programming language offers an intuitive syntax and good OO features. The collection of standard and 3rd party modules is comprehensive and growing. It is available on most platforms, and it is extendable and embedable. It is usable from soft RT applications to complex data analysis and Processing.

The lyrics: On the first day of Christmas my true love gave to me

On the first day of Christmas my true love gave to me
a partridge in a pear tree

On the second day of Christmas my true love gave to me
two turtle doves
and a partridge in a pear tree

On the third day of Christmas my true love gave to me
three French hens
two turtle doves
and a partridge in a pear tree

On the fourth day of Christmas my true love gave to me
four calling birds
three French hens
two turtle doves
and a partridge in a pear tree

On the fifth day of Christmas my true love gave to me
five gold rings
four calling birds
three French hens
two turtle doves
and a partridge in a pear tree

On the sixth day of Christmas my true love gave to me
six geese a-laying
five gold rings
four calling birds
three French hens
two turtle doves
and a partridge in a pear tree

On the seventh day of Christmas my true love gave to me
seven swans a-swimming
six geese a-laying
five gold rings
four calling birds
three French hens
two turtle doves
and a partridge in a pear tree

On the eighth day of Christmas my true love gave to me
eight maids a-milking
seven swans a-swimming
six geese a-laying
five gold rings
four calling birds
three French hens
two turtle doves
and a partridge in a pear tree

On the ninth day of Christmas my true love gave to me
nine ladies dancing
eight maids a-milking
seven swans a-swimming
six geese a-laying
five gold rings
four calling birds
three French hens
two turtle doves
and a partridge in a pear tree

On the tenth day of Christmas my true love gave to me
ten lords a-leaping
nine ladies dancing
eight maids a-milking
seven swans a-swimming
six geese a-laying
five gold rings
four calling birds
three French hens
two turtle doves
and a partridge in a pear tree

On the eleventh day of Christmas my true love gave to me
eleven pipers piping
ten lords a-leaping
nine ladies dancing
eight maids a-milking
seven swans a-swimming
six geese a-laying
five gold rings
four calling birds
three French hens
two turtle doves
and a partridge in a pear tree

On the twelfth day of Christmas my true love gave to me
twelve drummers drumming
eleven pipers piping
ten lords a-leaping
nine ladies dancing
eight maids a-milking
seven swans a-swimming
six geese a-laying
five gold rings
four calling birds
three French hens
two turtle doves
and a partridge in a pear tree

 

22 comments on “Python Challenge: How short can you write the python-code needed to generate the entire lyrics of “Twelve days of Christmas”?

  1. I made a solution, i dont know if this is allowed but here it is:

    import bs4, requests; soup=bs4.BeautifulSoup(requests.get(“https://blog.sintef.com/digital-en/how-many-python-lines-do-you-need-to-generate-the-entire-lyrics-of-twelve-days-of-christmas/”).text, ‘lxml’)
    for t in soup.find(“div”,{‘id’:’entry-content’}).findAll(“p”)[5:]:print(str(t).replace(“”, “”).replace(“”, “\n”).replace(“”, “”))

  2. If you are allowed to read from an external source, why not assume a local copy of the lyrics in a file called a?
    print(open(‘a’).read())

  3. I have a feeling this is not a winner,but it is possible to do it in 480 characters, without depending on external sources:

    import zlib,base64
    exec(zlib.decompress(base64.b64decode(b’eJxFUbtuwzAM/BVCU4w6BtIxa4Gu/YA0A2tRFls9DEqJGxT991Jxki6kdWc+7uiyAAMnEEwTbXbP3X4WTnVj3hJUT/DzCxYvkB28eOFSIxaIF6hyIgj5TDChhpoh0nsyg8sSUasdS6l9oTEn21fPYnuXT1J979hpLPzdIp0paSaevKbE7VFXKNy4ulDQCjOUObB27k134GP3ZMzwmTltDqb9oTtYOcVIUtYPTpPu05u1D8w8N0rTndAxKkBsAdwGwgeuS6gytEzaCdP4aNR2hIh8rYgcvu7MVQWUBVNjysL/01UmTESF2hC83GHHzbgcLKjXU1kxtQdGDEER+FDDVrh6IYJXoTR68JRu6JKhqp1BZesRFMRkQQmEGaUK24naVfVJKHotatc5Hna7Le+P0HV/Zu2xDw==’)))

    • import zlib,base64
      exec(zlib.decompress(base64.b64decode(b’eJxFUbtuwzAM/BVCU4w6BtIxa4Gu/YA0A2tRFls9DEqJGxT9
      91Jxki6kdWc+7uiyAAMnEEwTbXbP3X4WTnVj3hJUT/DzCxYvkB28eOFSIxaIF6hyIgj5TDChhpoh0nsyg8s
      SUasdS6l9oTEn21fPYnuXT1J979hpLPzdIp0paSaevKbE7VFXKNy4ulDQCjOUObB27k134GP3ZMzwmTlt
      Dqb9oTtYOcVIUtYPTpPu05u1D8w8N0rTndAxKkBsAdwGwgeuS6gytEzaCdP4aNR2hIh8rYgcvu7MVQW
      UBVNjysL/01UmTESF2hC83GHHzbgcLKjXU1kxtQdGDEER+FDDVrh6IYJXoTR68JRu6JKhqp1BZesRFM
      RkQQmEGaUK24naVfVJKHotatc5Hna7Le+P0HV/Zu2xDw==’)))

      • Does the entire lines survive? Or do you miss out on something? I will repsot once again with shorter lines:

        import zlib,base64
        exec(zlib.decompress(base64.b64decode(b’eJxFUbtuwzAM/BVCU4w6BtIxa4Gu/
        YA0A2tRFls9DEqJGxT991Jxki6kdWc+7uiyAAMnEEwTbXbP3X4WTnVj3hJUT/Dz
        CxYvkB28eOFSIxaIF6hyIgj5TDChhpoh0nsyg8sSUasdS6l9oTEn21fPYnuXT1J97
        9hpLPzdIp0paSaevKbE7VFXKNy4ulDQCjOUObB27k134GP3ZMzwmTltDqb9oT
        tYOcVIUtYPTpPu05u1D8w8N0rTndAxKkBsAdwGwgeuS6gytEzaCdP4aNR2hIh8
        rYgcvu7MVQWUBVNjysL/01UmTESF2hC83GHHzbgcLKjXU1kxtQdGDEER+FD
        DVrh6IYJXoTR68JRu6JKhqp1BZesRFMRkQQmEGaUK24naVfVJKHotatc5Hna7
        Le+P0HV/Zu2xDw==’)))

        Remember: Thereshould be no line breaks in the exec string

    • The system muddles my single quotes. Put in a normal single quote in b’eJxFUbt and Dw==’))) and it will run.

  4. Kristoffer Nyborg Gregertsen

    Code should be readable and maintainable:

    #! /usr/bin/python3

    def xmas():
    lyrics = [(1, ‘a partridge in a pear tree’,’first’),
    (2, ‘two turtle doves’,’second’),
    (3, ‘three French hens’,’third’),
    (4, ‘four calling birds’,’fourth’),
    (5, ‘five gold rings’,’fifth’),
    (6, ‘six geese a-laying’,’sixth’),
    (7, ‘seven swans a-swimming’,’seventh’),
    (8, ‘eight maids a-milking’,’eight’),
    (9, ‘nine ladies dancing’,’ninth’),
    (10, ‘ten lords a-leaping’,’tenth’),
    (11, ‘eleven pipers piping’,’eleventh’),
    (12, ‘twelve drummers drumming’,’twelfth’)]

    for (i, what, day) in lyrics:
    print(“On the {} of Christmas my true love gave to me”.format(day))

    for j in reversed(range(0, i)):
    if i > 1 and j == 0:
    print(‘and ‘, end=”)
    print(lyrics[j][1])

    print()

    if __name__ == ‘__main__’:
    xmas()

    Still some room for improvement. Wait for my Ada example…

    • Kristoffer Nyborg Gregertsen

      Formatting was messed up…

    • Kristoffer Nyborg Gregertsen

      #! /usr/bin/python3

      def day(i):
      o = [‘first’, ‘second’, ‘third’, ‘fourth’,
      ‘fifth’, ‘sixth’, ‘seventh’, ‘eight’,
      ‘ninth’, ‘tenth’, ‘eleventh’, ‘twelfth’]
      return “On the {} day of Christmas my true love gave to me”.format(o[i])

      def what(i):
      w = [‘a partridge in a pear tree’,
      ‘two turtle doves’,
      ‘three French hens’,
      ‘four calling birds’,
      ‘five gold rings’,
      ‘six geese a-laying’,
      ‘seven swans a-swimming’,
      ‘eight maids a-milking’,
      ‘nine ladies dancing’,
      ‘ten lords a-leaping’,
      ‘eleven pipers piping’,
      ‘twelve drummers drumming’]
      return w[i]

      def xmas():
      for i in range(0, 12):
      print(day(i))
      for j in reversed(range(0, i+1)):
      if i > 0 and j == 0:
      print(‘and ‘ + what(j))
      else:
      print(what(j))
      print()

      if __name__ == ‘__main__’:
      xmas()

      • Kjetil Johannessen

        +1 for readability. That is some SINTEF-quality over that piece of code!

  5. import zlib,base64
    exec(zlib.decompress(base64.b64decode(b’eJxFUbtuwzAM/BVCU4w6BtIxa4Gu/YA0A2tRFls9DEqJGxT991Jxki6kdWc+7uiyAAMnEEwTbXbP3X4WTnVj3hJUT/DzCxYvkB28eOFSIxaIF6hyIgj5TDChhpoh0nsyg8sSUasdS6l9oTEn21fPYnuXT1J979hpLPzdIp0paSaevKbE7VFXKNy4ulDQCjOUObB27k134GP3ZMzwmTltDqb9oTtYOcVIUtYPTpPu05u1D8w8N0rTndAxKkBsAdwGwgeuS6gytEzaCdP4aNR2hIh8rYgcvu7MVQWUBVNjysL/01UmTESF2hC83GHHzbgcLKjXU1kxtQdGDEER+FDDVrh6IYJXoTR68JRu6JKhqp1BZesRFMRkQQmEGaUK24naVfVJKHotatc5Hna7Le+P0HV/Zu2xDw==’)))

  6. The system messed up the single quotes, replae them with normal single quotes, and the code will run. e(b’eJ and ==’))
    If you prefer the uncompressed version:

    for i in range(12):print(“On the {} day of Christmas my true love gave to me\n”.format(“first,second,third,fourth,fifth,sixth,seventh,eighth,ninth,tenth,eleventh,twelfth”.split(“,”)[i])+””.join([“twelve drummers drumming\n”,”eleven pipers piping\n”,”ten lords a-leaping\n”,”nine ladies dancing\n”,”eight maids a-milking\n”,”seven swans a-swimming\n”,”six geese a-laying\n”,”five gold rings\n”,”four calling birds\n”,”three French hens\n”,”two turtle doves\nand “,”a partridge in a pear tree\n”][11-i:]))

    Everything in one line, to avoid wasting characters on line breaks. (Yes, I know that line breakes are normally not a waste, but in this setting they are.)

    • Kjetil Johannessen

      This is impressively dense for being uncompressed. 503 characters vs 480 on your compressed version. Think you can squeeze this down below 480?

  7. If I am alowed the inflect module, that was not part of the default Python instalation, but it easily downloaded using pip install inflect I get 476

    import inflect as e
    p=e.engine()
    for i in range(12):print(“On the {} day of Christmas my true love gave to me\n”.format(p.number_to_words(p.ordinal(i+1)))+””.join([“twelve drummers drumming\n”,”eleven pipers piping\n”,”ten lords a-leaping\n”,”nine ladies dancing\n”,”eight maids a-milking\n”,”seven swans a-swimming\n”,”six geese a-laying\n”,”five gold rings\n”,”four calling birds\n”,”three French hens\n”,”two turtle doves\nand “,”a partridge in a pear tree\n”][11-i:]))

    If I am alowed to write 1st instead of first, I do not need to call p.number_to_words() and get down to 457
    If I compress that again, I get 427. (But this is with 1st, 2nd etc.)

    import zlib,base64
    exec(zlib.decompress(base64.b64decode(b’eJxFkc9OwzAMxu97CqunVmOVynESJySuPMDYwbRuakicyElXJsS7425sXPLn98mf4y8cUtQCLKOnvgBmoE16opbEsVDdbMaowKaDojiqu8dmn5Sl1NWrQJkIvn9gwDPEEZ4n5VyCeYQzFJ0JfDwROLSlRAj0JlVrfgFLndqoAwv6mrdd02yrqv2ILPWhKgt5Kxh0DoE0Xw8szoofKvJ0IoHEaZVsuwnFqDfLDLjzhHcuNgV4HJjMCaW/G7GbCgTkS0Vg/3lT8qVDXlBWJS/83z3zFziiTGsTPN/wyOuU0Q9gwbh8ZXFW6NF7I/DO9rDrMyclghcl6SeYSP7oEqHMWryNbYkZRBnABISEWpQHR+sX2JVQLVpaozweum7H+2PT/AITT5yx’)))

Leave a Reply

Your email address will not be published. Required fields are marked *