0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【Python】辞書(dict)から値をモールス信号で検索してみる

Last updated at Posted at 2019-04-12

辞書(dict)オブジェクトにモールス信号リストを作成し、
入力に対応した文字列を検索して出力するものを作ってみた。

# モールス符号 「.」がトン、「-」がツー
dic = {
    '.-':'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'
}

def morse(src):
    result = []
    for word in src.split("  "):
        for char in word.split(" "):
            result.append(dic[char])
        result.append(" ")
    return "".join(result)


print(morse('.... . .-.. .-.. ---  .--. -.-- - .... --- -.'))
実行結果
HELLO PYTHON 

空文字2文字で単語を区切り、空文字1文字で文字を区切りながら辞書を検索していき文字を連結させる。

0
2
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?