From 07098a6e4ab6144ead499f0f911c541b8cd1a085 Mon Sep 17 00:00:00 2001 From: ee7 <45465154+ee7@users.noreply.github.com> Date: Sat, 23 Sep 2023 17:25:01 +0200 Subject: [PATCH 1/8] exercises(secret-handshake): overhaul to use enum This exercise has always [1] required the user to return seq[string], but the scenario is better modelled with an enum. This commit is similar to previous overhauls for the allergies [2] and space-age [3] exercises. Closes: #536 [1] 8015636c3c39, 2019-02-24, "Adds secret-handshake exercise" [2] a8ab9c84e6ab, 2023-01-11, "exercises(allergies): overhaul to use enum" [3] b770d96b4d1e, 2023-01-11, "exercises(space-age): overhaul to use enum" --- .../secret-handshake/.meta/example.nim | 28 ++++++++----------- .../secret-handshake/secret_handshake.nim | 6 +++- .../test_secret_handshake.nim | 18 ++++++------ 3 files changed, 26 insertions(+), 26 deletions(-) diff --git a/exercises/practice/secret-handshake/.meta/example.nim b/exercises/practice/secret-handshake/.meta/example.nim index 0f4618b8..3ba77b2b 100644 --- a/exercises/practice/secret-handshake/.meta/example.nim +++ b/exercises/practice/secret-handshake/.meta/example.nim @@ -1,22 +1,18 @@ import std/algorithm -const - signals = [ - "wink", - "double blink", - "close your eyes", - "jump" - ] - reverseBit = 4 +type + Action* = enum + Wink, DoubleBlink, CloseEyes, Jump, Reverse +func has(n: Natural, action: Action): bool = + (n and 1 shl action.ord) != 0 -func bitset(input: Natural, position: Natural): bool = - (input and 1 shl position) != 0 +func commands*(n: Natural): seq[Action] = + result = newSeqOfCap[Action](Action.high.ord) -func commands*(input: Natural): seq[string] = - for index, item in signals: - if input.bitset(index): - result.add(item) + for action in Wink..Jump: + if n.has(action): + result.add action - if input.bitset(reverseBit): - result.reverse + if n.has(Reverse): + reverse result 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 From 2ca6e38977ec5301b8134b3eb2522ea7f0261105 Mon Sep 17 00:00:00 2001 From: ee7 <45465154+ee7@users.noreply.github.com> Date: Sun, 1 Oct 2023 18:05:01 +0200 Subject: [PATCH 2/8] config(secret-handshake): add enumerations topic --- config.json | 1 + 1 file changed, 1 insertion(+) 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", From 85a6245b96fd308a13f8dbd606c955208c680397 Mon Sep 17 00:00:00 2001 From: ee7 <45465154+ee7@users.noreply.github.com> Date: Mon, 2 Oct 2023 08:00:01 +0200 Subject: [PATCH 3/8] exercises(secret-handshake): example: use testBit Similar to previous discussions [1] I probably have a small preference for using shifts here, but it's fine either way. Note that bitops2 calls this `getBit` [2]. [1] https://github.com/exercism/nim/pull/434#discussion_r1009360892 [2] https://github.com/status-im/nim-stew/blob/3159137d9a31/stew/bitops2.nim#L420-L424 Suggested-by: ynfle --- exercises/practice/secret-handshake/.meta/example.nim | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/exercises/practice/secret-handshake/.meta/example.nim b/exercises/practice/secret-handshake/.meta/example.nim index 3ba77b2b..51634f07 100644 --- a/exercises/practice/secret-handshake/.meta/example.nim +++ b/exercises/practice/secret-handshake/.meta/example.nim @@ -1,11 +1,11 @@ -import std/algorithm +import std/[algorithm, bitops] type Action* = enum Wink, DoubleBlink, CloseEyes, Jump, Reverse func has(n: Natural, action: Action): bool = - (n and 1 shl action.ord) != 0 + testBit(n, action.ord) func commands*(n: Natural): seq[Action] = result = newSeqOfCap[Action](Action.high.ord) From b0b582c0d17a739653eaec52a73b178bbc05b60e Mon Sep 17 00:00:00 2001 From: ee7 <45465154+ee7@users.noreply.github.com> Date: Mon, 2 Oct 2023 08:00:02 +0200 Subject: [PATCH 4/8] exercises(secret-handshake): example: inline use of testBit --- exercises/practice/secret-handshake/.meta/example.nim | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/exercises/practice/secret-handshake/.meta/example.nim b/exercises/practice/secret-handshake/.meta/example.nim index 51634f07..0b6eae6f 100644 --- a/exercises/practice/secret-handshake/.meta/example.nim +++ b/exercises/practice/secret-handshake/.meta/example.nim @@ -4,15 +4,12 @@ type Action* = enum Wink, DoubleBlink, CloseEyes, Jump, Reverse -func has(n: Natural, action: Action): bool = - testBit(n, action.ord) - func commands*(n: Natural): seq[Action] = result = newSeqOfCap[Action](Action.high.ord) for action in Wink..Jump: - if n.has(action): + if n.testBit(action.ord): result.add action - if n.has(Reverse): + if n.testBit(Reverse.ord): reverse result From e0a53ef077d373bf69fa921cc6de3821b13319a4 Mon Sep 17 00:00:00 2001 From: ee7 <45465154+ee7@users.noreply.github.com> Date: Mon, 2 Oct 2023 08:00:03 +0200 Subject: [PATCH 5/8] exercises(secret-handshake): example: DRY enum vals This better supports adding new enum values, as long as those values aren't "special" like Reverse. --- exercises/practice/secret-handshake/.meta/example.nim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exercises/practice/secret-handshake/.meta/example.nim b/exercises/practice/secret-handshake/.meta/example.nim index 0b6eae6f..04145456 100644 --- a/exercises/practice/secret-handshake/.meta/example.nim +++ b/exercises/practice/secret-handshake/.meta/example.nim @@ -7,7 +7,7 @@ type func commands*(n: Natural): seq[Action] = result = newSeqOfCap[Action](Action.high.ord) - for action in Wink..Jump: + for action in Action.low ..< Action.high: if n.testBit(action.ord): result.add action From eebf075def877642fc798ed069c4b0d02a7866fd Mon Sep 17 00:00:00 2001 From: ee7 <45465154+ee7@users.noreply.github.com> Date: Mon, 2 Oct 2023 08:00:04 +0200 Subject: [PATCH 6/8] exercises(secret-handshake): example: refactor via case --- .../practice/secret-handshake/.meta/example.nim | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/exercises/practice/secret-handshake/.meta/example.nim b/exercises/practice/secret-handshake/.meta/example.nim index 04145456..1aa8078e 100644 --- a/exercises/practice/secret-handshake/.meta/example.nim +++ b/exercises/practice/secret-handshake/.meta/example.nim @@ -7,9 +7,11 @@ type func commands*(n: Natural): seq[Action] = result = newSeqOfCap[Action](Action.high.ord) - for action in Action.low ..< Action.high: - if n.testBit(action.ord): - result.add action - - if n.testBit(Reverse.ord): - reverse result + for action in Action: + case action + of Wink..Jump: + if n.testBit(action.ord): + result.add action + of Reverse: + if n.testBit(action.ord): + reverse result From 0a08b7156c01714aef3e5b99af2f73e0ae1b8f1a Mon Sep 17 00:00:00 2001 From: ee7 <45465154+ee7@users.noreply.github.com> Date: Mon, 2 Oct 2023 08:00:05 +0200 Subject: [PATCH 7/8] exercises(secret-handshake): example: refactor to testBit first --- exercises/practice/secret-handshake/.meta/example.nim | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/exercises/practice/secret-handshake/.meta/example.nim b/exercises/practice/secret-handshake/.meta/example.nim index 1aa8078e..94f44016 100644 --- a/exercises/practice/secret-handshake/.meta/example.nim +++ b/exercises/practice/secret-handshake/.meta/example.nim @@ -8,10 +8,9 @@ func commands*(n: Natural): seq[Action] = result = newSeqOfCap[Action](Action.high.ord) for action in Action: - case action - of Wink..Jump: - if n.testBit(action.ord): + if n.testBit(action.ord): + case action + of Wink..Jump: result.add action - of Reverse: - if n.testBit(action.ord): + of Reverse: reverse result From 94fc7d39a00775edf4b44c8cec5dcdb890454b43 Mon Sep 17 00:00:00 2001 From: ee7 <45465154+ee7@users.noreply.github.com> Date: Mon, 2 Oct 2023 08:00:06 +0200 Subject: [PATCH 8/8] exercises(secret-handshake): example: write result first For symmetry with the branch above. --- exercises/practice/secret-handshake/.meta/example.nim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exercises/practice/secret-handshake/.meta/example.nim b/exercises/practice/secret-handshake/.meta/example.nim index 94f44016..e3a57d08 100644 --- a/exercises/practice/secret-handshake/.meta/example.nim +++ b/exercises/practice/secret-handshake/.meta/example.nim @@ -13,4 +13,4 @@ func commands*(n: Natural): seq[Action] = of Wink..Jump: result.add action of Reverse: - reverse result + result.reverse()