Skip to content

Commit 7d34727

Browse files
committed
Add the sevent seg hole
1 parent e4992e1 commit 7d34727

File tree

3 files changed

+52
-5
lines changed

3 files changed

+52
-5
lines changed

code-golf.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"strings"
1818
"syscall"
1919
"time"
20+
"unicode"
2021
)
2122

2223
const base = "views/base.html"
@@ -133,6 +134,9 @@ func codeGolf(w http.ResponseWriter, r *http.Request) {
133134

134135
// Drop the trailing newline.
135136
out.Exp = out.Exp[:len(out.Exp)-1]
137+
} else if in.Hole == "seven-segment" {
138+
args = make([]string, 1)
139+
args[0], out.Exp = sevenSegment()
136140
} else {
137141
out.Exp = answers[in.Hole]
138142
}
@@ -265,6 +269,9 @@ func runCode(lang, code string, args []string) (string, string) {
265269
switch lang {
266270
case "javascript":
267271
cmd.Args = []string{"/usr/bin/js", "-f", "-", "--"}
272+
// The perls seems to show -- in @ARGV :-(
273+
case "perl":
274+
cmd.Args = []string{"/usr/bin/perl", "-"}
268275
case "perl6":
269276
cmd.Args = []string{
270277
"/usr/bin/moar",
@@ -294,5 +301,6 @@ func runCode(lang, code string, args []string) (string, string) {
294301

295302
timer.Stop()
296303

297-
return string(bytes.TrimSpace(err.Bytes())), string(bytes.TrimSpace(out.Bytes()))
304+
return string(bytes.TrimRightFunc(err.Bytes(), unicode.IsSpace)),
305+
string(bytes.TrimRightFunc(out.Bytes(), unicode.IsSpace))
298306
}

preambles.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@ var intros = map[string]string{
55
"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>`,
66
"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>`,
77
"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+
"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>`,
89
"π": `<div class="adv hole"><a href=π>π<p>The ratio of a circle's circumference to its diameter…</p></a><table>`,
910
}
1011

1112
var preambles = map[string]string{
12-
"99-bottles-of-beer": `<h1>99 Bottles of Beer</h1><p>Print the lyrics to the song 99 Bottles of Beer.</p>`,
13+
"99-bottles-of-beer": `<h1>99 Bottles of Beer</h1><p>Print the lyrics to the song 99 Bottles of Beer.</p>`,
1314
"arabic-to-roman-numerals": `<h1>Arabic to Roman Numerals</h1><p>For each arabic number argument print the same number in roman numerals.</p>`,
14-
"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>`,
15-
"pascals-triangle": `<h1>Pascal's Triangle</h1><p>Print the first <b>20 rows</b> of Pascal's triangle.</p>`,
16-
"π": `<h1>π</h1><p>Print π (Pi) to the first 1,000 decimal places.</p>`,
15+
"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>`,
16+
"pascals-triangle": `<h1>Pascal's Triangle</h1><p>Print the first <b>20 rows</b> of Pascal's triangle.</p>`,
17+
"seven-segment": `<h1>Seven Segment</h1>`,
18+
"π": `<h1>π</h1><p>Print π (Pi) to the first 1,000 decimal places.</p>`,
1719
}

seven-segment.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package main
2+
3+
import (
4+
"math/rand"
5+
"strings"
6+
"unicode"
7+
)
8+
9+
var segments = [][]string{
10+
{" _ ", " ", " _ ", " _ ", " ", " _ ", " _ ", " _ ", " _ ", " _ "},
11+
{"| |", " |", " _|", " _|", "|_|", "|_ ", "|_ ", " |", "|_|", "|_|"},
12+
{"|_|", " |", "|_ ", " _|", " |", " _|", "|_|", " |", "|_|", " _|"},
13+
}
14+
15+
func sevenSegment() (input, output string) {
16+
digits := []byte{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}
17+
18+
// Shuffle the digits
19+
for i := range digits {
20+
j := rand.Intn(i + 1)
21+
digits[i], digits[j] = digits[j], digits[i]
22+
}
23+
24+
input = string(digits)
25+
26+
for row := 0; row < 3; row++ {
27+
for _, digit := range digits {
28+
output += segments[row][digit-'0']
29+
}
30+
31+
output += "\n"
32+
}
33+
34+
output = strings.TrimRightFunc(output, unicode.IsSpace)
35+
36+
return
37+
}

0 commit comments

Comments
 (0)