Answer to Question #186478 in Python for Alex Lee Deeds

Question #186478

You have been given an encrypted copy of the Final exam study guide here, but how do you decrypt and read it???

Along with the encrypted copy, some mysterious person has also given you the following documents:

helloworld.txt -- Maybe this file decrypts to say "Hello world!". Hmmm.


1
Expert's answer
2021-04-28T23:28:27-0400
def pennycost(c): #using to calculate the pennyvalue
    if c >="a" and c <="z":
        return ord(c)-96
    elif c>="A" and c<="Z":
        return ord(c)-64

  
def decryption(inputfile,outputfile): #decryption function definition
    with open(inputfile) as f: #opening a file to read
        fo = open(outputfile,"w") #opening the file to write
        count = 0 #count is the number to skip
        while True: #continue reading one character per iteration
            c = f.read(1)
            if not c: #if end of line then break the loop
                break;
            if count > 0: #if the count is not zero continue skipping
                count = count -1;
                continue
            elif c.isalpha(): #if the char is an alphabet
                count = pennycost(c) #compute the penny cost
                fo.write(c) #write the alphabet to output file
            elif c.isdigit(): #similarly check if the char is digit
                count = int(c) #get its integer value
                fo.write(c) #write it to the output
            else:
                count = 6 #otherwise have to skip 6 chars
                fo.write(c)

    fo.close() #closing the outputfile

inputfile = input("Please enter the input file name: ")
outputfile = input("Plese enter the output file name(EXISTING FILE WILL BE OVER WRITTEN!): ")
decryption(inputfile,outputfile)

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS