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 tuple
s.
And all keys of the structure are of type str
.
The exercise is to find all leafs, such that:
- A key of it is either string
'cii'
,'cuu'
or'boo'
- A key of its parent is either string
'buu'
or'caa'
- 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
- Sort leaves from part 1 by the 1st item and get a list of 2nd items. These are ordered indices that represent one letter.
- To get a lookup string, use
tracklist
from before:lookup = ''.join(tracklist).lower()
- Decode a sentence using results from (1) and (2).
- Add some punctuation to the result of (3):
sentence.replace(' ', '!').replace(' ', '. ')
Winners
3 winners:
- 1st one to decode the message.
- 1 fastest solution of “
solve(data)
”. - 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.