# "Christmas message" challenge (with optional music)

**URL:** <https://discuss.python.org/t/christmas-message-challenge-with-optional-music/75114>\
**Category:** Python Help\
**Created:** [December 23, 2024, 10:04pm UTC](https://discuss.python.org/t/christmas-message-challenge-with-optional-music/75114 "2024-12-23T22:04:39Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![dg-pb](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/dg-pb/32/16248_2.png) [@dg-pb](https://discuss.python.org/u/dg-pb)\
**Post date:** [December 23, 2024, 10:04pm UTC](https://discuss.python.org/t/christmas-message-challenge-with-optional-music/75114/1 "2024-12-23T22:04:39Z")

</div>

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):

```python
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:

```python
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:

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:

```python
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.

---

<div class="post-metadata">

**Author:** ![dg-pb](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/dg-pb/32/16248_2.png) [@dg-pb](https://discuss.python.org/u/dg-pb)\
**Post date:** [December 23, 2024, 10:05pm UTC](https://discuss.python.org/t/christmas-message-challenge-with-optional-music/75114/2 "2024-12-23T22:05:30Z")

</div>

List of youtube links for the playlist above:

[Frank Sinatra Have Yourself A Merry Little Christmas](https://www.youtube.com/watch?v=-tJtsKngXJU)  
[The Highway Song Aztec Two-Step](https://www.youtube.com/watch?v=4XzOV1lGZlU)  
[Bob Dylan And Johnny Cash Girl From The North Country](https://www.youtube.com/watch?v=Je4Eg77YSSA)  
[Jimi Hendrix All Along The Watchtower](https://www.youtube.com/watch?v=TLV4_xaYynY)  
[Fatboy Slim Sunset](https://www.youtube.com/watch?v=zKEWdRPRf3I)  
[Groove Armada Chicago](https://www.youtube.com/watch?v=qJ6-w7uQ-8w)  
[Marianne Faithfull As Tears Go By](https://www.youtube.com/watch?v=S8EykQaZ8CU)  
[Chuck Berry Cest La Vie](https://www.youtube.com/watch?v=g8MS58jZaUY)  
[Laurie Anderson O Superman](https://www.youtube.com/watch?v=S39NaDPNDtk&t=2s)  
[Flunk I Love Music](https://www.youtube.com/watch?v=UPNPTXKOhpw)

---

<div class="post-metadata">

**Author:** ![Stefan2](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/stefan2/32/18492_2.png) [@Stefan2](https://discuss.python.org/u/Stefan2)\
**Post date:** [December 23, 2024, 11:01pm UTC](https://discuss.python.org/t/christmas-message-challenge-with-optional-music/75114/3 "2024-12-23T23:01:27Z")

</div>

Decoded:

beautiful is better than ugly. merry christmas!

---

<div class="post-metadata">

**Author:** ![Stefan2](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/stefan2/32/18492_2.png) [@Stefan2](https://discuss.python.org/u/Stefan2)\
**Post date:** [December 23, 2024, 11:19pm UTC](https://discuss.python.org/t/christmas-message-challenge-with-optional-music/75114/4 "2024-12-23T23:19:38Z")

</div>

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`?

---

<div class="post-metadata">

**Author:** ![Stefan2](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/stefan2/32/18492_2.png) [@Stefan2](https://discuss.python.org/u/Stefan2)\
**Post date:** [December 23, 2024, 11:23pm UTC](https://discuss.python.org/t/christmas-message-challenge-with-optional-music/75114/5 "2024-12-23T23:23:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dg-pb](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/dg-pb/32/16248_2.png) [@dg-pb](https://discuss.python.org/u/dg-pb)\
**Post date:** [December 23, 2024, 11:29pm UTC](https://discuss.python.org/t/christmas-message-challenge-with-optional-music/75114/6 "2024-12-23T23:29:15Z")

</div>

> [@Stefan2](#):
>
> 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`?

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.

> [@Stefan2](#):
>
> For “shortest”, can you provide code to compute the length?

Included into original message.

---

<div class="post-metadata">

**Author:** ![Stefan2](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/stefan2/32/18492_2.png) [@Stefan2](https://discuss.python.org/u/Stefan2)\
**Post date:** [December 23, 2024, 11:33pm UTC](https://discuss.python.org/t/christmas-message-challenge-with-optional-music/75114/7 "2024-12-23T23:33:39Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![dg-pb](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/dg-pb/32/16248_2.png) [@dg-pb](https://discuss.python.org/u/dg-pb)\
**Post date:** [December 23, 2024, 11:34pm UTC](https://discuss.python.org/t/christmas-message-challenge-with-optional-music/75114/8 "2024-12-23T23:34:50Z")

</div>

Yes and yes.

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

---

<div class="post-metadata">

**Author:** ![Stefan2](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/stefan2/32/18492_2.png) [@Stefan2](https://discuss.python.org/u/Stefan2)\
**Post date:** [December 24, 2024, 7:59am UTC](https://discuss.python.org/t/christmas-message-challenge-with-optional-music/75114/9 "2024-12-24T07:59:06Z")

</div>

> **Length 74**
>
> ```python
> def solve(data):
> return eval(int(' '.translate({32:48,9:49}),2).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lFkeqmUuTwwzAsMTBwQTOcrLcwimBkbZySpQE1n0wEkgjEq1c2qg8xkkVD-Z-FyCldlOrE8ehNBIcH43E8jGCYjMfjCJLjCOLkKD4aHqFvlKBlOMKNhwHjw2Fs176-4tg4DkYjzEpik4usxjFmHg6PE3NLsEQ8PkbzcWxlmDNxnbN7ihQq5q_ByFvyCvdDTE_9oF9bUHnXCFvc1VJai9E-N9qXWJOaqx28gbEGVQkXfm3C0X9DA1cjnHSaNGUGJMtw25af_YIKzd6CEKU9GBwchSj8YbzH7Z5lgVnY1JwDQRiVdhvR5jSKq6XReKThCuPWMQs0i-DzF7vEZFOOjcGam5MCkxwA2WScBvVwRWBGIMFaSQvUNVB-qMUViSxkxfZ2xfBILAOb3m3kwkwmVi6d0361hk69NQKtEyMh013VTPZ0Hc_MKtM7HTSN17ouYQE_2HHu5jb5icmfY6epmfhKjPb1f4RpX12RbEVsHr7fl6p9zRUl69qK25R2-7Cgmcyq8ay-RZjWej0c_W1JPccDzsxl-Z2jOTmxHpdyXWTmhLOHRFC_8sJBeZIhbk6FpiKlrSibNmUzO-C4gGaBbL2wlykzxC5toGjGSUoDH_n5Efjf-WHbZkwD8N1r131P-gc)

---

<div class="post-metadata">

**Author:** ![dg-pb](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/dg-pb/32/16248_2.png) [@dg-pb](https://discuss.python.org/u/dg-pb)\
**Post date:** [December 24, 2024, 9:10am UTC](https://discuss.python.org/t/christmas-message-challenge-with-optional-music/75114/10 "2024-12-24T09:10:17Z")

</div>

Lovely! 🙂

---

<div class="post-metadata">

**Author:** ![Quercus](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/quercus/32/31483_2.png) [@Quercus](https://discuss.python.org/u/Quercus)\
**Post date:** [December 24, 2024, 1:16pm UTC](https://discuss.python.org/t/christmas-message-challenge-with-optional-music/75114/11 "2024-12-24T13:16:58Z")

</div>

> [@dg-pb](#):
>
> Here is a list of tracks to listen to (if you wish so) while doing this exercise (mostly thanks to @Quercus):

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

> [@Stefan2](#):
>
> Length 74 …

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. 😄

I’ll begin by offering this one: [Another Christmas Song - Jethro Tull](https://www.youtube.com/watch?v=-F2cZucio84)
