2017-03-25 6 views
1

私は学校に問題があり、それを理解できないようです。基本的には、私はオブジェクト指向のプログラミングクラスの紹介に入っているので、私はまだ学んでいないことを何も使用せずに、できるだけ基本的なものとしてこれを完了する必要があります。現在、辞書とセットについて学ぶには、長い文字列を1行に含む文書を暗号化するためのコードが書かれた辞書を使用する必要があります。特定のキーを使用したファイルの暗号化と復号化

したがって、辞書を読み、その文字列を含むテキストファイルを開くには、1つの部分が必要です。

"The course Introduction to Object Oriented Programming uses the Python programming language."

私はそれを暗号化し、encrypt.txtと呼ばれる別のテキストファイルに文字列の暗号化されたバージョンを書くために、この辞書からコードを使用する必要があります。

CODE = {'A': ')', 'a': '0', 'B': '(', 'b': '9', 'C': '*', 'c': '8', 
    'D': '&', 'd': '7', 'E': '^', 'e': '6', 'F': '%', 'f': '5', 
    'G': '$', 'g': '4', 'H': '#', 'h': '3', 'I': '@', 'i': '2', 
    'J': '!', 'j': '1', 'K': 'Z', 'k': 'z', 'L': 'Y', 'l': 'y', 
    'M': 'X', 'm': 'x', 'N': 'W', 'n': 'w', 'O': 'V', 'o': 'v', 
    'P': 'U', 'p': 'u', 'Q': 'T', 'q': 't', 'R': 'S', 'r': 's', 
    'S': 'R', 's': 'r', 'T': 'Q', 't': 'q', 'U': 'P', 'u': 'p', 
    'V': 'O', 'v': 'o', 'W': 'N', 'w': 'n', 'X': 'M', 'x': 'm', 
    'Y': 'L', 'y': 'l', 'Z': 'K', 'z': 'k', '!': 'J', '1': 'j', 
    '@': 'I', '2': 'i', '#': 'H', '3': 'h', '$': 'G', '4': 'g', 
    '%': 'F', '5': 'f', '^': 'E', '6': 'e', '&': 'D', '7': 'd', 
    '*': 'C', '8': 'c', '(': 'B', '9': 'b', ')': 'A', '0': 'a', 
    ':': ',', ',': ':', '?': '.', '.': '?', '<': '>', '>': '<', 
    "'": '"', '"': "'", '+': '-', '-': '+', '=': ';', ';': '=', 
    '{': '[', '[': '{', '}': ']', ']': '}'} 

これはこれまでのコードです。どのような助けも大いにありがたくなり、素人の言葉での説明も大いに評価されます。

CODE = {'A': ')', 'a': '0', 'B': '(', 'b': '9', 'C': '*', 'c': '8', 
    'D': '&', 'd': '7', 'E': '^', 'e': '6', 'F': '%', 'f': '5', 
    'G': '$', 'g': '4', 'H': '#', 'h': '3', 'I': '@', 'i': '2', 
    'J': '!', 'j': '1', 'K': 'Z', 'k': 'z', 'L': 'Y', 'l': 'y', 
    'M': 'X', 'm': 'x', 'N': 'W', 'n': 'w', 'O': 'V', 'o': 'v', 
    'P': 'U', 'p': 'u', 'Q': 'T', 'q': 't', 'R': 'S', 'r': 's', 
    'S': 'R', 's': 'r', 'T': 'Q', 't': 'q', 'U': 'P', 'u': 'p', 
    'V': 'O', 'v': 'o', 'W': 'N', 'w': 'n', 'X': 'M', 'x': 'm', 
    'Y': 'L', 'y': 'l', 'Z': 'K', 'z': 'k', '!': 'J', '1': 'j', 
    '@': 'I', '2': 'i', '#': 'H', '3': 'h', '$': 'G', '4': 'g', 
    '%': 'F', '5': 'f', '^': 'E', '6': 'e', '&': 'D', '7': 'd', 
    '*': 'C', '8': 'c', '(': 'B', '9': 'b', ')': 'A', '0': 'a', 
    ':': ',', ',': ':', '?': '.', '.': '?', '<': '>', '>': '<', 
    "'": '"', '"': "'", '+': '-', '-': '+', '=': ';', ';': '=', 
    '{': '[', '[': '{', '}': ']', ']': '}'} 

def main(): 
    #Open the file you want to encrypt. 
    infile = str(input('Enter the name of the input file: ')) 
    #read its contents 
    dtext = open(infile, 'r') 
    #read the line from the file 
    dtext = dtext.readlines() 

    #strip the newline 
    #dtext = dtext.rstrip('\n') 

    #call the encryptText function 
    encryptText(dtext) 

