Pythonのモールス信号翻訳者
モールス信号変換器は暗号化で使用されます。サミュエル・F・B・モールスによって名付けられました。この手法により、メッセージを一連のドット、コンマ、 "-"、"/"に変換します。
このテクニックはとても簡単です。英語のすべてのアルファベットは、一連の「。」、「、」、「/」、「-」を意味します。メッセージをメッセージから記号に暗号化し、記号から英語に復号化するだけです。
辞書は以下のとおりです
'A':'.-', 'B':'-...',
'C':'-.-.', 'D':'-..', 'E':'.',
'F':'..-.', 'G':'--.', 'H':'....',
'I':'..', 'J':'.---', 'K':'-.-',
'L':'.-..', 'M':'--', 'N':'-.',
'O':'---', 'P':'.--.', 'Q':'--.-',
'R':'.-.', 'S':'...', 'T':'-',
'U':'..-', 'V':'...-', 'W':'.--',
'X':'-..-', 'Y':'-.--', 'Z':'--..',
'1':'.----', '2':'..---', '3':'...--',
'4':'....-', '5':'.....', '6':'-....',
'7':'--...', '8':'---..', '9':'----.',
'0':'-----', ', ':'--..--', '.':'.-.-.-',
'?':'..--..', '/':'-..-.', '-':'-....-',
'(':'-.--.', ')':'-.--.-'}
例
Message is PYTHON-PROGRAM Output is .--. -.-- - .... --- -. -....- .--. .-. --- --. .-. .- --
アルゴリズム
暗号化
Step1: Given a string, atfirst we extract each letter from the word and match with the Morse Code dictionary, then we consider the code corresponding the letter. Step2: Next step is to store the code into a variable. And we have to follow that one space should be maintained between every Morse code. Step3: Two spaces should be maintained in between every word.
復号化
Step1: First we add a space at the end of the string. Step2: Now we traverse each letter of the message until space is not encountered. Step3: When we get space then check with Morse Code Dictionary and store in a variable. Step4: When get 2 consecutive spaces we will add another space to our variable containing the decoded string. Step5: When get last space of the message that means this is the last letter of Morse Code Generator.
サンプルコード
# -*- coding: utf-8 -*-
"""
Created on Tue Oct 2 11:21:31 2018
@author: Satyajit
"""
# Dictionary representing the morse code chart
MORSE_CODE_DICT = { 'A':'.-', 'B':'-...',
'C':'-.-.', 'D':'-..', 'E':'.',
'F':'..-.', 'G':'--.', 'H':'....',
'I':'..', 'J':'.---', 'K':'-.-',
'L':'.-..', 'M':'--', 'N':'-.',
'O':'---', 'P':'.--.', 'Q':'--.-',
'R':'.-.', 'S':'...', 'T':'-',
'U':'..-', 'V':'...-', 'W':'.--',
'X':'-..-', 'Y':'-.--', 'Z':'--..',
'1':'.----', '2':'..---', '3':'...--',
'4':'....-', '5':'.....', '6':'-....',
'7':'--...', '8':'---..', '9':'----.',
'0':'-----', ', ':'--..--', '.':'.-.-.-',
'?':'..--..', '/':'-..-.', '-':'-....-',
'(':'-.--.', ')':'-.--.-'
}
def encryption(message):
my_cipher = ''
for myletter in message:
if myletter != ' ':
my_cipher += MORSE_CODE_DICT[myletter] + ' '
else:
my_cipher += ' '
return my_cipher
# This function is used to decrypt
# Morse code to English
def decryption(message):
message += ' '
decipher = ''
mycitext = ''
for myletter in message:
# checks for space
if (myletter != ' '):
i = 0
mycitext += myletter
else:
i += 1
if i == 2 :
decipher += ' '
else:
decipher += list(MORSE_CODE_DICT.keys())[list(MORSE_CODE_DICT
.values()).index(mycitext)]
mycitext = ''
return decipher
def main():
my_message = "PYTHON-PROGRAM"
output = encryption(my_message.upper())
print (output)
my_message = ".--. -.-- - .... --- -. -....- .--. .-. --- --. .-. .- -- "
output = decryption(my_message)
print (output)
# Executes the main function
if __name__ == '__main__':
main()
出力
.--. -.-- - .... --- -. -....- .--. .-. --- --. .-. .- -- PYTHON-PROGRAM
-
Pythonコードの最適化のヒント?
Pythonは他の準拠言語ほど高速でも効率的でもないことは誰もが知っていますが、ただし、Pythonコードがはるかに大きなワークロードを処理できることを示している大企業はたくさんあり、それほど遅くはないことを示しています。このセクションでは、正しいPythonプログラムをさらに高速かつ効率的に実行するために、覚えておくべきヒントをいくつか紹介します。 ヒント1:組み込み関数を使用する Pythonで効率的なコードを書くことはできますが、組み込み関数(Cで書かれている)に勝るものはありません。下の画像は、Python組み込み関数のリストを示しています ヒント2:Pythonの複数の代入を
-
修正:インデントエラーPython
Pythonは、1991年に最初にリリースされた新しいプログラミング言語です。この言語は、その大規模な包括的なライブラリで知られており、機能、必須、手続き型、オブジェクト指向などのいくつかのプログラミングパラダイムをサポートしています。 「インデントエラー:インデントされたブロックが必要です ’はあらゆる種類のユーザーに発生します。彼らが初心者であろうと経験者であろうと。 Pythonはすべてのコードを正しい空白で配置するため、インデントが不適切な場合、コードはコンパイルされず、エラーメッセージが返されます。 PEP8で従う規則によれば、必要に応じて4つの空白が必要です。すべてのプログラ