Skip to content

Commit 35da66a

Browse files
committed
Add the "sierpiński-triangle" hole
Updates #3
1 parent aa9e81e commit 35da66a

File tree

4 files changed

+51
-1
lines changed

4 files changed

+51
-1
lines changed

answers.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,22 @@ Buzz`,
485485
83
486486
89
487487
97`,
488+
"sierpiński-triangle": ` ▲
489+
▲ ▲
490+
▲ ▲
491+
▲ ▲ ▲ ▲
492+
▲ ▲
493+
▲ ▲ ▲ ▲
494+
▲ ▲ ▲ ▲
495+
▲ ▲ ▲ ▲ ▲ ▲ ▲ ▲
496+
▲ ▲
497+
▲ ▲ ▲ ▲
498+
▲ ▲ ▲ ▲
499+
▲ ▲ ▲ ▲ ▲ ▲ ▲ ▲
500+
▲ ▲ ▲ ▲
501+
▲ ▲ ▲ ▲ ▲ ▲ ▲ ▲
502+
▲ ▲ ▲ ▲ ▲ ▲ ▲ ▲
503+
▲ ▲ ▲ ▲ ▲ ▲ ▲ ▲ ▲ ▲ ▲ ▲ ▲ ▲ ▲ ▲`,
488504
"pascals-triangle": `1
489505
1 1
490506
1 2 1

code-golf.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"bufio"
45
"bytes"
56
"compress/gzip"
67
"crypto/hmac"
@@ -252,6 +253,7 @@ func codeGolf(w http.ResponseWriter, r *http.Request) {
252253
"pascals-triangle",
253254
"prime-numbers",
254255
"seven-segment",
256+
"sierpiński-triangle",
255257
"spelling-numbers",
256258
"π":
257259
hole = parts[0]
@@ -380,9 +382,23 @@ func runCode(lang, code string, args []string) (string, string) {
380382

381383
timer.Stop()
382384

385+
var outBytes []byte
386+
387+
// Trim trailing spaces per line.
388+
// FIXME This is all very hacky, but needed for Sierpiński.
389+
scanner := bufio.NewScanner(bytes.NewReader(out.Bytes()))
390+
for scanner.Scan() {
391+
outBytes = append(outBytes, bytes.TrimRightFunc(scanner.Bytes(), unicode.IsSpace)...)
392+
outBytes = append(outBytes, '\n')
393+
}
394+
395+
if err := scanner.Err(); err != nil {
396+
panic(err)
397+
}
398+
383399
// Trim trailing whitespace.
384400
errBytes := bytes.TrimRightFunc(err.Bytes(), unicode.IsSpace)
385-
outBytes := bytes.TrimRightFunc(out.Bytes(), unicode.IsSpace)
401+
outBytes = bytes.TrimRightFunc(outBytes, unicode.IsSpace)
386402

387403
// Escape HTML & convert ANSI to HTML in stderr.
388404
errBytes = terminal.Render(errBytes)

db.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ func printScores(w io.WriteCloser, hole, lang string, userID int) {
185185
{"fizz-buzz", "Fizz Buzz"},
186186
{"pascals-triangle", "Pascal's Triangle"},
187187
{"prime-numbers", "Prime Numbers"},
188+
{"sierpiński-triangle", "Sierpiński Triangle"},
188189
{"seven-segment", "Seven Segment"},
189190
{"spelling-numbers", "Spelling Numbers"},
190191
{"π", "π"},

preambles.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ var intros = map[string]string{
66
"fibonacci": `<div class="beg hole"><a href=fibonacci>Fibonacci<p>Each number is the sum of the two preceding numbers…</p></a><table>`,
77
"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>`,
88
"prime-numbers": `<div class="beg hole"><a href=prime-numbers>Prime Numbers</a><table>`,
9+
"sierpiński-triangle": `<div class="beg hole"><a href=sierpiński-triangle>Sierpiński Triangle</a><table>`,
910
"arabic-to-roman-numerals": `<div class="int hole"><a href=arabic-to-roman-numerals>Arabic to Roman<p>Convert Hindu–Arabic numerals to Roman numerals…</p></a><table>`,
1011
"pascals-triangle": `<div class="int hole"><a href=pascals-triangle>Pascal's Triangle<p>Blaise Pascal's arithmetic and geometric figure…</p></a><table>`,
1112
"seven-segment": `<div class="int hole"><a href=seven-segment>Seven Segment<p>Using pipes and underscores print a seven segment display…</p></a><table>`,
@@ -20,6 +21,22 @@ var preambles = map[string]string{
2021
"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>`,
2122
"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>`,
2223
"prime-numbers": `<h1>Prime Numbers</h1><p>Print all the prime numbers between <b>1</b> and <b>100</b></p>`,
24+
"sierpiński-triangle": `<h1>Sierpiński Triangle</h1><p>The Sierpiński triangle is a fractal and attractive fixed set with the overall shape of an equilateral triangle, subdivided recursively into smaller equilateral triangles.</p><p>A Sierpiński triangle of order 4 should look like this, print such an output:</p><pre> ▲
25+
▲ ▲
26+
▲ ▲
27+
▲ ▲ ▲ ▲
28+
▲ ▲
29+
▲ ▲ ▲ ▲
30+
▲ ▲ ▲ ▲
31+
▲ ▲ ▲ ▲ ▲ ▲ ▲ ▲
32+
▲ ▲
33+
▲ ▲ ▲ ▲
34+
▲ ▲ ▲ ▲
35+
▲ ▲ ▲ ▲ ▲ ▲ ▲ ▲
36+
▲ ▲ ▲ ▲
37+
▲ ▲ ▲ ▲ ▲ ▲ ▲ ▲
38+
▲ ▲ ▲ ▲ ▲ ▲ ▲ ▲
39+
▲ ▲ ▲ ▲ ▲ ▲ ▲ ▲ ▲ ▲ ▲ ▲ ▲ ▲ ▲ ▲</pre>`,
2340
"arabic-to-roman-numerals": `<h1>Arabic to Roman Numerals</h1><p>For each arabic number argument print the same number in roman numerals.</p>`,
2441
"pascals-triangle": `<h1>Pascal's Triangle</h1><p>Print the first <b>20 rows</b> of Pascal's triangle.</p>`,
2542
"seven-segment": `<h1>Seven Segment</h1>`,

0 commit comments

Comments
 (0)