Skip to content

Commit

Permalink
Implement atbash-cipher in jq
Browse files Browse the repository at this point in the history
  • Loading branch information
Sgoettschkes committed Nov 23, 2023
1 parent 27e2cca commit bb96414
Show file tree
Hide file tree
Showing 5 changed files with 1,068 additions and 0 deletions.
104 changes: 104 additions & 0 deletions jq/atbash-cipher/HELP.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
# Help

## Running the tests

Each exercise contains a test file.
Run the tests using the `bats` program.

```bash
bats test-hello-world.bats
```

`bats` will need to be installed.
See the [Testing on the Bash track][bash] page for instructions to install `bats` for your system.

### bats is implemented in bash

The bats file is a bash script, with some special functions recognized by the `bats` command.
You'll see some tests that look like

```sh
jq -f some-exercise.jq <<< "{some,json,here}"
```

That `<<<` syntax is a bash [Here String][here-string].
It sends the string on the right-hand side into the standard input of the program on the left-hand side.
It is ([approximately][so]) the same as

```sh
echo "{some,json,here}" | jq -f some-exercise.jq
```

## Help for assert functions

The tests use functions from the [bats-assert][bats-assert] library.
Help for the various `assert*` functions can be found there.

## Skipped tests

Solving an exercise means making all its tests pass.
By default, only one test (the first one) is executed when you run the tests.
This is intentional, as it allows you to focus on just making that one test pass.
Once it passes, you can enable the next test by commenting out or removing the

[[ $BATS_RUN_SKIPPED == true ]] || skip

annotations prepending other tests.

## Overriding skips

To run all tests, including the ones with `skip` annotations, you can run:

```bash
BATS_RUN_SKIPPED=true bats test-some-exercise.bats
```

It can be convenient to use a wrapper function to save on typing: in `bash` you can do:

```bash
bats() {
BATS_RUN_SKIPPED=true command bats *.bats
}
```

Then run tests with just:

```bash
bats
```

[bash]: https://exercism.org/docs/tracks/bash/tests
[bats-assert]: https://github.com/bats-core/bats-assert
[here-string]: https://www.gnu.org/software/bash/manual/bash.html#Here-Strings
[so]: https://unix.stackexchange.com/a/80372/4667

## Submitting your solution

You can submit your solution using the `exercism submit atbash-cipher.jq` command.
This command will upload your solution to the Exercism website and print the solution page's URL.

It's possible to submit an incomplete solution which allows you to:

- See how others have completed the exercise
- Request help from a mentor

## Need to get help?

If you'd like help solving the exercise, check the following pages:

- The [jq track's documentation](https://exercism.org/docs/tracks/jq)
- The [jq track's programming category on the forum](https://forum.exercism.org/c/programming/jq)
- [Exercism's programming category on the forum](https://forum.exercism.org/c/programming/5)
- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)

Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.

Need help?

- Go to the [Exercism Community forum](https://forum.exercism.org) to get support and ask questions (or just chat!)
- Use the [Exercism Support](https://forum.exercism.org/c/support/8) category if you face any issues with working in the web editor, or downloading or submitting your exercises locally.
- Use the [Programming:jq](https://forum.exercism.org/c/programming/jq/133) category for jq-specific topics.
- [StackOverflow](https://stackoverflow.com/questions/tagged/jq) can be used to search for your problem and see if it has been answered already.
You can also ask and answer questions.
- [Github issue tracker](https://github.com/exercism/jq/issues) is where we track our development and maintainance of `jq` exercises in exercism.
If none of the above links help you, feel free to post an issue here.
42 changes: 42 additions & 0 deletions jq/atbash-cipher/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Atbash Cipher

Welcome to Atbash Cipher on Exercism's jq Track.
If you need help running the tests or submitting your code, check out `HELP.md`.

## Instructions

Create an implementation of the atbash cipher, an ancient encryption system created in the Middle East.

The Atbash cipher is a simple substitution cipher that relies on transposing all the letters in the alphabet such that the resulting alphabet is backwards.
The first letter is replaced with the last letter, the second with the second-last, and so on.

An Atbash cipher for the Latin alphabet would be as follows:

```text
Plain: abcdefghijklmnopqrstuvwxyz
Cipher: zyxwvutsrqponmlkjihgfedcba
```

It is a very weak cipher because it only has one possible key, and it is a simple mono-alphabetic substitution cipher.
However, this may not have been an issue in the cipher's time.

Ciphertext is written out in groups of fixed length, the traditional group size being 5 letters, leaving numbers unchanged, and punctuation is excluded.
This is to make it harder to guess things based on word boundaries.
All text will be encoded as lowercase letters.

## Examples

- Encoding `test` gives `gvhg`
- Encoding `x123 yes` gives `c123b vh`
- Decoding `gvhg` gives `test`
- Decoding `gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt` gives `thequickbrownfoxjumpsoverthelazydog`

## Source

### Created by

- @glennj

### Based on

Wikipedia - https://en.wikipedia.org/wiki/Atbash
43 changes: 43 additions & 0 deletions jq/atbash-cipher/atbash-cipher.jq
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
def chunk:
if length <= 5 then
.
else
"\(.[:5]) \(.[5:] | chunk)"
end
;

if .property == "encode" then
.input.phrase |
ascii_downcase |
explode |
map(
select(
(. >= 48 and . <= 57) or (. >= 97 and . <= 122)
)
) |
map(
if . >= 97 then
122-(.-97)
else
.
end
) |
implode |
chunk
else
.input.phrase |
explode |
map(
select(
(. >= 48 and . <= 57) or (. >= 97 and . <= 122)
)
) |
map(
if . >= 97 then
122-(.-97)
else
.
end
) |
implode
end
Loading

0 comments on commit bb96414

Please sign in to comment.