Add tasks for all the new languages
This commit is contained in:
parent
9dc3c2bb62
commit
bba7bfd280
13208 changed files with 134745 additions and 0 deletions
40
Task/24-game/Nim/24-game.nim
Normal file
40
Task/24-game/Nim/24-game.nim
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
import math, strutils, algorithm, sequtils
|
||||
randomize()
|
||||
|
||||
template newSeqWith(len: int, init: expr): expr =
|
||||
var result {.gensym.} = newSeq[type(init)](len)
|
||||
for i in 0 .. <len:
|
||||
result[i] = init
|
||||
result
|
||||
|
||||
var
|
||||
problem = newSeqWith(4, random(1..9))
|
||||
stack = newSeq[float]()
|
||||
digits = newSeq[int]()
|
||||
|
||||
echo "Make 24 with the digits: ", problem
|
||||
|
||||
template op(c): stmt =
|
||||
let a = stack.pop
|
||||
stack.add c(stack.pop, a)
|
||||
|
||||
for c in stdin.readLine:
|
||||
case c
|
||||
of '1'..'9':
|
||||
digits.add c.ord - '0'.ord
|
||||
stack.add float(c.ord - '0'.ord)
|
||||
of '+': op `+`
|
||||
of '*': op `*`
|
||||
of '-': op `-`
|
||||
of '/': op `/`
|
||||
of Whitespace: discard
|
||||
else: raise ValueError.newException "Wrong char: " & c
|
||||
|
||||
sort digits, cmp[int]
|
||||
sort problem, cmp[int]
|
||||
if digits.deduplicate != problem.deduplicate:
|
||||
raise ValueError.newException "Not using the given digits."
|
||||
if stack.len != 1:
|
||||
raise ValueError.newException "Wrong expression."
|
||||
echo "Result: ", stack[0]
|
||||
echo if abs(stack[0] - 24) < 0.001: "Good job!" else: "Try again."
|
||||
Loading…
Add table
Add a link
Reference in a new issue