Skip to content

Commit

Permalink
exercises(secret-handshake): overhaul to use enum (#537)
Browse files Browse the repository at this point in the history
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] 8015636, 2019-02-24, "Adds secret-handshake exercise"
[2] a8ab9c8, 2023-01-11, "exercises(allergies): overhaul to use enum"
[3] b770d96, 2023-01-11, "exercises(space-age): overhaul to use enum"
  • Loading branch information
ee7 authored Oct 2, 2023
1 parent 33e3bcd commit 81d1c6c
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 29 deletions.
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

0 comments on commit 81d1c6c

Please sign in to comment.