A grille cipher was a technique for encrypting plaintext by writing it onto a sheet of paper through a pierced sheet. The earliest known use is from the mathematician Girolamo Cardano in 1550. His proposal was for a rectangular stencil allowing single letters, syllables, or words to be written, then later read. The written fragments of the plaintext could be further disguised by filling the gaps between the fragments with benign words or letters.
Wikipedia: https://en.wikipedia.org/wiki/Grille_(cryptography)
Write a function that accepts two inputs: message
and code
. Code is a non-negative integer and should be converted to binary. Overlay the code converted to binary and the message to reveal the result.
Example
Grille("abcdef", 5) => "df"
* convert 5 to binary:
000101
* overlay message and code:
message : abcdef
code : 000101
----------------
result : df
Tests
Grille("0abc", 2)
Grille("abcde", 32)
Grille("abcd", 1)
Good luck!
This challenge comes from dcieslak on CodeWars. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!
Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!
Top comments (8)
JS
This is the solution in PHP, but I'm not sure if it's 100% correct since there aren't enough test cases and the test cases provided above don't have the result so they're pretty much useless.
anyway, here's my attempt:
Here is the simple solution with Python:
Python 3-liner with test cases and TIO link.
Apart from some type conversions, itertools.compress does the job.
Try it online!
Scala solution
ruby
Another JavaScript solution, using binary operations:
JS solution