Skip to content

Commit ee1742f

Browse files
committed
Add the "quine" hole
Updates #3
1 parent 1456371 commit ee1742f

File tree

6 files changed

+45
-31
lines changed

6 files changed

+45
-31
lines changed

assets/hole.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,11 @@ onload = function() {
7777

7878
let pre = document.getElementById(prop);
7979

80-
pre.innerHTML = data[prop];
80+
// Err can be ANSI coloured via HTML.
81+
if (prop == Err)
82+
pre.innerHTML = data[prop];
83+
else
84+
pre.innerText = data[prop];
8185

8286
// Only show Arg & Err if they contain something.
8387
if (prop === 'Arg' || prop === 'Err')

routes/hole.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ var preambles = map[string]string{
1919
"pascals-triangle": `<h1>Pascal's Triangle</h1><p>Print the first <b>20 rows</b> of Pascal's triangle.</p>`,
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>`,
22+
"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>`,
2223
"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> _ _ _ _ _ _ _ _
2324
| | | _| _||_||_ |_ ||_||_|
2425
|_| ||_ _| | _||_| ||_| _|</pre>`,

routes/home.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,14 @@ func home(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
6262
WHEN 'pascals-triangle' THEN 6
6363
WHEN 'pernicious-numbers' THEN 7
6464
WHEN 'prime-numbers' THEN 8
65-
WHEN '99-bottles-of-beer' THEN 9
66-
WHEN 'seven-segment' THEN 10
67-
WHEN 'sierpiński-triangle' THEN 11
68-
WHEN 'π' THEN 12
69-
WHEN 'e' THEN 13
70-
WHEN 'arabic-to-roman-numerals' THEN 14
71-
WHEN 'spelling-numbers' THEN 15
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
7273
END, row_number`,
7374
printHeader(w, r, 200),
7475
)
@@ -148,6 +149,8 @@ func home(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
148149
w.Write([]byte(`Fast><a href=pernicious-numbers>Pernicious Numbers`))
149150
case "prime-numbers":
150151
w.Write([]byte(`Fast><a href=prime-numbers>Prime Numbers`))
152+
case "quine":
153+
w.Write([]byte(`Fast><a href=quine>Quine`))
151154
case "seven-segment":
152155
w.Write([]byte(`Medium><a href=seven-segment>Seven Segment`))
153156
case "sierpiński-triangle":

routes/routes.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ func init() {
4141
Router.GET("/pascals-triangle", middleware.Gzip(hole))
4242
Router.GET("/pernicious-numbers", middleware.Gzip(hole))
4343
Router.GET("/prime-numbers", middleware.Gzip(hole))
44+
Router.GET("/quine", middleware.Gzip(hole))
4445
Router.GET("/seven-segment", middleware.Gzip(hole))
4546
Router.GET("/sierpiński-triangle", middleware.Gzip(hole))
4647
Router.GET("/spelling-numbers", middleware.Gzip(hole))

routes/scores.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ func scores(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
2727
"pascals-triangle",
2828
"pernicious-numbers",
2929
"prime-numbers",
30+
"quine",
3031
"seven-segment",
3132
"sierpiński-triangle",
3233
"spelling-numbers",
@@ -65,6 +66,7 @@ func scores(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
6566
{"pascals-triangle", "Pascal's Triangle"},
6667
{"pernicious-numbers", "Pernicious Numbers"},
6768
{"prime-numbers", "Prime Numbers"},
69+
{"quine", "Quine"},
6870
{"sierpiński-triangle", "Sierpiński Triangle"},
6971
{"seven-segment", "Seven Segment"},
7072
{"spelling-numbers", "Spelling Numbers"},

routes/solution.go

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ func solution(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
4848

4949
// Drop the trailing newline.
5050
out.Exp = out.Exp[:len(out.Exp)-1]
51+
} else if in.Hole == "quine" {
52+
out.Exp = in.Code
5153
} else if in.Hole == "seven-segment" {
5254
args = make([]string, 1)
5355
args[0], out.Exp = sevenSegment()
@@ -57,7 +59,7 @@ func solution(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
5759
out.Exp = answers[in.Hole]
5860
}
5961

60-
out.Err, out.Out = runCode(in.Lang, in.Code, args)
62+
out.Err, out.Out = runCode(in.Hole, in.Lang, in.Code, args)
6163
out.Arg = strings.Join(args, " ")
6264

6365
out.Diff, _ = difflib.GetUnifiedDiffString(difflib.UnifiedDiff{
@@ -102,7 +104,7 @@ func solution(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
102104
}
103105
}
104106

105-
func runCode(lang, code string, args []string) (string, string) {
107+
func runCode(hole, lang, code string, args []string) (string, string) {
106108
var err, out bytes.Buffer
107109

108110
if lang == "php" {
@@ -177,32 +179,33 @@ func runCode(lang, code string, args []string) (string, string) {
177179

178180
// Trim trailing whitespace.
179181
errBytes := bytes.TrimRightFunc(err.Bytes(), unicode.IsSpace)
180-
outBytes = bytes.TrimRightFunc(outBytes, unicode.IsSpace)
182+
183+
if hole != "quine" {
184+
outBytes = bytes.TrimRightFunc(outBytes, unicode.IsSpace)
185+
}
181186

182187
// Escape HTML & convert ANSI to HTML in stderr.
183188
errBytes = terminal.Render(errBytes)
184189

185-
// Escape HTML in stdout
186-
outBytes = bytes.Replace(outBytes, []byte{'<'}, []byte("&lt;"), -1)
187-
outBytes = bytes.Replace(outBytes, []byte{'>'}, []byte("&gt;"), -1)
188-
189190
// ASCII-ify roman numerals
190-
outBytes = bytes.Replace(outBytes, []byte("Ⅰ"), []byte("I"), -1)
191-
outBytes = bytes.Replace(outBytes, []byte("Ⅱ"), []byte("II"), -1)
192-
outBytes = bytes.Replace(outBytes, []byte("Ⅲ"), []byte("III"), -1)
193-
outBytes = bytes.Replace(outBytes, []byte("Ⅳ"), []byte("IV"), -1)
194-
outBytes = bytes.Replace(outBytes, []byte("Ⅴ"), []byte("V"), -1)
195-
outBytes = bytes.Replace(outBytes, []byte("Ⅵ"), []byte("VI"), -1)
196-
outBytes = bytes.Replace(outBytes, []byte("Ⅶ"), []byte("VII"), -1)
197-
outBytes = bytes.Replace(outBytes, []byte("Ⅷ"), []byte("VIII"), -1)
198-
outBytes = bytes.Replace(outBytes, []byte("Ⅸ"), []byte("IX"), -1)
199-
outBytes = bytes.Replace(outBytes, []byte("Ⅹ"), []byte("X"), -1)
200-
outBytes = bytes.Replace(outBytes, []byte("Ⅺ"), []byte("XI"), -1)
201-
outBytes = bytes.Replace(outBytes, []byte("Ⅻ"), []byte("XII"), -1)
202-
outBytes = bytes.Replace(outBytes, []byte("Ⅼ"), []byte("L"), -1)
203-
outBytes = bytes.Replace(outBytes, []byte("Ⅽ"), []byte("C"), -1)
204-
outBytes = bytes.Replace(outBytes, []byte("Ⅾ"), []byte("D"), -1)
205-
outBytes = bytes.Replace(outBytes, []byte("Ⅿ"), []byte("M"), -1)
191+
if hole == "arabic-to-roman-numerals" {
192+
outBytes = bytes.Replace(outBytes, []byte("Ⅰ"), []byte("I"), -1)
193+
outBytes = bytes.Replace(outBytes, []byte("Ⅱ"), []byte("II"), -1)
194+
outBytes = bytes.Replace(outBytes, []byte("Ⅲ"), []byte("III"), -1)
195+
outBytes = bytes.Replace(outBytes, []byte("Ⅳ"), []byte("IV"), -1)
196+
outBytes = bytes.Replace(outBytes, []byte("Ⅴ"), []byte("V"), -1)
197+
outBytes = bytes.Replace(outBytes, []byte("Ⅵ"), []byte("VI"), -1)
198+
outBytes = bytes.Replace(outBytes, []byte("Ⅶ"), []byte("VII"), -1)
199+
outBytes = bytes.Replace(outBytes, []byte("Ⅷ"), []byte("VIII"), -1)
200+
outBytes = bytes.Replace(outBytes, []byte("Ⅸ"), []byte("IX"), -1)
201+
outBytes = bytes.Replace(outBytes, []byte("Ⅹ"), []byte("X"), -1)
202+
outBytes = bytes.Replace(outBytes, []byte("Ⅺ"), []byte("XI"), -1)
203+
outBytes = bytes.Replace(outBytes, []byte("Ⅻ"), []byte("XII"), -1)
204+
outBytes = bytes.Replace(outBytes, []byte("Ⅼ"), []byte("L"), -1)
205+
outBytes = bytes.Replace(outBytes, []byte("Ⅽ"), []byte("C"), -1)
206+
outBytes = bytes.Replace(outBytes, []byte("Ⅾ"), []byte("D"), -1)
207+
outBytes = bytes.Replace(outBytes, []byte("Ⅿ"), []byte("M"), -1)
208+
}
206209

207210
return string(errBytes), string(outBytes)
208211
}

0 commit comments

Comments
 (0)