Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
169 changes: 169 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -299,10 +299,91 @@
],
"difficulty": 4
},
{
"slug": "luhn",
"name": "Luhn",
"uuid": "e83e333e-612a-4ec8-8d9f-da387327acdc",
"practices": [],
"prerequisites": [],
"difficulty": 4
},
{
"slug": "robot-simulator",
"name": "Robot Simulator",
"uuid": "69868e6a-7e87-4cff-8f36-dd24c042e879",
"practices": [],
"prerequisites": [],
"difficulty": 4
},
{
"slug": "sieve",
"name": "Sieve",
"uuid": "8b1fc515-71c2-436c-98dc-8789d25c7ddb",
"practices": [],
"prerequisites": [],
"difficulty": 4
},
{
"slug": "space-age",
"name": "Space Age",
"uuid": "92fc6937-1b33-49f1-b66e-7ec7e3786906",
"practices": [],
"prerequisites": [],
"difficulty": 4
},
{
"slug": "word-count",
"name": "Word Count",
"uuid": "f0a2b8f5-b244-4c35-86a7-9bf86843093c",
"practices": [],
"prerequisites": [],
"difficulty": 4
},
{
"slug": "accumulate",
"name": "Accumulate",
"uuid": "8a373305-98c9-4b19-9011-085abe19cfed",
"practices": [],
"prerequisites": [],
"difficulty": 5,
"status": "deprecated"
},
{
"slug": "affine-cipher",
"name": "Affine Cipher",
"uuid": "3e018380-7d24-403e-a951-b6f33ccc8afd",
"practices": [],
"prerequisites": [],
"difficulty": 5
},
{
"slug": "anagram",
"name": "Anagram",
"uuid": "ec107dbf-5123-4405-934e-3e0846433fd0",
"practices": [],
"prerequisites": [],
"difficulty": 5
},
{
"slug": "clock",
"name": "Clock",
"uuid": "2a606909-644b-4fd3-bd6c-bf0bb605a48d",
"practices": [],
"prerequisites": [],
"difficulty": 5
},
{
"slug": "crypto-square",
"name": "Crypto Square",
"uuid": "c4b23be2-92e7-40ee-ad93-19f9f79eebc9",
"practices": [],
"prerequisites": [],
"difficulty": 5
},
{
"slug": "list-ops",
"name": "List Ops",
"uuid": "9eb1ed19-7c5a-4c8f-98e3-7d99826deb18",
"practices": [
"quotations",
"collections",
Expand All @@ -315,6 +396,38 @@
],
"difficulty": 5
},
{
"slug": "matching-brackets",
"name": "Matching Brackets",
"uuid": "afef364d-12dd-42fd-bbc8-f2c6228f0f53",
"practices": [],
"prerequisites": [],
"difficulty": 5
},
{
"slug": "run-length-encoding",
"name": "Run-Length Encoding",
"uuid": "009090ef-d7fd-4ba5-9942-c6e686069e12",
"practices": [],
"prerequisites": [],
"difficulty": 5
},
{
"slug": "square-root",
"name": "Square Root",
"uuid": "53cef38b-ad56-4462-bf37-48902fd3745c",
"practices": [],
"prerequisites": [],
"difficulty": 5
},
{
"slug": "yacht",
"name": "Yacht",
"uuid": "c94bbdbb-cdfe-4b53-913c-fc7f705cc0d0",
"practices": [],
"prerequisites": [],
"difficulty": 5
},
{
"slug": "all-your-base",
"name": "All Your Base",
Expand All @@ -336,6 +449,62 @@
"topics": [
"math"
]
},
{
"slug": "game-of-life",
"name": "Conway's Game of Life",
"uuid": "36771f7d-aee3-44bd-a718-60efeaca3a8a",
"practices": [],
"prerequisites": [],
"difficulty": 6
},
{
"slug": "ocr-numbers",
"name": "OCR Numbers",
"uuid": "725a35a9-b2ae-4c71-8dc9-a801a8699bd2",
"practices": [],
"prerequisites": [],
"difficulty": 6
},
{
"slug": "rail-fence-cipher",
"name": "Rail Fence Cipher",
"uuid": "7f404c18-968e-41de-8067-7be5596a87ef",
"practices": [],
"prerequisites": [],
"difficulty": 6
},
{
"slug": "say",
"name": "Say",
"uuid": "613a30e5-d545-40c7-b41e-f5ccb2c4ec88",
"practices": [],
"prerequisites": [],
"difficulty": 6
},
{
"slug": "dominoes",
"name": "Dominoes",
"uuid": "cbbd1c5f-587b-4274-9889-1332d9446c63",
"practices": [],
"prerequisites": [],
"difficulty": 7
},
{
"slug": "perfect-numbers",
"name": "Perfect Numbers",
"uuid": "0420eb1a-c607-4a65-8acb-aa3f60c01302",
"practices": [],
"prerequisites": [],
"difficulty": 7
},
{
"slug": "rectangles",
"name": "Rectangles",
"uuid": "bfcd7856-38df-48e5-9bf1-438962ac44f8",
"practices": [],
"prerequisites": [],
"difficulty": 7
}
]
},
Expand Down
12 changes: 2 additions & 10 deletions exercises/practice/accumulate/.meta/example.factor
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
USING: arrays kernel locals sequences sequences.extras ;
USING: kernel locals make sequences ;
IN: accumulate

