"Christmas message" challenge (with optional music)

A quick challenge for those who will have some free time during festive period to do some Python with optional music.

Here is a list of tracks to listen to (if you wish so) while doing this exercise (mostly thanks to @Quercus):

tracklist = [
    'Frank Sinatra Have Yourself A Merry Little Christmas',
    'The Highway Song Aztec Two-Step',
    'Bob Dylan And Johnny Cash Girl From The North Country',
    'Jimi Hendrix All Along The Watchtower',
    'Fatboy Slim Sunset',
    'Groove Armada Chicago',
    'Marianne Faithfull As Tears Go By'
    'Chuck Berry Cest La Vie',
    'Laurie Anderson O Superman',
    'Flunk I Love Music'
]

Part 1. Find

Here is a data generator function which returns nested dict structure:

def get_data():
    import random
    import itertools as itl
    from string import ascii_lowercase
    vowels = 'aeiouy'
    constants = [l for l in ascii_lowercase if l not in vowels]
    constants = constants[:len(constants) // 4]
    vowels2 = [v * 2 for v in vowels[:-1]]
    syllables = list(map(''.join, itl.product(constants, vowels2)))
    n, depth = 6, 6
    nleafs = int(n * (n**depth - 1) / (n - 1))
    seeds = [1167, 308814, 42888, 29, 1251545, 362, 246170, 48341,
             110, 40669, 2129, 6, 81, 33492, 3327, 1896, 91]
    bin_size = nleafs / len(seeds)
    bin_size = int(-(-bin_size // 1))
    idxs = list()
    for s in seeds:
        random.seed(s)
        for _ in range(bin_size):
            idxs.append(int((random.random() - 0.05) * 311))
    idxs = zip(itl.count(), idxs)
    random.seed(61)
    stack, data = [], {}
    names = itl.batched(random.sample(syllables, n * 2), 2)
    stack.append((data, 1, names))
    while stack:
        data, d, it = stack[-1]
        for k1, k2 in it:
            data[k1] = next(idxs)
            if d < depth:
                data[k2] = branch = {}
                names = itl.batched(random.sample(syllables, n * 2), 2)
                stack.append((branch, d + 1, names))
                break
        else:
            stack.pop()
    return data

The leafs of a structure are length-2 tuples.
And all keys of the structure are of type str.

The exercise is to find all leafs, such that:

  1. A key of it is either string 'cii', 'cuu' or 'boo'
  2. A key of its parent is either string 'buu' or 'caa'
  3. A key of its grandparent is either string 'bee' or 'baa'

Write a function “solve(data)” to get a list of all leafs that satisfy condition above.

Note:

  • All code with imports must be within a function
  • 3rd party libraries are permitted

Part 2. Decode

  1. Sort leaves from part 1 by the 1st item and get a list of 2nd items. These are ordered indices that represent one letter.
  2. To get a lookup string, use tracklist from before: lookup = ''.join(tracklist).lower()
  3. Decode a sentence using results from (1) and (2).
  4. Add some punctuation to the result of (3):
    • sentence.replace(' ', '!').replace(' ', '. ')

Winners

3 winners:

  1. 1st one to decode the message.
  2. 1 fastest solution of “solve(data)”.
  3. 1 shortest solution of “solve(data)”. (All whitespaces will be eliminated)

Length of the solution will be calculated as follows:

func_string = """
def solve(data):
    <code code>
"""

import re
print(len(re.sub(r'\s', '', func_string)))

(1) does not have a deadline.
Submissions to (2) and (3) can be done until NY (2025-01-01T00:00ZEST).
One participant can submit unlimited number of solutions.

2 Likes

List of youtube links for the playlist above:

Frank Sinatra Have Yourself A Merry Little Christmas
The Highway Song Aztec Two-Step
Bob Dylan And Johnny Cash Girl From The North Country
Jimi Hendrix All Along The Watchtower
Fatboy Slim Sunset
Groove Armada Chicago
Marianne Faithfull As Tears Go By
Chuck Berry Cest La Vie
Laurie Anderson O Superman
Flunk I Love Music

1 Like

Decoded:

beautiful is better than ugly. merry christmas!

2 Likes

Can our solve function assume that data is the exact output of that given get_data function? If not, what can we assume about data?

For “shortest”, can you provide code to compute the length?

It needs to work on any dict nested structure. In other words, it needs to be able to get the result of “Part 1” without knowing the answer.

Included into original message.

But can we assume that all dict keys are consonant+vowel+vowel strings and that all dict values are either dicts or 2-tuples of ints?

Yes and yes.

Edit, you can assume that keys will be strings. No assumption on string structure.

Length 74
def solve(data):
    return eval(int('	 		 		 				     	      		  		  		 				 			  	   	      	 		     	      		 	  	 		 			  	 		 		 	 		 		  	 	    		  	   		    	 			 	   		    	  	 		    	 	 	   	  			  	 		 	  	 		 	  	 		 	  	  			  	 	  	 	 			 	 	 			 	 		  		  		 				 			  	   	      				     	 		   		    	  	 		   		   	   	 		   		   		  	      		 	  	 		 			   	      	 		     	      		 	  	 		  		   	 	    		   		  	      		 	  	 		 			  				 		  	  			 		   		 		 	  	 		 	  	  	  			  	 		    	  			 		   		 			 	 	 			 	 	  	  			  	 		    	  			 		   	  		 				 		 				  	  			 					 	 		    	 		 			  		  	    	      		   	   	      		 	  	 		 			   	      				 		  	  			 		   	  			 	 	 			 	 	  	  			  	 		    	  			 		   		 		    	 		    	  	  			 					 	 		    	 		 			  		  	    	      		    	  	      		 	  	 		 			  				 		  	  			 		   	  		  	 	 		  	 	  	  			  	 		    	  			 		   	  		    	 		    	  	  			 					 	 		 	  	 		  		   	      			 	   				  	 			     		  	 	  	 	    				     	 	  	  	    	  				 	 		  	   		 	  	 		   		 			 	    	      		  	 	 		 		   			  		 		  	 	  	      	 		     	 			  		  	 	 				    			 	   		  	 	 		 			  		  	    	 	     	 	    				    	 		 		 		 	 		 	 			 	  	 		   		   	   	 		   		   		  	 		   		 	 		  	 	  	 		  		  		 				 			  	   	      		 	 		  	      		 	  	 		 			   	      				     	 	  	  	 	  	 	 			 	'.translate({32:48,9:49}),2).to_bytes(170))

Attempt This Online!

2 Likes

Lovely! :slight_smile:

And of course, some of the tracks listed there are ones you chose as well. :smile:

Great work, @Stefan2! I won’t be able to beat that one.

While we’re here, if anyone wishes to share their favorite music with us by posting links to it, this would be a great opportunity to do so, whether or not it’s holiday music, and whether or not you are entering this code golf challenge. I happen to know that @dg-pb would welcome that. We would all love to hear your music. :smile:

I’ll begin by offering this one: Another Christmas Song - Jethro Tull

1 Like