-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmorse_challenge.py
75 lines (57 loc) · 2.13 KB
/
morse_challenge.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import winsound
from time import sleep
def str_to_morse(word):
"""Converts a string into a morse string"""
# File that contains mapping between morse code and characters
mapping_path = "resources/morse_mapping.txt"
# Creates a dictionary that maps morse code and characters
with open(mapping_path, 'r') as infile:
mapping = dict()
for line in infile:
string, morse = line[:-1].split('\t')
mapping.update({string: morse})
# Creates a list of words within input string
word_list = list(word.upper().strip())
output_list = []
for char in word_list:
if char.isspace():
output_list.pop()
output_list.append('/')
elif char not in mapping:
return None
else:
output_list.append(mapping[str(char)])
output_list.append(" ")
return str(''.join(output_list))
def morse_audio(morse_str):
"""Translates a morse string into audio"""
# Define frequency (frq) and duration of each beep (base_dur)
frq = 500
base_dur = 100
# short mark, dot or dit: "dit duration" is one time unit long
# long mark, dash or dah: three time units long
# inter-element gap between the dits and dahs within a character: one dot duration or one unit long
# short gap (between letters): three time units long
# medium gap (between words): seven time units long
for char in morse_str:
if char == '.':
winsound.Beep(frq, base_dur)
elif char == '-':
winsound.Beep(frq, base_dur * 3)
elif char == "/":
sleep(base_dur / 1000 * 7)
continue
elif char.isspace():
sleep(base_dur / 1000 * 3)
continue
sleep(base_dur / 1000)
def test():
assert str_to_morse("SOS") == "... --- ... "
assert str_to_morse("testing 123") == "- . ... - .. -. --./.---- ..--- ...-- "
assert str_to_morse("SOS?!") == "... --- ... ..--.. -.-.-- "
assert str_to_morse("パイソンは好きです") == None
def main():
test()
morse_audio(str_to_morse("SOS"))
if __name__ == "__main__":
main()