draftply Download

Morse Code Converter

← All samples

A .dplybundle packs a document together with its libraries in one file. The libraries are collapsed below — click to expand.

morse_converter.dplyEditor
1---
2app_version: 0.1.0
3precision: 2
4form_title: Morse Code Converter
5form_description: Convert between text and Morse code. Pick a direction, type your input, read the result.
6form_inputs: Direction|direction[Text to Morse:1,Morse to Text:0], Input|input"
7form_outputs: Result|result
8---
9# Morse Code Converter
10// The library below is bundled with this document and imported here.
11// Form → Form view shows an input mask.
12import "morse_code.dplylib"
13
14# Your input
15input = "Hello World!"
16direction = 1 // 1 = text to morse, 0 = morse to text
17
18# Conversion
19// The chip options of a form field carry numbers, so the direction is one.
20result = to_morse(input) if direction == 1 else from_morse(input)
21
22"Direction (1 = to morse)": direction
23"Input": input
24"Result": result
25
26# Round trip: encode, then decode again
27"Encoded": to_morse("SOS")
28"Decoded again": from_morse(to_morse("SOS"))
29
30# The library's parts, usable on their own
31// Both tables must stay the same length — the n-th character has the n-th symbol.
32"Table size": len(morse_chars())
33"Symbols": len(morse_codes())
34"Split": split_spaces("..- .- ...")
35"Where is Q?": index_of(morse_chars(), "Q")
⬡ morse_code.dplyliblibrary
1---
2app_version: 0.1.0
3---
4# morse_code.dplylib — Morse encoding and decoding for radio amateurs.
5// Import with: import "morse_code.dplylib" (Pro: functions + loops)
6//
7// to_morse("SOS") -> "... --- ..."
8// from_morse("... --- ...") -> "SOS"
9//
10// Letters are separated by one space, words by " / ".
11//
12// How this works: two parallel tables. The n-th character of morse_chars()
13// has the n-th symbol of morse_codes(), so encoding and decoding are the
14// same lookup read in opposite directions — find the index in one table,
15// take that index from the other.
16//
17// Everything here rests on three string features: `s[i]` reads a character,
18// `==` compares text, and `for c in text:` walks a string. A library exports
19// functions and not variables, so the two tables are functions as well.
20
21# A split, since draftply has none
22// "..- .-" -> ["..-", ".-"]. Collect characters until a space ends the part;
23// the space appended to the input flushes the last one.
24def split_spaces(s):
25 out = list()
26 part = ""
27 for c in s + " ":
28 if c == " ":
29 if len(part) > 0:
30 out = append(out, part)
31 part = ""
32 else:
33 part = part + c
34 return out
35
36// Where does x sit in items? -1 if it is not in there at all. Works on a list
37// and on a string, because both answer to len() and [i].
38def index_of(items, x):
39 for i in 0..len(items)-1:
40 if items[i] == x: return i
41 return -1
42
43// draftply has no upper(), but ord()/char() bridge text and numbers:
44// a lower-case letter sits 32 code points above its upper-case twin.
45def upper(c):
46 k = ord(c)
47 return char(k - 32) if k >= 97 and k <= 122 else c
48
49# The alphabet
50// 53 characters, and below them their 53 symbols in the same order.
51def morse_chars():
52 s = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" // 26 letters
53 s = s + "0123456789" // 10 digits
54 s = s + ".,?'!()&:;=+-_$@" + "\"" // 17 punctuation marks
55 return s
56
57def morse_codes():
58 s = ".- -... -.-. -.. . ..-. --. .... .. .--- -.- .-.. --" // A-M
59 s = s + " -. --- .--. --.- .-. ... - ..- ...- .-- -..- -.-- --.." // N-Z
60 s = s + " ----- .---- ..--- ...-- ....- ..... -.... --... ---.. ----." // 0-9
61 s = s + " .-.-.- --..-- ..--.. .----. -.-.-- -.--. -.--.- .-..." // . , ? ' ! ( ) &
62 s = s + " ---... -.-.-. -...- .-.-. -....- ..--.- ...-..- .--.-. .-..-." // : ; = + - _ $ @ "
63 return split_spaces(s)
64
65# Text → morse
66def to_morse(text):
67 chars = morse_chars()
68 codes = morse_codes()
69 out = ""
70 for c in text:
71 i = index_of(chars, upper(c))
72 if c == " ":
73 sym = "/" // word gap; the separator below spaces it out
74 elif i >= 0:
75 sym = codes[i]
76 else:
77 sym = "" // unknown character: skipped
78 if len(sym) > 0:
79 out = out + (" " if len(out) > 0 else "") + sym
80 return out
81
82# Morse → text
83// Collect dots and dashes until something else ends the symbol; the space
84// appended to the input flushes the last one.
85def from_morse(code):
86 chars = morse_chars()
87 codes = morse_codes()
88 out = ""
89 sym = ""
90 for c in code + " ":
91 if c == "." or c == "-":
92 sym = sym + c
93 else:
94 if len(sym) > 0:
95 i = index_of(codes, sym)
96 out = out + (chars[i] if i >= 0 else "")
97 sym = ""
98 if c == "/":
99 out = out + " "
100 return out