def encryptText(dtext): 
    #enter the name of the file to write to 
    outfile = str(input('Enter the name of the output file: ')) 
    #open the file to send encrypted text to 
    etext = open(outfile, 'w') 
    #set accumulator value 
    count = 0 
    #create a for loop to read each separate character 
    for line in dtext: 
     wordList = line.split() 
     print(dtext, CODE[dtext]) 
    count += 1 

main() 
+0

何が問題になりましたか?私は 'TypeError:unhashable型: 'list''を得ました...それはあなたが見るものですか? – tdelaney

+0

ええ、私はその部分を分かったと思うけど、今は新しいエラーが出てきています... KeyError: '' – Classicalclown

+0

私の解決方法では、私は 'dict.get(c、c)'を使ってマップを取得しようとしましたそれ以外の場合はオリジナルを通します。 ASCIIテキストの場合、タブや改行などのエンコードは行いません。 – tdelaney

答えて

1

文字列は不変です。つまり、作成後には配列とは対照的に編集できません。テキストを暗号化するには、新しい文字列を作成する必要があります。また、一度に1文字ずつ行う必要があります。あなたは文字列としてdtext内のファイルのテキストを持っているので、あなたはそうのような、元の文字列の文字をループすることができます

for i in range (0, len(dtext)): 
    # add new character to string 

(私はあなただけをコピーして貼り付けることはできませんので、別にこれを壊しますよ)

暗号化されたテキストをforループの外側に配置するには、新しい文字列を作成する必要があります。彼らはあなたが定義した辞書にある場合は、forループという点で、暗号化された文字列に一度に文字を追加することができます置換を行うことで値を暗号化するために

enc = "" 

if (dtext[i] in CODE.keys()): 
    enc += CODE[dtext[i]] 

次に、新しい文字列をファイルに書き込んでください。

pythonでの辞書は事実上、キーによってインデックス可能な配列です。このキーは、配列インデックスがある値にマップするのと同じように、指定された値にマップされます。詳細については、https://www.tutorialspoint.com/python/python_dictionary.htmを参照してください。

+0

ああ、私はそれを理解すると思います!私は今それを得ることができるかどうか試してみる。私はこれを使ってどこかに着い始めましたが、あなたの方法はもっと理にかなっています。 for line in dtext: chの行: print(ch、CODE [ch]) – Classicalclown

1

文字単位で暗号化する必要があり、結果を取得して文字列に戻す必要があります。 str.joinは一連の文字を文字列に変換し、それぞれの文字を暗号化するためのジェネレータを作成することができます。

CODE = {'A': ')', 'a': '0', 'B': '(', 'b': '9', 'C': '*', 'c': '8', 
    'D': '&', 'd': '7', 'E': '^', 'e': '6', 'F': '%', 'f': '5', 
    'G': '$', 'g': '4', 'H': '#', 'h': '3', 'I': '@', 'i': '2', 
    'J': '!', 'j': '1', 'K': 'Z', 'k': 'z', 'L': 'Y', 'l': 'y', 
    'M': 'X', 'm': 'x', 'N': 'W', 'n': 'w', 'O': 'V', 'o': 'v', 
    'P': 'U', 'p': 'u', 'Q': 'T', 'q': 't', 'R': 'S', 'r': 's', 
    'S': 'R', 's': 'r', 'T': 'Q', 't': 'q', 'U': 'P', 'u': 'p', 
    'V': 'O', 'v': 'o', 'W': 'N', 'w': 'n', 'X': 'M', 'x': 'm', 
    'Y': 'L', 'y': 'l', 'Z': 'K', 'z': 'k', '!': 'J', '1': 'j', 
    '@': 'I', '2': 'i', '#': 'H', '3': 'h', '$': 'G', '4': 'g', 
    '%': 'F', '5': 'f', '^': 'E', '6': 'e', '&': 'D', '7': 'd', 
    '*': 'C', '8': 'c', '(': 'B', '9': 'b', ')': 'A', '0': 'a', 
    ':': ',', ',': ':', '?': '.', '.': '?', '<': '>', '>': '<', 
    "'": '"', '"': "'", '+': '-', '-': '+', '=': ';', ';': '=', 
    '{': '[', '[': '{', '}': ']', ']': '}'} 

def main(): 
    #Open the file you want to encrypt. 
    infile = str(input('Enter the name of the input file: ')) 
    #read its contents 
    dtext = open(infile, 'r') 
    #read the line from the file 
    dtext = dtext.readlines() 

    #strip the newline 
    #dtext = dtext.rstrip('\n') 

    #call the encryptText function 
    encryptText(dtext) 

def encryptText(dtext): 
    #enter the name of the file to write to 
    outfile = str(input('Enter the name of the output file: ')) 
    #open the file to send encrypted text to 
    etext = open(outfile, 'w') 
    #set accumulator value 

    #create a for loop to read each separate character 
    for line in dtext: 
     # encrypt character by character then join to a string 
     encrypted = ''.join(CODE.get(c, c) for c in line) 
     print(repr(line), repr(encrypted)) 
     etext.write(encrypted) 
    etext.close() 

