The exercise is as follows:
develop a function named decode(message_file) . This function should read an encoded message from a .txt file and return its decoded version as a string.Your function must be able to process an input file with the following format:
3 love 6 computers 2 dogs 4 cats 1 I 5 you
this file, each line contains a number followed by a word. The task is to decode a hidden message based on the arrangement of these numbers into a “pyramid” structure. The numbers are placed into the pyramid in ascending order, with each line of the pyramid having one more number than the line above it. The smallest number is 1, and the numbers increase consecutively, like so:
1
2 3
4 5 6
The key to decoding the message is to use the words corresponding to the numbers at the end of each pyramid line (in this example, 1, 3, and 6). You should ignore all the other words. So for the example input file above, the message words are:
1: I 3: love 6: computers
and your function should return the string “I love computers”.
I can’t make the input file work. Can anyone help me? Thanks!!
read the input file and break it into words and group the words in pairs (the number and the “real” word)
consult the number pyramid to see if the number from each pair is on the right hand end
collect those for which this is true
The number pyramid is probably the fiddliest thing. I’d start with a function shaped like is_right_hand_value(n) where you pass a number from the source text as n and it returns true/false according to the location of the number in the pyramid.
Hint: you aren’t actually supposed to build the pyramid. You’re just supposed to recognize the mathematical rule that tells you which numbers are important, and then fetch the corresponding words from the input.
Hint: you should use a data structure that lets you associate a given number with the corresponding word. For example, a dict.
But before you get that far, you should assess your existing skill set, and determine where you are stuck. For example, do you understand how to open and read the file, in the first place? Do you understand how to break a string apart into words? If you have a “word” from the string that is just text for a number, do you understand how to get the actual number from that?
Thank you for your response! I think i took the instructions too literally. I makes more sense the way you explained the mathematical rule. I also do need to learn more about reading files. I guess there are no shortcuts to learning, eh? Thanks again!!
Thank you for your reply! I totally misunderstood the concept of the pyramid structure. I’m also still learning, and tried to jump ahead. I don’t fully understand reading files. I am still learning, and probably shouldn’t try to jump ahead haha. Thanks again for your help!!