Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

exercises(secret-handshake): overhaul to use enum #537

Merged
merged 9 commits into from
Oct 2, 2023
1 change: 1 addition & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -853,6 +853,7 @@
"topics": [
"bitwise_operations",
"conditionals",
"enumerations",
"integers",
"lists",
"logic",
Expand Down
32 changes: 13 additions & 19 deletions exercises/practice/secret-handshake/.meta/example.nim
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
import std/algorithm
import std/[algorithm, bitops]

const
signals = [
"wink",
"double blink",
"close your eyes",
"jump"
]
reverseBit = 4
type
Action* = enum
Wink, DoubleBlink, CloseEyes, Jump, Reverse

func commands*(n: Natural): seq[Action] =
result = newSeqOfCap[Action](Action.high.ord)

func bitset(input: Natural, position: Natural): bool =
(input and 1 shl position) != 0

func commands*(input: Natural): seq[string] =
for index, item in signals:
if input.bitset(index):
result.add(item)

if input.bitset(reverseBit):
result.reverse
for action in Action:
if n.testBit(action.ord):
case action
of Wink..Jump:
result.add action
of Reverse:
result.reverse()
6 changes: 5 additions & 1 deletion exercises/practice/secret-handshake/secret_handshake.nim
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
proc commands*(n: int): seq[string] =
type
Action* = enum
Wink, DoubleBlink, CloseEyes, Jump

proc commands*(n: int): seq[Action] =
discard
18 changes: 9 additions & 9 deletions exercises/practice/secret-handshake/test_secret_handshake.nim
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,34 @@ import secret_handshake

suite "Secret Handshake":
test "wink for 1":
check commands(1) == @["wink"]
check commands(1) == @[Wink]

test "double blink for 10":
check commands(2) == @["double blink"]
check commands(2) == @[DoubleBlink]

test "close your eyes for 100":
check commands(4) == @["close your eyes"]
check commands(4) == @[CloseEyes]

test "jump for 1000":
check commands(8) == @["jump"]
check commands(8) == @[Jump]

test "combine two actions":
check commands(3) == @["wink", "double blink"]
check commands(3) == @[Wink, DoubleBlink]

test "reverse two actions":
check commands(19) == @["double blink", "wink"]
check commands(19) == @[DoubleBlink, Wink]

test "reversing one action gives the same action":
check commands(24) == @["jump"]
check commands(24) == @[Jump]

test "reversing no actions still gives no actions":
check commands(16).len == 0

test "all possible actions":
check commands(15) == @["wink", "double blink", "close your eyes", "jump"]
check commands(15) == @[Wink, DoubleBlink, CloseEyes, Jump]

test "reverse all possible actions":
check commands(31) == @["jump", "close your eyes", "double blink", "wink"]
check commands(31) == @[Jump, CloseEyes, DoubleBlink, Wink]

test "do nothing for zero":
check commands(0).len == 0
Loading