Monday, September 30, 2024

Python Code to decode text file decode a hidden message based on the arrangement of these numbers into a "pyramid" structure

In this exercise, you will 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. Note that you can write your code using any language and IDE you want (Python is preferred if possible, but not mandatory). 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 In 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".

 

 

This is the solution:

 

# First, open text file, look at the max number, and then use that max number to create list of all the possible pyramid edges. Then loop through text file and each loop check if the words belong to the numbers at the end of the pyramid line, for each loop we will move to the next pyramid level, example: 1,3,6. If word is an edge of the level, then add it to the list. If word doesn't belong to number at end of each pyramid line, ignore those words. Last step we join the selected words and get the output message.
 
 
THIS IS THE PYTHON CODE


def decode(message_file):
    store_word = []     #empty list to store words
    max_num = 0
    pyramid_level = 0
    edges = []



    with open(message_file, "r") as myfile:
        max_num=len(myfile.readlines())
        for n in range(1,max_num):
            new_edge = n*(n+1)/2
            edges.append(int(new_edge))

   
    for i in range(len(edges)):
        with open(message_file, "r") as myfile_2:
            for line in myfile_2:
                number, word = line.strip().split()
               
                if int(number) == edges[pyramid_level]:
                    store_word.append(word)                  
                    pyramid_level=pyramid_level+1
                    continue
   
    decoded_text = " ".join(store_word[i] for i in range( len(store_word)))

    return decoded_text

def main():
    text_file = "coding_qual_input.txt"
    decoded_text = decode(text_file)
    print(decoded_text)

if __name__ == "__main__":
    main()
   


This is the decoded message:
design all skill whole check deal wish visit now moment offer planet people electric lot huge system card current man way our parent wait
 
This is the example input file:
 
193 land
284 sun
16 too
136 huge
26 dont
286 such
130 noun
202 student
184 brown
135 complete
118 play
29 cook
72 yard
233 clock
275 would
265 plain
5 excite
132 fire
28 wish
213 cool
272 child
163 past
212 colony
222 oil
7 dog
115 back
100 money
214 kind
64 open
107 finger
19 touch
109 are
241 dad
104 am
208 modern
108 meant
44 ocean
228 pitch
194 suit
58 town
179 east
204 over
35 group
250 good
137 kind
257 down
71 band
203 especially
113 organ
2 of
218 fire
197 out
247 area
280 touch
299 happen
126 sat
105 electric
198 wrote
67 buy
120 lot
252 stop
13 corn
201 where
264 check
34 live
150 best
86 hold
292 cause
235 grand
30 present
138 indicate
92 counter
87 we
183 like
36 visit
79 state
263 morning
227 true
209 are
234 ball
254 history
219 seat
62 rain
53 less
84 glass
178 tone
48 song
156 fair
226 element
25 speed
77 produce
223 quotient
46 sand
232 begin
83 moment
66 offer
267 probable
3 all
140 necessary
281 post
38 cent
225 happen
278 speech
161 object
283 silver
216 third
166 crease
261 wait
168 triangle
239 idea
240 clothe
169 young
139 discuss
243 field
89 company
96 capital
255 compare
99 chart
122 possible
174 written
162 remember
144 mile
256 cold
259 lady
274 felt
95 against
123 skin
124 prepare
42 he
294 card
196 organ
145 object
253 our
287 major
20 discuss
153 system
111 hole
75 above
266 they
236 produce
224 straight
285 level
164 though
290 modern
65 dry
262 bought
90 milk
127 make
165 show
23 middle
170 center
97 blood
159 speak
12 prove
51 select
4 power
172 come
68 brown
167 experiment
230 strong
101 hurry
24 touch
296 reach
22 case
119 beat
182 over
185 dry
40 hill
69 company
8 opposite
143 work
268 field
188 felt
187 prepare
45 now
82 his
269 stay
279 toward
245 observe
289 time
149 stop
295 possible
171 card
14 prepare
190 current
57 compare
116 neighbor
88 thus
112 include
103 copy
47 bit
133 stead
134 does
80 general
258 solve
271 glad
158 duck
229 offer
176 happen
177 ball
291 bread
244 like
117 machine
238 come
59 any
129 band
63 it
94 section
60 close
273 heavy
43 produce
237 got
102 possible
251 insect
231 way
50 before
18 men
211 bird
146 ease
220 trade
151 winter
277 am
157 repeat
114 first
242 to
154 each
297 guide
152 column
206 single
260 remember
155 wild
282 major
125 coast
175 class
11 done
160 jump
217 sister
248 feel
15 check
76 fire
17 nine
181 indicate
276 parent
10 whole
121 her
192 the
128 temperature
1 design
56 big
6 skill
186 friend
33 hit
300 wait
191 instant
288 blow
85 about
32 chick
199 answer
210 man
81 material
249 current
246 think
98 print
141 nor
142 better
73 example
61 people
41 drink
27 gun
110 together
49 cost
180 require
293 or
91 people
39 planet
54 ease
215 ready
74 enough
37 sugar
21 deal
52 with
131 us
270 share
93 office
106 protect
200 low
221 thus
173 farm
9 oxygen
207 fire
70 force
195 select
147 paragraph
298 always
205 poem
31 chick
78 planet
189 fact
55 moment
148 term