Skip to content

Commit 71cad37

Browse files
committed
Make "Reverse Polish Notation" live
1 parent cff9bb2 commit 71cad37

File tree

4 files changed

+27
-21
lines changed

4 files changed

+27
-21
lines changed

config/holes.toml

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1730,16 +1730,22 @@ preamble = '''
17301730
<p>For each Roman numeral argument print the same number in Arabic numerals.
17311731
'''
17321732

1733-
['RPN Evaluator']
1733+
['Reverse Polish Notation']
17341734
category = 'Computing'
1735-
experiment = -1
17361735
links = [
17371736
{ name = 'Rosetta Code', url = '//rosettacode.org/wiki/Parsing/RPN_calculator_algorithm' },
17381737
{ name = 'Wikipedia', url = '//www.wikipedia.org/wiki/Reverse_Polish_notation' },
17391738
]
17401739
preamble = '''
1741-
<p>Evaluate each RPN (Reverse Polish notation) expression.
1742-
<p>The result of each operation is a non-negative integer not exceeding 32,767.
1740+
<p>Evaluate each Reverse Polish notation expression.
1741+
1742+
<p>
1743+
All given numbers and the result of each operation are positive integers
1744+
not exceeding <b>32,767</b>.
1745+
1746+
<p>
1747+
Only the operators <code><b>+</b></code>, <code><b>-</b></code>,
1748+
<code><b>*</b></code>, and <code><b>/</b></code> are used.
17431749
'''
17441750

17451751
['Rule 110']

db/a-schema.sql

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,12 @@ CREATE TYPE hole AS ENUM (
3030
'morse-encoder', 'musical-chords', 'niven-numbers', 'number-spiral',
3131
'odious-numbers', 'ordinal-numbers', 'pangram-grep', 'pascals-triangle',
3232
'pernicious-numbers', 'poker', 'prime-numbers', 'prime-numbers-long',
33-
'qr-decoder', 'quine', 'recamán', 'rock-paper-scissors-spock-lizard',
34-
'roman-to-arabic', 'rule-110', 'seven-segment', 'sierpiński-triangle',
35-
'smith-numbers', 'spelling-numbers', 'star-wars-opening-crawl', 'sudoku',
36-
'sudoku-v2', 'ten-pin-bowling', 'tongue-twisters', 'united-states',
37-
'vampire-numbers', 'van-eck-sequence', 'λ', 'π', 'τ', 'φ', '√2', '𝑒'
33+
'qr-decoder', 'quine', 'recamán', 'reverse-polish-notation',
34+
'rock-paper-scissors-spock-lizard', 'roman-to-arabic', 'rule-110',
35+
'seven-segment', 'sierpiński-triangle', 'smith-numbers',
36+
'spelling-numbers', 'star-wars-opening-crawl', 'sudoku', 'sudoku-v2',
37+
'ten-pin-bowling', 'tongue-twisters', 'united-states', 'vampire-numbers',
38+
'van-eck-sequence', 'λ', 'π', 'τ', 'φ', '√2', '𝑒'
3839
);
3940

4041
CREATE TYPE keymap AS ENUM ('default', 'vim');

hole/play.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ func getAnswer(holeID, code string) (args []string, answer string) {
9595
answer = code
9696
case "rock-paper-scissors-spock-lizard":
9797
args, answer = rockPaperScissorsSpockLizard()
98-
case "rpn-evaluator":
99-
args, answer = rpnEvaluator()
98+
case "reverse-polish-notation":
99+
args, answer = reversePolishNotation()
100100
case "seven-segment":
101101
args, answer = sevenSegment()
102102
case "spelling-numbers":
Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ func expand(node *Node) {
3232
val := node.value
3333
op := "+-*/"[rand.Intn(4)]
3434

35-
var leftVal int
36-
var rightVal int
35+
var leftVal, rightVal int
3736

3837
switch op {
3938
case '+':
@@ -119,32 +118,32 @@ func genExpr(initVal int, expander func(*Node, int), expandCount int) *Node {
119118
return init
120119
}
121120

122-
func rpnEvaluator() ([]string, string) {
123-
const TestsCount = 20
121+
func reversePolishNotation() ([]string, string) {
122+
const tests = 20
124123

125-
exprs := [TestsCount]*Node{
124+
exprs := [tests]*Node{
126125
genExpr(randInt(1, MaxInt), expandLeft, randInt(16, 31)),
127126
genExpr(randInt(1, MaxInt), expandRight, randInt(16, 31)),
128127
genExpr(randInt(1, MaxInt), expandRight, 0),
129128
genExpr(0, expandRand, randInt(16, 31)),
130129
}
131130

132-
for i := 4; i < TestsCount; i++ {
131+
for i := 4; i < tests; i++ {
133132
exprs[i] = genExpr(randInt(1, MaxInt), expandRand, randInt(1, 31))
134133
}
135134

136135
rand.Shuffle(len(exprs), func(i, j int) {
137136
exprs[i], exprs[j] = exprs[j], exprs[i]
138137
})
139138

140-
args := make([]string, TestsCount)
139+
args := make([]string, tests)
141140
var answer strings.Builder
142141

143-
for k, expr := range exprs {
142+
for i, expr := range exprs {
144143
var arg strings.Builder
145144
printNode(&arg, expr)
146-
args[k] = arg.String()
147-
if k > 0 {
145+
args[i] = arg.String()
146+
if i > 0 {
148147
answer.WriteByte('\n')
149148
}
150149
answer.WriteString(strconv.Itoa(expr.value))

0 commit comments

Comments
 (0)