DEV Community

loading...

Discussion on: Daily Challenge #299 - Time to Grille!

Collapse
peter279k profile image
peter279k

Here is the simple solution with Python:

def grille(message, code):
    bin_number = bin(code)[2:]
    bin_number_len = len(bin_number)

    if len(message) <= 1:
        return ''
    if len(message) > bin_number_len:
        bin_number = ('0' * (len(message) - bin_number_len)) + bin_number

    index = 0
    result = ''
    while index < len(message):
        if bin_number[index] == '1':
            result += message[index]
        index += 1

    if len(message) < bin_number_len:
        if len(result) == len(message):
            return result
        else:
            return ''

    return result

Enter fullscreen mode Exit fullscreen mode