Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add bitwise concept #722

Open
wants to merge 26 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
90ed84e
Initial stub.
stewartmurrie Nov 2, 2024
906a767
Adds stub for bitwise concept exericse (secrets)
stewartmurrie Nov 2, 2024
c925970
Adds first draft of concept
stewartmurrie Nov 2, 2024
52770b9
Added notes on masking
stewartmurrie Nov 3, 2024
82e0424
Adds introduction.md (a copy of about.md)
stewartmurrie Nov 3, 2024
77679ee
Updates config and renames concept folder
stewartmurrie Nov 3, 2024
cbbe48f
Updates configs (lints OK now)
stewartmurrie Nov 3, 2024
2f18e88
Updates allergies metadata to refer to bitwise-operations
stewartmurrie Nov 3, 2024
2d5dea3
Adds secrets exercise docs
stewartmurrie Nov 3, 2024
9a17e1c
Adds stub, example implementation, and tests
stewartmurrie Nov 3, 2024
6e4d3f3
Removes the WIP status from the exercise
stewartmurrie Nov 5, 2024
f8fca1a
Fixes typo in the exercise instructions
stewartmurrie Nov 5, 2024
b073b12
Adds Secret handshake to bitwise exercises
stewartmurrie Nov 7, 2024
54545de
Updates heading
stewartmurrie Nov 7, 2024
b0cf475
Updates for consistency & formatting
stewartmurrie Nov 7, 2024
7d00134
Update for consistency with other exercises
stewartmurrie Nov 7, 2024
aa79d27
Attempts to use templates
stewartmurrie Nov 17, 2024
ce39d30
Updates to the templated
stewartmurrie Nov 17, 2024
fc09e07
Simplified the examples. Added spacing.
stewartmurrie Nov 17, 2024
e76389a
Added general hint linking to package docs
stewartmurrie Nov 17, 2024
6ceb47f
Adds helpful links to the hints
stewartmurrie Nov 17, 2024
40ad002
Fixes typos in the hints
stewartmurrie Nov 17, 2024
6a60cab
Adds 5th concept step to tie it all together
stewartmurrie Nov 17, 2024
662f383
Formatting
stewartmurrie Nov 17, 2024
d7cfa4b
Fixes doc bugs
stewartmurrie Nov 24, 2024
0ca8335
Add contribs
stewartmurrie Nov 24, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Adds 5th concept step to tie it all together
stewartmurrie committed Nov 17, 2024
commit 6a60cab269ba270bade42e15fc62820d9d824cdb
11 changes: 11 additions & 0 deletions exercises/concept/secrets/.docs/hints.md
Original file line number Diff line number Diff line change
@@ -19,6 +19,17 @@
- [One of the bitwise functions][bitwise-and] clears bits where the bit in the mask is 0.
- But, you may need to combine it with [another function][bitwise-complement] to clear bits where the mask is 1.

## 5. Decrypt a message

- Apply the other functions you wrote to the input in the following order, taking the output of one and using it as the input to the next one:

1. `setBits`
2. `flipBits`
3. `shiftBack`
4. `clearBits`

For step 4, you'll need to convert the binary number with the 1st and 5th bits set (10001) to decimal.

[bitwise-docs]: https://package.elm-lang.org/packages/elm/core/latest/Bitwise
[bitwise-shiftRightZfBy]: https://package.elm-lang.org/packages/elm/core/latest/Bitwise#shiftRightZfBy
[bitwise-or]: https://package.elm-lang.org/packages/elm/core/latest/Bitwise#or
16 changes: 15 additions & 1 deletion exercises/concept/secrets/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -43,7 +43,7 @@ flipBits 23 157 --> 138

## 4. Clear specific bits

Lastly, there are also certain bits that always decrypt to 0.
There are also certain bits that always decrypt to 0.

Implement the `clearBits` functions that takes a mask and a value.
stewartmurrie marked this conversation as resolved.
Show resolved Hide resolved
The bits in the `value` should be set to 0 where the bit in the mask is 1.
@@ -52,3 +52,17 @@ All other bits should be kept unchanged.
```elm
clearBits 2 15 --> 13
```

## 5. Decrypt a message

Now that you have all the functions you need, you can decode your friend's message.
Implement the `decrypt` function that performs the following operations:

1. Set the bits from the year your friend was born (1996)
2. Flip the result with the year that you first met (2009)
3. Shift the bits back by the number of classes you take together (5)
4. Clear the first and fifth bit.

```elm
decrypt 380182 -->
stewartmurrie marked this conversation as resolved.
Show resolved Hide resolved
```
10 changes: 9 additions & 1 deletion exercises/concept/secrets/.meta/Exemplar.elm
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Secrets exposing (clearBits, flipBits, setBits, shiftBack)
module Secrets exposing (clearBits, flipBits, setBits, shiftBack, decrypt)

import Bitwise

@@ -19,3 +19,11 @@ clearBits mask value =
mask
|> Bitwise.complement
|> Bitwise.and value


decrypt secret =
secret
|> setBits 1996
|> flipBits 2009
|> shiftBack 5
|> clearBits 17
6 changes: 5 additions & 1 deletion exercises/concept/secrets/src/Secrets.elm
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Secrets exposing (clearBits, flipBits, setBits, shiftBack)
module Secrets exposing (clearBits, decrypt, flipBits, setBits, shiftBack)


shiftBack amount value =
@@ -15,3 +15,7 @@ flipBits mask value =

clearBits mask value =
Debug.todo "Please implement clearBits"


decrypt secret =
Debug.todo "Please implement decrypt"
10 changes: 10 additions & 0 deletions exercises/concept/secrets/tests/Tests.elm
stewartmurrie marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -48,4 +48,14 @@ tests =
Secrets.clearBits 240 90
|> Expect.equal 10
]
, describe "5"
[ test "Decrypt 12345" <|
\_ ->
Secrets.decrypt 12345
|> Expect.equal 384
, test "Decrypt 123456789" <|
\_ ->
Secrets.decrypt 123456789
|> Expect.equal 3857984
]
]