:: accum ( seq quot: ( x -- y ) -- newseq )
seq >resizable :> seq
seq length seq new-resizable :> newseq

[ seq empty? ] [
seq pop quot call
newseq push
] until

newseq reverse >array ; inline
[ seq [ quot call , ] each ] { } make ; inline
2 changes: 1 addition & 1 deletion exercises/practice/accumulate/accumulate/accumulate.factor
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ USING: kernel ;
IN: accumulate

:: accum ( seq quot: ( x -- y ) -- newseq )
"unimplemented" throw ;
"unimplemented" throw ; inline
74 changes: 74 additions & 0 deletions exercises/practice/affine-cipher/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# Instructions

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 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:

```text
E(x) = (ai + b) mod m
```

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 Latin alphabet `m` is `26`.
- `a` and `b` are integers which make up 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.

## Decryption

The decryption function is:

```text
D(y) = (a^-1)(y - b) mod m
```

Where:

- `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.

The MMI of `a` is `x` such that the remainder after dividing `ax` by `m` is `1`:

```text
ax mod m = 1
```

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

- 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
19 changes: 19 additions & 0 deletions exercises/practice/affine-cipher/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"keiravillekode"
],
"files": {
"solution": [
"affine-cipher/affine-cipher.factor"
],
"test": [
"affine-cipher/affine-cipher-tests.factor"
],
"example": [
".meta/example.factor"
]
},
"blurb": "Create an implementation of the Affine cipher, an ancient encryption algorithm from the Middle East.",
"source": "Wikipedia",
"source_url": "https://en.wikipedia.org/wiki/Affine_cipher"
}
30 changes: 30 additions & 0 deletions exercises/practice/affine-cipher/.meta/example.factor
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
USING: combinators grouping kernel locals math math.functions
ranges sequences sets strings unicode ;
IN: affine-cipher

: check-coprime ( a -- a )
dup 26 gcd nip 1 = [ "a and m must be coprime." throw ] unless ;

: mmi ( a -- a^-1 )
26 [1..b] [ over * 26 mod 1 = ] find nip nip ;

: group5 ( str -- str ) 5 group " " join ;

:: encode ( phrase a b -- cipher )
a check-coprime drop
phrase >lower
[ [ Letter? ] [ digit? ] bi or ] filter
[| ch |
ch digit?
[ ch ]
[ ch CHAR: a - a * b + 26 mod CHAR: a + ] if
] map group5 ;

:: decode ( phrase a b -- plain )
a check-coprime mmi :> a-inv
phrase " " without
[| ch |
ch digit?
[ ch ]
[ ch CHAR: a - b - a-inv * 26 mod dup 0 < [ 26 + ] when CHAR: a + ] if
] map ;
17 changes: 17 additions & 0 deletions exercises/practice/affine-cipher/.meta/generator.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module AffineCipher

function gen_test_case(case)
phrase = escape_factor(case["input"]["phrase"])
a = Int(case["input"]["key"]["a"])
b = Int(case["input"]["key"]["b"])
prop = case["property"]
expected = case["expected"]
if expected isa Dict
msg = expected["error"]
return """[ "$(phrase)" $(a) $(b) $(prop) ]\n[ "$(msg)" = ] must-fail-with"""
else
return """{ "$(expected)" }\n[ "$(phrase)" $(a) $(b) $(prop) ] unit-test"""
end
end

end
8 changes: 8 additions & 0 deletions exercises/practice/affine-cipher/.meta/supplements.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[
{
"description": "encode boundary characters",
"property": "encode",
"input": {"phrase": "/09:@AMNZ[`amnz{", "key": {"a": 25, "b": 12}},
"expected": "09maz nmazn"
}
]
Loading