Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3b4ace1

Browse files
committed
add morse code snippet
1 parent d14538e commit 3b4ace1

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

‎snippets/python/morse_code.md‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Morse code conversion
2+
A small snippet to convert text into morse code.
3+
Created the core of it working on another project, but thought this could be useful for others too.
4+
5+
## Dictionary
6+
```
7+
CHARACTERS = {'A': '.- ', 'B': '-... ', 'C': '-.-. ',
8+
'D': '-.. ', 'E': '. ', 'F': '..-. ',
9+
'G': '--. ', 'H': '.... ', 'I': '.. ',
10+
'J': '.--- ', 'K': '-.- ', 'L': '.-.. ',
11+
'M': '-- ', 'N': '-. ', 'O': '--- ',
12+
'P': '.--. ', 'Q': '--.- ', 'R': '.-. ',
13+
'S': '... ', 'T': '- ', 'U': '..- ',
14+
'V': '...- ', 'W': '.-- ', 'X': '-..- ',
15+
'Y': '-.-- ', 'Z': '--.. ', ' ': ' ',
16+
'0': '----- ', '1': '.---- ', '2': '..--- ',
17+
'3': '...-- ', '4': '....- ', '5': '..... ',
18+
'6': '-.... ', '7': '--... ', '8': '---.. ',
19+
'9': '----. '}
20+
```
21+
22+
## Function
23+
```
24+
#Input should be string
25+
def encode(msg):
26+
m_msg=''
27+
for char in msg:
28+
m_msg += CHARACTERS[char.upper()]
29+
return m_msg
30+
```

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /