Skip to content

Commit 63510fc

Browse files
committed
Add the "roman-to-arabic" hole
Updates #3
1 parent fd23cac commit 63510fc

File tree

5 files changed

+47
-29
lines changed

5 files changed

+47
-29
lines changed

routes/hole.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99

1010
var preambles = map[string]string{
1111
"99-bottles-of-beer": `<h1>99 Bottles of Beer</h1><p>Print the lyrics to the song 99 Bottles of Beer.</p>`,
12-
"arabic-to-roman-numerals": `<h1>Arabic to Roman Numerals</h1><p>For each arabic number argument print the same number in roman numerals.</p>`,
12+
"arabic-to-roman": `<h1>Arabic to Roman</h1><p>For each arabic numeral argument print the same number in roman numerals.</p>`,
1313
"emirp-numbers": `<h1>Emirp Numbers</h1><p>An emirp (prime spelled backwards) is a prime number that results in a different prime when its decimal digits are reversed. For example both <b>13</b> and <b>31</b> are emirps.</p><p>Print all the emirp numbers from <b>1</b> to <b>1000</b> inclusive, each on their own line.</p>`,
1414
"evil-numbers": `<h1>Evil Numbers</h1><p>An evil number is a non-negative number that has an even number of 1s in its binary expansion.<p>Print all the evil numbers from <b>0</b> to <b>50</b> inclusive, each on their own line.<p>Numbers that are not evil are called <a href=odious-numbers>odious numbers</a>.</p>`,
1515
"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>`,
@@ -20,6 +20,7 @@ var preambles = map[string]string{
2020
"pernicious-numbers": `<h1>Pernicious Numbers</h1><p>A pernicious number is a positive number where the sum of its binary expansion is a <a href=prime-numbers>prime number</a>.<p>For example, <b>5</b> is a pernicious number since <b>5 = 101<sub>2</sub></b> and <b>1 + 1 = 2</b>, which is prime.<p>Print all the pernicious numbers from <b>0</b> to <b>50</b> inclusive, each on their own line.</p>`,
2121
"prime-numbers": `<h1>Prime Numbers</h1><p>Print all the prime numbers from <b>1</b> to <b>100</b> inclusive, each on their own line.</p>`,
2222
"quine": `<h1>Quine</h1><p>A <b>quine</b> is a non-empty computer program which takes no input and produces a copy of its own source code as its only output, produce such a program.<p>Trailing whitespace is <b>NOT</b> stripped from the output for this hole.</p>`,
23+
"roman-to-arabic": `<h1>Roman to Arabic</h1><p>For each roman numeral argument print the same number in arabic numerals.</p>`,
2324
"seven-segment": `<h1>Seven Segment</h1><p>Using pipes and underscores print the argument as if it were displayed on a seven segment display.<p>For example the number <b>0123456789</b> should be displayed as:<pre> _ _ _ _ _ _ _ _
2425
| | | _| _||_||_ |_ ||_||_|
2526
|_| ||_ _| | _||_| ||_| _|</pre>`,

routes/home.go

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -53,23 +53,24 @@ func home(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
5353
WHERE row_number < 6
5454
OR user_id = $1
5555
ORDER BY CASE hole
56-
WHEN 'emirp-numbers' THEN 0
57-
WHEN 'evil-numbers' THEN 1
58-
WHEN 'fibonacci' THEN 2
59-
WHEN 'fizz-buzz' THEN 3
60-
WHEN 'happy-numbers' THEN 4
61-
WHEN 'odious-numbers' THEN 5
62-
WHEN 'pascals-triangle' THEN 6
63-
WHEN 'pernicious-numbers' THEN 7
64-
WHEN 'prime-numbers' THEN 8
65-
WHEN 'quine' THEN 9
66-
WHEN '99-bottles-of-beer' THEN 10
67-
WHEN 'seven-segment' THEN 11
68-
WHEN 'sierpiński-triangle' THEN 12
69-
WHEN 'π' THEN 13
70-
WHEN 'e' THEN 14
71-
WHEN 'arabic-to-roman-numerals' THEN 15
72-
WHEN 'spelling-numbers' THEN 16
56+
WHEN 'emirp-numbers' THEN 0
57+
WHEN 'evil-numbers' THEN 1
58+
WHEN 'fibonacci' THEN 2
59+
WHEN 'fizz-buzz' THEN 3
60+
WHEN 'happy-numbers' THEN 4
61+
WHEN 'odious-numbers' THEN 5
62+
WHEN 'pascals-triangle' THEN 6
63+
WHEN 'pernicious-numbers' THEN 7
64+
WHEN 'prime-numbers' THEN 8
65+
WHEN 'quine' THEN 9
66+
WHEN '99-bottles-of-beer' THEN 10
67+
WHEN 'seven-segment' THEN 11
68+
WHEN 'sierpiński-triangle' THEN 12
69+
WHEN 'π' THEN 13
70+
WHEN 'e' THEN 14
71+
WHEN 'arabic-to-roman' THEN 15
72+
WHEN 'roman-to-arabic' THEN 16
73+
WHEN 'spelling-numbers' THEN 17
7374
END, row_number`,
7475
printHeader(w, r, 200),
7576
)
@@ -127,8 +128,8 @@ func home(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
127128
switch hole {
128129
case "99-bottles-of-beer":
129130
w.Write([]byte(`Medium><a href=99-bottles-of-beer>99 Bottles of Beer`))
130-
case "arabic-to-roman-numerals":
131-
w.Write([]byte(`Slow><a href=arabic-to-roman-numerals>Arabic to Roman`))
131+
case "arabic-to-roman":
132+
w.Write([]byte(`Slow><a href=arabic-to-roman>Arabic to Roman`))
132133
case "e":
133134
w.Write([]byte(`Medium><a href=e>e`))
134135
case "emirp-numbers":
@@ -151,6 +152,8 @@ func home(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
151152
w.Write([]byte(`Fast><a href=prime-numbers>Prime Numbers`))
152153
case "quine":
153154
w.Write([]byte(`Fast><a href=quine>Quine`))
155+
case "roman-to-arabic":
156+
w.Write([]byte(`Slow><a href=roman-to-arabic>Roman to Arabic`))
154157
case "seven-segment":
155158
w.Write([]byte(`Medium><a href=seven-segment>Seven Segment`))
156159
case "sierpiński-triangle":

routes/routes.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func init() {
3030

3131
// TODO Create this with a loop.
3232
Router.GET("/99-bottles-of-beer", middleware.Gzip(hole))
33-
Router.GET("/arabic-to-roman-numerals", middleware.Gzip(hole))
33+
Router.GET("/arabic-to-roman", middleware.Gzip(hole))
3434
Router.GET("/e", middleware.Gzip(hole))
3535
Router.GET("/emirp-numbers", middleware.Gzip(hole))
3636
Router.GET("/evil-numbers", middleware.Gzip(hole))
@@ -42,6 +42,7 @@ func init() {
4242
Router.GET("/pernicious-numbers", middleware.Gzip(hole))
4343
Router.GET("/prime-numbers", middleware.Gzip(hole))
4444
Router.GET("/quine", middleware.Gzip(hole))
45+
Router.GET("/roman-to-arabic", middleware.Gzip(hole))
4546
Router.GET("/seven-segment", middleware.Gzip(hole))
4647
Router.GET("/sierpiński-triangle", middleware.Gzip(hole))
4748
Router.GET("/spelling-numbers", middleware.Gzip(hole))

routes/scores.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func scores(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
1616
switch hole {
1717
case "all",
1818
"99-bottles-of-beer",
19-
"arabic-to-roman-numerals",
19+
"arabic-to-roman",
2020
"e",
2121
"emirp-numbers",
2222
"evil-numbers",
@@ -28,6 +28,7 @@ func scores(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
2828
"pernicious-numbers",
2929
"prime-numbers",
3030
"quine",
31+
"roman-to-arabic",
3132
"seven-segment",
3233
"sierpiński-triangle",
3334
"spelling-numbers",
@@ -55,7 +56,7 @@ func scores(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
5556
for _, v := range [][]string{
5657
{"all", "All Holes"},
5758
{"99-bottles-of-beer", "99 Bottles of Beer"},
58-
{"arabic-to-roman-numerals", "Arabic to Roman"},
59+
{"arabic-to-roman", "Arabic to Roman"},
5960
{"e", "e"},
6061
{"emirp-numbers", "Emirp Numbers"},
6162
{"evil-numbers", "Evil Numbers"},
@@ -67,6 +68,7 @@ func scores(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
6768
{"pernicious-numbers", "Pernicious Numbers"},
6869
{"prime-numbers", "Prime Numbers"},
6970
{"quine", "Quine"},
71+
{"roman-to-arabic", "Roman to Arabic"},
7072
{"sierpiński-triangle", "Sierpiński Triangle"},
7173
{"seven-segment", "Seven Segment"},
7274
{"spelling-numbers", "Spelling Numbers"},

routes/solution.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ func solution(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
3838
var args []string
3939
var out Out
4040

41-
if in.Hole == "arabic-to-roman-numerals" {
41+
switch in.Hole {
42+
case "arabic-to-roman":
4243
for i := 0; i < 20; i++ {
4344
i := rand.Intn(3998) + 1 // 1 - 3999 inclusive.
4445

@@ -48,14 +49,24 @@ func solution(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
4849

4950
// Drop the trailing newline.
5051
out.Exp = out.Exp[:len(out.Exp)-1]
51-
} else if in.Hole == "quine" {
52+
case "quine":
5253
out.Exp = in.Code
53-
} else if in.Hole == "seven-segment" {
54+
case "roman-to-arabic":
55+
for i := 0; i < 20; i++ {
56+
i := rand.Intn(3998) + 1 // 1 - 3999 inclusive.
57+
58+
out.Exp += strconv.Itoa(i) + "\n"
59+
args = append(args, arabicToRoman(i))
60+
}
61+
62+
// Drop the trailing newline.
63+
out.Exp = out.Exp[:len(out.Exp)-1]
64+
case "seven-segment":
5465
args = make([]string, 1)
5566
args[0], out.Exp = sevenSegment()
56-
} else if in.Hole == "spelling-numbers" {
67+
case "spelling-numbers":
5768
args, out.Exp = spellingNumbers()
58-
} else {
69+
default:
5970
out.Exp = answers[in.Hole]
6071
}
6172

@@ -192,7 +203,7 @@ func runCode(hole, lang, code string, args []string) (string, string) {
192203
errBytes = terminal.Render(errBytes)
193204

194205
// ASCII-ify roman numerals
195-
if hole == "arabic-to-roman-numerals" {
206+
if hole == "arabic-to-roman" {
196207
outBytes = bytes.Replace(outBytes, []byte("Ⅰ"), []byte("I"), -1)
197208
outBytes = bytes.Replace(outBytes, []byte("Ⅱ"), []byte("II"), -1)
198209
outBytes = bytes.Replace(outBytes, []byte("Ⅲ"), []byte("III"), -1)

0 commit comments

Comments
 (0)