diff --git a/config.json b/config.json index 448ff0b6..772a7e76 100644 --- a/config.json +++ b/config.json @@ -853,6 +853,7 @@ "topics": [ "bitwise_operations", "conditionals", + "enumerations", "integers", "lists", "logic", diff --git a/exercises/practice/secret-handshake/.meta/example.nim b/exercises/practice/secret-handshake/.meta/example.nim index 0f4618b8..e3a57d08 100644 --- a/exercises/practice/secret-handshake/.meta/example.nim +++ b/exercises/practice/secret-handshake/.meta/example.nim @@ -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() diff --git a/exercises/practice/secret-handshake/secret_handshake.nim b/exercises/practice/secret-handshake/secret_handshake.nim index 7910973b..13d4e583 100644 --- a/exercises/practice/secret-handshake/secret_handshake.nim +++ b/exercises/practice/secret-handshake/secret_handshake.nim @@ -1,2 +1,6 @@ -proc commands*(n: int): seq[string] = +type + Action* = enum + Wink, DoubleBlink, CloseEyes, Jump + +proc commands*(n: int): seq[Action] = discard diff --git a/exercises/practice/secret-handshake/test_secret_handshake.nim b/exercises/practice/secret-handshake/test_secret_handshake.nim index bcd0a892..c401f269 100644 --- a/exercises/practice/secret-handshake/test_secret_handshake.nim +++ b/exercises/practice/secret-handshake/test_secret_handshake.nim @@ -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