swift
swift copied to clipboard
BombDefuser exemplar solution and tests
I am trying a to mentor a student on the BombDefuser exercise.
It states:
For each bit in the ID number, starting with the leftmost bit, you will apply the flipper closure to the wires tuple if the bit is a 0 and you will apply the rotator closure if it is a 1 giving the new state of the wires.
To my understanding, this means that I should process the leftmost bit, which the left most 1 or 0.
If my number is 253 (like in the tests), its binary representation is 1111 1101 and I should process bits in the following order: 1, 1, 1, 1, 1, 1, 0 and 1.
In the exemplar solution, the implementation performs: if bits.isMultiple(of: 2). But that is checking the rightmost bit, not the leftmost.
What am I missing? Is this an error in the implementation? Or in the instructions?
Fun fact, the 113 number (used in tests) is working in both directions... leading to some confusion debugging all this.
What am I missing? Is this an error in the implementation? Or in the instructions?
It can be what we choose it to be. Do we want to update the instructions or the implementation? The right-to-left implementation is a bit easier I think, right? If so, it might be best to update the instructions as we don't want to have students implement a very complex solution.
Fun fact, the 113 number (used in tests) is working in both directions... leading to some confusion debugging all this.
Aha, this must be why the exemplar solution's implementation still passes the tests. We should add a new test that would fail with an invalid implementation.
Would you be willing to submit a PR to fix this?
The right-to-left implementation is indeed a bit easier, using % and / operators. But instructions mention bits, and they would not be used nor manipulated through bitwise operators in the exemplar solution. Which could be confusing.
In both cases, instructions should mention to encode on eight bits, just to be sure students do not forget some 0s.
Note : the String(_:radix:uppercase:) can also be useful here, and allow students to visualise bits.
I am open to submit a PR as soon as we decided where to go 🙂
Also, sorry for creating this issue in the wrong repo. Could you please transfer it to exercism/swift? Thanks!
These are the exercise's prerequisites:
- "functions", "higher-order-functions", "loops"
With just those prerequisites (and their transitive prerequisites), I don't think we can expect the user to know how to do bitwise manipulation. Similarly, the exercise uses the UInt8 type, which also is not known to the student. I would suggest one of the following:
Option 1:
- Add
arraysas a prerequisite - Change the
UInt8parameter to an array ofIntvalues, effectively replacing the single byte with an array of0and1s
Option 2:
- Add
charactersas a prerequisite - Change the
UInt8parameter to aStringthat contains0and1characters.
You are right, bitwise operators are too complicated for a beginner. Actually there are only explained in one the last chapter of the Apple Swift book.
Are you suggesting to completely replace Int by an array Int, or a String?
I am.
OK great. Will work on the PR then.
I've just completed the exercise myself and was confused by the description to start with the leftmost bit. Checking the bits in Calculator.app gave me the clue to my misunderstanding. If you visualize the binary representation as an array, the first bit starts at position 0 and grows to the right. However the string representation shows the Least Significant Bit (LSB) on the rightmost side.
@paiv mentioned the same observation in #493. I just wanted to +1 this conversation. 😄
I think it would be nice to treat the issue as a typo als long as the exercise isn't thorougly reworked. That would certainly prevent some confusion - as I just had, too ^^