Python 3.xでCodeEvalのMorse Code

CodeEval


# coding=utf-8
import sys
MORSE2ALPHA = {
".-": "A",
"-.-.": "C",
"-...": "B",
".": "E",
"-..": "D",
"--.": "G",
"..-.": "F",
"..": "I",
"....": "H",
"-.-": "K",
".---": "J",
"--": "M",
".-..": "L",
"---": "O",
"-.": "N",
"--.-": "Q",
".--.": "P",
"...": "S",
".-.": "R",
"-": "T",
"..-": "U",
".--": "W",
"...-": "V",
"-.--": "Y",
"-..-": "X",
"--..": "Z",
".----": "1",
"-----": "0",
"...--": "3",
"..---": "2",
".....": "5",
"....-": "4",
"--...": "7",
"-....": "6",
"----.": "9",
"---..": "8",
}
def main():
with open(sys.argv[1], "r") as f:
for line in f:
for e in line.rstrip().split(" "):
if e == "":
sys.stdout.write(" ")
else:
sys.stdout.write(MORSE2ALPHA[e])
sys.stdout.write("\n")
if __name__ == "__main__":
main()
view raw main.py hosted with ❤ by GitHub


関連:Go言語でCodeEvalのMorse Code - brainstorm