Skip to content

Commit 5790715

Browse files
committed
Add the "morse" holes
Updates #3
1 parent 11867f1 commit 5790715

File tree

6 files changed

+120
-18
lines changed

6 files changed

+120
-18
lines changed

assets/common.css

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,27 +30,27 @@ label {
3030
margin-bottom: 10px;
3131
}
3232

33-
li {
33+
ul li {
3434
display: inline;
3535
}
3636

37-
li::before {
37+
ul li::before {
3838
content: '■';
3939
font-size: 24px;
4040
line-height: 24px;
4141
margin: 0 5px;
4242
}
4343

44-
li:first-child::before {
44+
ul li:first-child::before {
4545
color: #3d8b3d;
4646
margin-left: 0;
4747
}
4848

49-
li:nth-child(2)::before {
49+
ul li:nth-child(2)::before {
5050
color: #df8a13;
5151
}
5252

53-
li:nth-child(3)::before {
53+
ul li:nth-child(3)::before {
5454
color: #b52b27;
5555
}
5656

@@ -105,7 +105,8 @@ nav img {
105105
}
106106

107107
pre,
108-
.CodeMirror {
108+
.CodeMirror,
109+
#hole table {
109110
font-family: 'SFMono-Regular', Consolas, 'Liberation Mono', Menlo, Courier, monospace;
110111
}
111112

routes/hole.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77
"github.com/julienschmidt/httprouter"
88
)
99

10+
const morseTable = "<ol><li>The length of a dot is one unit.<li>A dash is three units.<li>The space between parts of the same letter is one unit.<li>The space between letters is three units<li>The space between words is seven units</ol><table><tr><th>A<td>▄ ▄▄▄<tr><th>B<td>▄▄▄ ▄ ▄ ▄<tr><th>C<td>▄▄▄ ▄ ▄▄▄ ▄<tr><th>D<td>▄▄▄ ▄ ▄<tr><th>E<td>▄<tr><th>F<td>▄ ▄ ▄▄▄ ▄<tr><th>G<td>▄▄▄ ▄▄▄ ▄<tr><th>H<td>▄ ▄ ▄ ▄<tr><th>I<td>▄ ▄<tr><th>J<td>▄ ▄▄▄ ▄▄▄ ▄▄▄<tr><th>K<td>▄▄▄ ▄ ▄▄▄<tr><th>L<td>▄ ▄▄▄ ▄ ▄<tr><th>M<td>▄▄▄ ▄▄▄<tr><th>N<td>▄▄▄ ▄<tr><th>O<td>▄▄▄ ▄▄▄ ▄▄▄<tr><th>P<td>▄ ▄▄▄ ▄▄▄ ▄<tr><th>Q<td>▄▄▄ ▄▄▄ ▄ ▄▄▄<tr><th>R<td>▄ ▄▄▄ ▄<tr><th>S<td>▄ ▄ ▄<tr><th>T<td>▄▄▄<tr><th>Y<td>▄ ▄ ▄▄▄<tr><th>V<td>▄ ▄ ▄ ▄▄▄<tr><th>W<td>▄ ▄▄▄ ▄▄▄<tr><th>X<td>▄▄▄ ▄ ▄ ▄▄▄<tr><th>Y<td>▄▄▄ ▄ ▄▄▄ ▄▄▄<tr><th>Z<td>▄▄▄ ▄▄▄ ▄ ▄<tr><th>0<td>▄▄▄ ▄▄▄ ▄▄▄ ▄▄▄ ▄▄▄<tr><th>1<td>▄ ▄▄▄ ▄▄▄ ▄▄▄ ▄▄▄<tr><th>2<td>▄ ▄ ▄▄▄ ▄▄▄ ▄▄▄<tr><th>3<td>▄ ▄ ▄ ▄▄▄ ▄▄▄<tr><th>4<td>▄ ▄ ▄ ▄ ▄▄▄<tr><th>5<td>▄ ▄ ▄ ▄ ▄<tr><th>6<td>▄▄▄ ▄ ▄ ▄ ▄<tr><th>7<td>▄▄▄ ▄▄▄ ▄ ▄ ▄<tr><th>8<td>▄▄▄ ▄▄▄ ▄▄▄ ▄ ▄<tr><th>9<td>▄▄▄ ▄▄▄ ▄▄▄ ▄▄▄ ▄</table>"
11+
1012
var preambles = map[string]string{
1113
"12-days-of-christmas": `<h1>12 Days of Christmas</h1><p>Print the lyrics to the song <b>The 12 Days of Christmas</b>:</p><blockquote>On the First day of Christmas
1214
My true love sent to me
@@ -50,6 +52,8 @@ A Partridge in a Pear Tree.</blockquote>`,
5052
"fibonacci": `<h1>Fibonacci</h1><p>Print the first <b>31</b> Fibonacci numbers from <b>F<sub>0</sub> = 0</b> to <b>F<sub>30</sub> = 832040</b> (inclusive), each on a separate line.</p>`,
5153
"fizz-buzz": `<h1>Fizz Buzz</h1><p>Print the numbers from <b>1</b> to <b>100</b> inclusive, each on their own line.</p><p>If, however, the number is a multiple of <b>three</b> then print <b>Fizz</b> instead, and if the number is a multiple of <b>five</b> then print <b>Buzz</b>.</p><p>For numbers which are multiples of <b>both three and five</b> then print <b>FizzBuzz</b>.</p>`,
5254
"happy-numbers": `<h1>Happy Numbers</h1><p>A happy number is defined by the following sequence: Starting with any positive integer, replace the number by the sum of the squares of its digits in base-ten, and repeat the process until the number either equals 1 (where it will stay), or it loops endlessly in a cycle that does not include 1. Those numbers for which this process ends in 1 are happy numbers, while those that do not end in 1 are sad numbers.<p>For example, 19 is happy, as the associated sequence is:</p><dl><dd>1<sup>2</sup> + 9<sup>2</sup> = 82<dd>8<sup>2</sup> + 2<sup>2</sup> = 68<dd>6<sup>2</sup> + 8<sup>2</sup> = 100<dd>1<sup>2</sup> + 0<sup>2</sup> + 0<sup>2</sup> = 1.</dl><p>Print all the happy numbers from <b>1</b> to <b>200</b> inclusive, each on their own line.</p>`,
55+
"morse-decoder": `<h1>Morse Decoder</h1><p>Using ▄ (U+2584 Lower Half Block) to represent a dot, encode the argument into Internation Morse Code.` + morseTable,
56+
"morse-encoder": `<h1>Morse Encoder</h1><p>Using ▄ (U+2584 Lower Half Block) to represent a dot, decode the argument from Internation Morse Code.` + morseTable,
5357
"odious-numbers": `<h1>Odious Numbers</h1><p>An odious number is a non-negative number that has an odd number of 1s in its binary expansion.<p>Print all the odious numbers from <b>0</b> to <b>50</b> inclusive, each on their own line.<p>Numbers that are not odious are called <a href=evil-numbers>evil numbers</a>.</p>`,
5458
"pangram-grep": `<h1>Pangram Grep</h1><p>A pangram is a sentence that uses every letter of a given alphabet.<p>Write a program that will receive various sentences as arguments and print those that are valid pangrams.</p>`,
5559
"pascals-triangle": `<h1>Pascal's Triangle</h1><p>Print the first <b>20 rows</b> of Pascal's triangle.</p>`,
@@ -95,7 +99,7 @@ func hole(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
9599
"<h2>Standard Error</h2><pre id=Err></pre>" +
96100
"<h2>Expected Output</h2><pre id=Exp></pre>" +
97101
"<h2>Standard Output</h2><pre id=Out></pre>" +
98-
"</div></div><main",
102+
"</div></div><main id=hole",
99103
))
100104

101105
if userID == 0 {

routes/home.go

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -66,17 +66,19 @@ func home(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
6666
WHEN '12-days-of-christmas' THEN 11
6767
WHEN '99-bottles-of-beer' THEN 12
6868
WHEN 'christmas-trees' THEN 13
69-
WHEN 'pangram-grep' THEN 14
70-
WHEN 'seven-segment' THEN 15
71-
WHEN 'sierpiński-triangle' THEN 16
72-
WHEN 'π' THEN 17
73-
WHEN 'φ' THEN 18
74-
WHEN '𝑒' THEN 19
75-
WHEN 'τ' THEN 20
76-
WHEN 'arabic-to-roman' THEN 21
77-
WHEN 'brainfuck' THEN 22
78-
WHEN 'roman-to-arabic' THEN 23
79-
WHEN 'spelling-numbers' THEN 24
69+
WHEN 'morse-decoder' THEN 14
70+
WHEN 'morse-encoder' THEN 15
71+
WHEN 'pangram-grep' THEN 16
72+
WHEN 'seven-segment' THEN 17
73+
WHEN 'sierpiński-triangle' THEN 18
74+
WHEN 'π' THEN 19
75+
WHEN 'φ' THEN 20
76+
WHEN '𝑒' THEN 21
77+
WHEN 'τ' THEN 22
78+
WHEN 'arabic-to-roman' THEN 23
79+
WHEN 'brainfuck' THEN 24
80+
WHEN 'roman-to-arabic' THEN 25
81+
WHEN 'spelling-numbers' THEN 26
8082
END, row_number`,
8183
printHeader(w, r, 200),
8284
)
@@ -142,6 +144,10 @@ func home(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
142144
w.Write([]byte(`Slow><a href=brainfuck>Brainfuck`))
143145
case "christmas-trees":
144146
w.Write([]byte(`Medium><a href=christmas-trees>Christmas Trees`))
147+
case "morse-decoder":
148+
w.Write([]byte(`Medium><a href=morse-decoder>Morse Decoder`))
149+
case "morse-encoder":
150+
w.Write([]byte(`Medium><a href=morse-encoder>Morse Encoder`))
145151
case "divisors":
146152
w.Write([]byte(`Fast><a href=divisors>Divisors`))
147153
case "emirp-numbers":

routes/morse.go

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package routes
2+
3+
// The 123 quick brown foxes jump over the 456 lazy dogs
4+
import "math/rand"
5+
6+
var morseMap = map[rune]string{
7+
'A': "▄ ▄▄▄",
8+
'B': "▄▄▄ ▄ ▄ ▄",
9+
'C': "▄▄▄ ▄ ▄▄▄ ▄",
10+
'D': "▄▄▄ ▄ ▄",
11+
'E': "▄",
12+
'F': "▄ ▄ ▄▄▄ ▄",
13+
'G': "▄▄▄ ▄▄▄ ▄",
14+
'H': "▄ ▄ ▄ ▄",
15+
'I': "▄ ▄",
16+
'J': "▄ ▄▄▄ ▄▄▄ ▄▄▄",
17+
'K': "▄▄▄ ▄ ▄▄▄",
18+
'L': "▄ ▄▄▄ ▄ ▄",
19+
'M': "▄▄▄ ▄▄▄",
20+
'N': "▄▄▄ ▄",
21+
'O': "▄▄▄ ▄▄▄ ▄▄▄",
22+
'P': "▄ ▄▄▄ ▄▄▄ ▄",
23+
'Q': "▄▄▄ ▄▄▄ ▄ ▄▄▄",
24+
'R': "▄ ▄▄▄ ▄",
25+
'S': "▄ ▄ ▄",
26+
'T': "▄▄▄",
27+
'U': "▄ ▄ ▄▄▄",
28+
'V': "▄ ▄ ▄ ▄▄▄",
29+
'W': "▄ ▄▄▄ ▄▄▄",
30+
'X': "▄▄▄ ▄ ▄ ▄▄▄",
31+
'Y': "▄▄▄ ▄ ▄▄▄ ▄▄▄",
32+
'Z': "▄▄▄ ▄▄▄ ▄ ▄",
33+
'1': "▄ ▄▄▄ ▄▄▄ ▄▄▄ ▄▄▄",
34+
'2': "▄ ▄ ▄▄▄ ▄▄▄ ▄▄▄",
35+
'3': "▄ ▄ ▄ ▄▄▄ ▄▄▄",
36+
'4': "▄ ▄ ▄ ▄ ▄▄▄",
37+
'5': "▄ ▄ ▄ ▄ ▄",
38+
'6': "▄▄▄ ▄ ▄ ▄ ▄",
39+
'7': "▄▄▄ ▄▄▄ ▄ ▄ ▄",
40+
'8': "▄▄▄ ▄▄▄ ▄▄▄ ▄ ▄",
41+
'9': "▄▄▄ ▄▄▄ ▄▄▄ ▄▄▄ ▄",
42+
'0': "▄▄▄ ▄▄▄ ▄▄▄ ▄▄▄ ▄▄▄",
43+
' ': " ",
44+
}
45+
46+
func morse(reverse bool) (args []string, out string) {
47+
digits := []byte{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}
48+
49+
// Shuffle the digits.
50+
for i := range digits {
51+
j := rand.Intn(i + 1)
52+
digits[i], digits[j] = digits[j], digits[i]
53+
}
54+
55+
// It would look weird if the numbers started with zero.
56+
if digits[0] == '0' || digits[5] == '0' {
57+
digits[0], digits[1] = digits[1], digits[0]
58+
digits[5], digits[6] = digits[6], digits[5]
59+
}
60+
61+
args = []string{"THE "}
62+
63+
for _, digit := range digits[:5] {
64+
args[0] += string(digit)
65+
}
66+
67+
args[0] += " BROWN FOXES JUMP OVER THE "
68+
69+
for _, digit := range digits[5:] {
70+
args[0] += string(digit)
71+
}
72+
73+
args[0] += " LAZY DOGS"
74+
75+
for _, char := range args[0] {
76+
out += morseMap[char] + " "
77+
}
78+
79+
// Knock off the trailing three spaces.
80+
out = out[:len(out)-3]
81+
82+
if reverse {
83+
args[0], out = out, args[0]
84+
}
85+
86+
return
87+
}

routes/scores.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ var holes = [][]string{
2222
{"fibonacci", "Fibonacci"},
2323
{"fizz-buzz", "Fizz Buzz"},
2424
{"happy-numbers", "Happy Numbers"},
25+
{"morse-decoder", "Morse Decoder"},
26+
{"morse-encoder", "Morse Encoder"},
2527
{"odious-numbers", "Odious Numbers"},
2628
{"pangram-grep", "Pangram Grep"},
2729
{"pascals-triangle", "Pascal's Triangle"},

routes/solution.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ func solution(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
4343
args, out.Exp = arabicToRoman(false)
4444
case "brainfuck":
4545
args, out.Exp = brainfuck()
46+
case "morse-decoder", "morse-encoder":
47+
args, out.Exp = morse(in.Hole == "morse-decoder")
4648
case "pangram-grep":
4749
args, out.Exp = pangramGrep()
4850
case "quine":

0 commit comments

Comments
 (0)