main() 
+1

これはPython 3のように見えますが、これは劇的に単純化することができます。 'CODE = str.maketrans(CODE)'という行を追加して 'str.translate'の' dict'に変換した後、genexprと 'join'を' encrypted = line.translate (CODE) 'と呼びます。 *はるかに*速くなければならず、仕事のために最も単純なツールを使用しています。 – ShadowRanger

+1

@ShadowRanger - このコードはより洗練された方法と方法があることに同意しますが、初心者向けのプログラミングクラスのため、できるだけ邪魔したくありませんでした。私が初心者のクラスを教えていて、あなたの解決策を得たら、私はFを不正行為とみなしたいと思うでしょう! – tdelaney

+0

本当ですか?つまり、 'str'メソッドを見れば、' str.translate'は基本的に、与えられたフォームの 'dict'をどのように使うのかを記述しています(まあ、大丈夫、' maketrans'で修正する必要があります序数をキーとして使用しますが、十分に近づけてください)。実際には、初心者が盲目的に見ているのは、「適切な方法」をすでに知っていて、前にその特定の方法に遭遇したことのない、中程度の経験を積んだプログラマーです。 – ShadowRanger

0

tdelaneyの回答は正しいものの、より簡単なバージョンにしました。私は少しそれを変更しました。これは私がやったことです:

空の文字列に結合するのではなく、その部分を完全に削除しました。私はちょうど文字列全体の文字を繰り返して、それからtdelaneyのように、コード辞書の中でキーを使うためにgetメソッドを使いました。

CODE = {'A': ')', 'a': '0', 'B': '(', 'b': '9', 'C': '*', 'c': '8', 
    'D': '&', 'd': '7', 'E': '^', 'e': '6', 'F': '%', 'f': '5', 
    'G': '$', 'g': '4', 'H': '#', 'h': '3', 'I': '@', 'i': '2', 
    'J': '!', 'j': '1', 'K': 'Z', 'k': 'z', 'L': 'Y', 'l': 'y', 
    'M': 'X', 'm': 'x', 'N': 'W', 'n': 'w', 'O': 'V', 'o': 'v', 
    'P': 'U', 'p': 'u', 'Q': 'T', 'q': 't', 'R': 'S', 'r': 's', 
    'S': 'R', 's': 'r', 'T': 'Q', 't': 'q', 'U': 'P', 'u': 'p', 
    'V': 'O', 'v': 'o', 'W': 'N', 'w': 'n', 'X': 'M', 'x': 'm', 
    'Y': 'L', 'y': 'l', 'Z': 'K', 'z': 'k', '!': 'J', '1': 'j', 
    '@': 'I', '2': 'i', '#': 'H', '3': 'h', '$': 'G', '4': 'g', 
    '%': 'F', '5': 'f', '^': 'E', '6': 'e', '&': 'D', '7': 'd', 
    '*': 'C', '8': 'c', '(': 'B', '9': 'b', ')': 'A', '0': 'a', 
    ':': ',', ',': ':', '?': '.', '.': '?', '<': '>', '>': '<', 
    "'": '"', '"': "'", '+': '-', '-': '+', '=': ';', ';': '=', 
    '{': '[', '[': '{', '}': ']', ']': '}'} 

def main(): 
    #Open the file you want to encrypt. 
    infile = str(input('Enter the name of the input file: ')) 
    #read its contents 
    dtext = open(infile, 'r') 
    #read the line from the file 
    dtext = dtext.readlines() 

    #call the encryptText function 
    encryptText(dtext) 

def encryptText(dtext): 
    #enter the name of the file to write to 
    outfile = str(input('Enter the name of the output file: ')) 
    #open the file to send encrypted text to 
    etext = open(outfile, 'w') 
    #create a for loop to read each separate character 
    for line in dtext: 
     # encrypt character by character then join to a string 
     for c in line: 
      encrypted = (CODE.get(c, c)) 
      etext.write(encrypted) 
    #close the file 
    etext.close() 

main() 
+0

ちょうどあなたが知っている、あなた自身の質問に答えるのは慣習ではありません。私が最後にしたとき、私は大いに下降し、かなりの報酬を失った。私はそれについて何もするつもりはない、私はそれが理にかなっていると思う。私はあなたに警告すると思った。 – thedevelop3r

+0

本当に?申し訳ありませんが、私はこれとこのウェブサイトのすべてを初めて知っています。私はフィードバックを感謝します。 – Classicalclown

+0

@ thedevelop3rあなた自身の質問に答えるのはまったく問題ありません。あなたは質問された質問に対する完全な答えである必要があることを理解する必要があります。それには何らかのコードと何が間違っているかの説明が含まれています。また、他の人の回答をコピーすることもまた不可能です。 –

関連する問題