Skip to content

Commit b1d91b8

Browse files
committed
Add Arabic to Roman Numerals
1 parent 14d73fc commit b1d91b8

File tree

3 files changed

+41
-12
lines changed

3 files changed

+41
-12
lines changed

arabic-to-roman.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package main
2+
3+
var roman0 = []string{"", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"}
4+
var roman1 = []string{"", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"}
5+
var roman2 = []string{"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"}
6+
7+
func arabicToRoman(n int) string {
8+
return roman2[n%1000/100] + roman1[n%100/10] + roman0[n%10]
9+
}

code-golf.go

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"encoding/base64"
1010
"html/template"
1111
"io"
12+
"math/rand"
1213
"net/http"
1314
"os"
1415
"os/exec"
@@ -125,13 +126,12 @@ func codeGolf(w http.ResponseWriter, r *http.Request) {
125126
hole = path
126127
}
127128

128-
switch hole {
129-
case "99-bottles-of-beer", "fizz-buzz", "pascals-triangle", "π":
129+
if preamble, ok := preambles[hole]; ok {
130130
switch lang {
131131
case "javascript", "perl", "perl6", "php", "python", "ruby":
132132
vars := map[string]interface{}{
133133
"lang": lang,
134-
"preamble": template.HTML(preambles[hole]),
134+
"preamble": template.HTML(preamble),
135135
"r": r,
136136
}
137137

@@ -142,8 +142,24 @@ func codeGolf(w http.ResponseWriter, r *http.Request) {
142142
code := strings.Replace(r.FormValue("code"), "\r", "", -1)
143143
vars["code"] = code
144144

145-
answer := answers[hole]
146-
output := runCode(lang, code)
145+
var answer string
146+
var args []string
147+
148+
if hole == "arabic-to-roman-numerals" {
149+
for i := 0; i < 20; i++ {
150+
i := rand.Intn(3998) + 1 // 1 - 3999 inclusive.
151+
152+
answer += arabicToRoman(i) + "\n"
153+
args = append(args, strconv.Itoa(i))
154+
}
155+
156+
// Drop the trailing newline.
157+
answer = answer[:len(answer)-1]
158+
} else {
159+
answer = answers[hole]
160+
}
161+
162+
output := runCode(lang, code, args)
147163

148164
if answer == output {
149165
vars["pass"] = true
@@ -177,7 +193,7 @@ func codeGolf(w http.ResponseWriter, r *http.Request) {
177193
default:
178194
http.Redirect(w, r, "/"+hole+"/perl6", 302)
179195
}
180-
default:
196+
} else {
181197
w.WriteHeader(http.StatusNotFound)
182198
}
183199
}
@@ -216,7 +232,7 @@ func render(w http.ResponseWriter, tmpl *template.Template, vars map[string]inte
216232
writer.Close()
217233
}
218234

219-
func runCode(lang, code string) string {
235+
func runCode(lang, code string, args []string) string {
220236
var out bytes.Buffer
221237

222238
if lang == "php" {
@@ -237,7 +253,7 @@ func runCode(lang, code string) string {
237253
// $binary, @new_argv where $new_argv[0] is used for hostname too.
238254
switch lang {
239255
case "javascript":
240-
cmd.Args = []string{"/usr/bin/js", "javascript", "-f", "-"}
256+
cmd.Args = []string{"/usr/bin/js", "javascript", "-f"}
241257
case "perl6":
242258
cmd.Args = []string{
243259
"/usr/bin/moar",
@@ -254,6 +270,8 @@ func runCode(lang, code string) string {
254270
cmd.Args = []string{"/usr/bin/" + lang, lang}
255271
}
256272

273+
cmd.Args = append(cmd.Args, append([]string{"-"}, args...)...)
274+
257275
if err := cmd.Start(); err != nil {
258276
panic(err)
259277
}

preambles.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
package main
22

33
var intros = map[string]string{
4-
"99-bottles-of-beer": `<div class="beg hole"><a href=99-bottles-of-beer>99 Bottles of Beer<p>99 bottles of beer on the wall, 99 bottles of beer…</p></a><table>`,
5-
"fizz-buzz": `<div class="beg hole"><a href=fizz-buzz>Fizz Buzz<p>Write a program that prints the numbers from 1 to 100…</p></a><table>`,
6-
"pascals-triangle": `<div class="int hole"><a href=pascals-triangle>Pascal's Triangle<p>Blaise Pascal's arithmetic and geometric figure…</p></a><table>`,
7-
"π": `<div class="adv hole"><a href=π>π<p>The ratio of a circle's circumference to its diameter…</p></a><table>`,
4+
"99-bottles-of-beer": `<div class="beg hole"><a href=99-bottles-of-beer>99 Bottles of Beer<p>99 bottles of beer on the wall, 99 bottles of beer…</p></a><table>`,
5+
"arabic-to-roman-numerals": `<div class="int hole"><a href=arabic-to-roman-numerals>Arabic to Roman Numerals</a><table>`,
6+
"fizz-buzz": `<div class="beg hole"><a href=fizz-buzz>Fizz Buzz<p>Write a program that prints the numbers from 1 to 100…</p></a><table>`,
7+
"pascals-triangle": `<div class="int hole"><a href=pascals-triangle>Pascal's Triangle<p>Blaise Pascal's arithmetic and geometric figure…</p></a><table>`,
8+
"π": `<div class="adv hole"><a href=π>π<p>The ratio of a circle's circumference to its diameter…</p></a><table>`,
89
}
910

1011
var preambles = map[string]string{
1112
"99-bottles-of-beer": `<h1>99 Bottles of Beer</h1>
1213
1314
<p>Print the lyrics to the song 99 Bottles of Beer.</p>`,
15+
"arabic-to-roman-numerals": `<h1>Arabic to Roman Numerals</h1>`,
1416
"fizz-buzz": `<h1>Fizz Buzz</h1>
1517
1618
<p>Print the numbers from <b>1</b> to <b>100</b> (inclusive), each on their own line.</p>

0 commit comments

Comments
 (0)