-
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
exercises(secret-handshake): overhaul to use enum (#537)
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
Showing
4 changed files
with
28 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -853,6 +853,7 @@ | |
"topics": [ | ||
"bitwise_operations", | ||
"conditionals", | ||
"enumerations", | ||
"integers", | ||
"lists", | ||
"logic", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters