-
-
Notifications
You must be signed in to change notification settings - Fork 194
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
71 changed files
with
761 additions
and
768 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
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,70 +1,74 @@ | ||
# Instructions | ||
|
||
Create an implementation of the affine cipher, | ||
an ancient encryption system created in the Middle East. | ||
Create an implementation of the affine cipher, an ancient encryption system created in the Middle East. | ||
|
||
The affine cipher is a type of monoalphabetic substitution cipher. | ||
Each character is mapped to its numeric equivalent, encrypted with | ||
a mathematical function and then converted to the letter relating to | ||
its new numeric value. Although all monoalphabetic ciphers are weak, | ||
the affine cypher is much stronger than the atbash cipher, | ||
because it has many more keys. | ||
Each character is mapped to its numeric equivalent, encrypted with a mathematical function and then converted to the letter relating to its new numeric value. | ||
Although all monoalphabetic ciphers are weak, the affine cipher is much stronger than the atbash cipher, because it has many more keys. | ||
|
||
[//]: # " monoalphabetic as spelled by Merriam-Webster, compare to polyalphabetic " | ||
|
||
## Encryption | ||
|
||
The encryption function is: | ||
|
||
`E(x) = (ax + b) mod m` | ||
- where `x` is the letter's index from 0 - length of alphabet - 1 | ||
- `m` is the length of the alphabet. For the roman alphabet `m == 26`. | ||
- and `a` and `b` make the key | ||
```text | ||
E(x) = (ai + b) mod m | ||
``` | ||
|
||
The decryption function is: | ||
Where: | ||
|
||
- `i` is the letter's index from `0` to the length of the alphabet - 1 | ||
- `m` is the length of the alphabet. | ||
For the Roman alphabet `m` is `26`. | ||
- `a` and `b` are integers which make the encryption key | ||
|
||
Values `a` and `m` must be _coprime_ (or, _relatively prime_) for automatic decryption to succeed, i.e., they have number `1` as their only common factor (more information can be found in the [Wikipedia article about coprime integers][coprime-integers]). | ||
In case `a` is not coprime to `m`, your program should indicate that this is an error. | ||
Otherwise it should encrypt or decrypt with the provided key. | ||
|
||
For the purpose of this exercise, digits are valid input but they are not encrypted. | ||
Spaces and punctuation characters are excluded. | ||
Ciphertext is written out in groups of fixed length separated by space, the traditional group size being `5` letters. | ||
This is to make it harder to guess encrypted text based on word boundaries. | ||
|
||
`D(y) = a^-1(y - b) mod m` | ||
- where `y` is the numeric value of an encrypted letter, ie. `y = E(x)` | ||
- it is important to note that `a^-1` is the modular multiplicative inverse | ||
of `a mod m` | ||
- the modular multiplicative inverse of `a` only exists if `a` and `m` are | ||
coprime. | ||
## Decryption | ||
|
||
To find the MMI of `a`: | ||
The decryption function is: | ||
|
||
```text | ||
D(y) = (a^-1)(y - b) mod m | ||
``` | ||
|
||
`an mod m = 1` | ||
- where `n` is the modular multiplicative inverse of `a mod m` | ||
Where: | ||
|
||
More information regarding how to find a Modular Multiplicative Inverse | ||
and what it means can be found [here.](https://en.wikipedia.org/wiki/Modular_multiplicative_inverse) | ||
- `y` is the numeric value of an encrypted letter, i.e., `y = E(x)` | ||
- it is important to note that `a^-1` is the modular multiplicative inverse (MMI) of `a mod m` | ||
- the modular multiplicative inverse only exists if `a` and `m` are coprime. | ||
|
||
Because automatic decryption fails if `a` is not coprime to `m` your | ||
program should return status 1 and `"Error: a and m must be coprime."` | ||
if they are not. Otherwise it should encode or decode with the | ||
provided key. | ||
The MMI of `a` is `x` such that the remainder after dividing `ax` by `m` is `1`: | ||
|
||
The Caesar (shift) cipher is a simple affine cipher where `a` is 1 and | ||
`b` as the magnitude results in a static displacement of the letters. | ||
This is much less secure than a full implementation of the affine cipher. | ||
```text | ||
ax mod m = 1 | ||
``` | ||
|
||
Ciphertext is written out in groups of fixed length, the traditional group | ||
size being 5 letters, and punctuation is excluded. This is to make it | ||
harder to guess things based on word boundaries. | ||
More information regarding how to find a Modular Multiplicative Inverse and what it means can be found in the [related Wikipedia article][mmi]. | ||
|
||
## General Examples | ||
|
||
- Encoding `test` gives `ybty` with the key a=5 b=7 | ||
- Decoding `ybty` gives `test` with the key a=5 b=7 | ||
- Decoding `ybty` gives `lqul` with the wrong key a=11 b=7 | ||
- Decoding `kqlfd jzvgy tpaet icdhm rtwly kqlon ubstx` | ||
- gives `thequickbrownfoxjumpsoverthelazydog` with the key a=19 b=13 | ||
- Encoding `test` with the key a=18 b=13 | ||
- gives `Error: a and m must be coprime.` | ||
- because a and m are not relatively prime | ||
|
||
## Examples of finding a Modular Multiplicative Inverse (MMI) | ||
|
||
- simple example: | ||
- `9 mod 26 = 9` | ||
- `9 * 3 mod 26 = 27 mod 26 = 1` | ||
- `3` is the MMI of `9 mod 26` | ||
- a more complicated example: | ||
- `15 mod 26 = 15` | ||
- `15 * 7 mod 26 = 105 mod 26 = 1` | ||
- `7` is the MMI of `15 mod 26` | ||
- Encrypting `"test"` gives `"ybty"` with the key `a = 5`, `b = 7` | ||
- Decrypting `"ybty"` gives `"test"` with the key `a = 5`, `b = 7` | ||
- Decrypting `"ybty"` gives `"lqul"` with the wrong key `a = 11`, `b = 7` | ||
- Decrypting `"kqlfd jzvgy tpaet icdhm rtwly kqlon ubstx"` gives `"thequickbrownfoxjumpsoverthelazydog"` with the key `a = 19`, `b = 13` | ||
- Encrypting `"test"` with the key `a = 18`, `b = 13` is an error because `18` and `26` are not coprime | ||
|
||
## Example of finding a Modular Multiplicative Inverse (MMI) | ||
|
||
Finding MMI for `a = 15`: | ||
|
||
- `(15 * x) mod 26 = 1` | ||
- `(15 * 7) mod 26 = 1`, ie. `105 mod 26 = 1` | ||
- `7` is the MMI of `15 mod 26` | ||
|
||
[mmi]: https://en.wikipedia.org/wiki/Modular_multiplicative_inverse | ||
[coprime-integers]: https://en.wikipedia.org/wiki/Coprime_integers |
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
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
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,8 +1,13 @@ | ||
# Instructions | ||
|
||
An anagram is a rearrangement of letters to form a new word. | ||
Given a word and a list of candidates, select the sublist of anagrams of the given word. | ||
An anagram is a rearrangement of letters to form a new word: for example `"owns"` is an anagram of `"snow"`. | ||
A word is not its own anagram: for example, `"stop"` is not an anagram of `"stop"`. | ||
|
||
Given `"listen"` and a list of candidates like `"enlists" "google" | ||
"inlets" "banana"` the program should return a list containing | ||
`"inlets"`. | ||
Given a target word and a set of candidate words, this exercise requests the anagram set: the subset of the candidates that are anagrams of the target. | ||
|
||
The target and candidates are words of one or more ASCII alphabetic characters (`A`-`Z` and `a`-`z`). | ||
Lowercase and uppercase characters are equivalent: for example, `"PoTS"` is an anagram of `"sTOp"`, but `StoP` is not an anagram of `sTOp`. | ||
The anagram set is the subset of the candidate set that are anagrams of the target (in any order). | ||
Words in the anagram set should have the same letter case as in the candidate set. | ||
|
||
Given the target `"stone"` and candidates `"stone"`, `"tones"`, `"banana"`, `"tons"`, `"notes"`, `"Seton"`, the anagram set is `"tones"`, `"notes"`, `"Seton"`. |
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,12 +1,14 @@ | ||
# Instructions | ||
|
||
An [Armstrong number](https://en.wikipedia.org/wiki/Narcissistic_number) is a number that is the sum of its own digits each raised to the power of the number of digits. | ||
An [Armstrong number][armstrong-number] is a number that is the sum of its own digits each raised to the power of the number of digits. | ||
|
||
For example: | ||
|
||
- 9 is an Armstrong number, because `9 = 9^1 = 9` | ||
- 10 is *not* an Armstrong number, because `10 != 1^2 + 0^2 = 1` | ||
- 10 is _not_ an Armstrong number, because `10 != 1^2 + 0^2 = 1` | ||
- 153 is an Armstrong number, because: `153 = 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153` | ||
- 154 is *not* an Armstrong number, because: `154 != 1^3 + 5^3 + 4^3 = 1 + 125 + 64 = 190` | ||
- 154 is _not_ an Armstrong number, because: `154 != 1^3 + 5^3 + 4^3 = 1 + 125 + 64 = 190` | ||
|
||
Write some code to determine whether a number is an Armstrong number. | ||
|
||
[armstrong-number]: https://en.wikipedia.org/wiki/Narcissistic_number |
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
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,27 +1,12 @@ | ||
# Instructions | ||
|
||
Simulate a bank account supporting opening/closing, withdrawals, and deposits | ||
of money. Watch out for concurrent transactions! | ||
Simulate a bank account supporting opening/closing, withdrawals, and deposits of money. | ||
Watch out for concurrent transactions! | ||
|
||
A bank account can be accessed in multiple ways. Clients can make | ||
deposits and withdrawals using the internet, mobile phones, etc. Shops | ||
can charge against the account. | ||
A bank account can be accessed in multiple ways. | ||
Clients can make deposits and withdrawals using the internet, mobile phones, etc. | ||
Shops can charge against the account. | ||
|
||
Create an account that can be accessed from multiple threads/processes | ||
(terminology depends on your programming language). | ||
Create an account that can be accessed from multiple threads/processes (terminology depends on your programming language). | ||
|
||
It should be possible to close an account; operations against a closed | ||
account must fail. | ||
|
||
## Instructions | ||
|
||
Run the test file, and fix each of the errors in turn. When you get the | ||
first test to pass, go to the first pending or skipped test, and make | ||
that pass as well. When all of the tests are passing, feel free to | ||
submit. | ||
|
||
Remember that passing code is just the first step. The goal is to work | ||
towards a solution that is as readable and expressive as you can make | ||
it. | ||
|
||
Have fun! | ||
It should be possible to close an account; operations against a closed account must fail. |
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
Oops, something went wrong.