Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Goal: to make this function without importing any modules -- using Python Part 1

ID: 3714764 • Letter: G

Question

Goal: to make this function without importing any modules -- using Python

Part 1d: Rail Fence Ciphei https://en.wikipedia.org/wiki/Rail fence General Reference & Basic Idea History This is an ancient Greek cipher which implemented with a tool called a scytale s://en wikipedia.org/wiki/Scytale The only change from what's outlined in the article is that we're going to encode all letters in uppercase and all non-letters with the character"." (a period). So, encoding the message "We are discovered, flee at once!" with three rails will be Computer? Will looks like this: "WRIVDLANEAEDSOEE. FE.TOC.. .CR.E. E" The decoding of the above message will be: "WE.ARE.DISCOVERED. .FLEE.AT.ONCE." Note that the Wikipedia article gives the algorithm for both encode and decode (see the solution" section). However, instead of decoding by writing then Imagine writing then ?.EAL.YL.KE.UZZ.ES - - ? Functions rail_encode(num_rails, message): This function accepts num_rails (an int) and a message and returns the encoded message

Explanation / Answer

def get_char_for_message(c):
if c.isalpha():
return c.upper()
return "."
def rail_encode(num_rails, message):
down=True
raw_out=[]
out=''
i=0
for x in range(num_rails) :
raw_out.append({})
for pos in range(len(message)) :
raw_out[i][pos]=get_char_for_message(message[pos])
if i==num_rails-1 :
down=False
if i==0 :
down=True
if down :
i=i+1
else :
i=i-1
for p in raw_out :
for q in p :
out+=p[q]

return out

print(rail_encode(3, "We are discovered, flee at once!"))

Sample run

# copy pastable code link: https://paste.ee/p/K476o

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote