Created
March 22, 2024 18:17
-
-
Save anuraagdjain/6ae7bac2a343bd4a20e8ac270d72ec46 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Give an file as input which has following data | |
3 love | |
6 computers | |
2 dogs | |
4 cats | |
1 I | |
5 you | |
Arranging them into a pyramid and picking. the right most numbers 1,3,6 get the values of them from the given input file. | |
1 | |
2 3 | |
4 5 6 | |
The expected out here is "I love computers" | |
""" | |
def decode_message(file) -> str: | |
number_map = dict() | |
# prepare a map for number: str | |
with open(file) as f: | |
for line in f.readlines(): | |
row = line.split(' ') | |
number_map[row[0]] = ' '.join(row[1:]).replace('\n','') | |
result = [] | |
for i in range(1,len(number_map.items())+1): | |
pyramid_last_value = str(int((i*i+i)/2)) | |
if pyramid_last_value in number_map: | |
result.append(number_map[pyramid_last_value]) | |
else: | |
# We don't have that many numbers to scan | |
break | |
return ' '.join(result) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment