Skip to content

Commit 086286b

Browse files
authored
Run configlet sync --docs on exercises` (#2334)
1 parent 75e5ad5 commit 086286b

File tree

108 files changed

+923
-1020
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

108 files changed

+923
-1020
lines changed

exercises/practice/acronym/.docs/instructions.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,14 @@ Convert a phrase to its acronym.
44

55
Techies love their TLA (Three Letter Acronyms)!
66

7-
Help generate some jargon by writing a program that converts a long name
8-
like Portable Network Graphics to its acronym (PNG).
7+
Help generate some jargon by writing a program that converts a long name like Portable Network Graphics to its acronym (PNG).
8+
9+
Punctuation is handled as follows: hyphens are word separators (like whitespace); all other punctuation can be removed from the input.
10+
11+
For example:
12+
13+
| Input | Output |
14+
| ------------------------- | ------ |
15+
| As Soon As Possible | ASAP |
16+
| Liquid-crystal display | LCD |
17+
| Thank George It's Friday! | TGIF |
Lines changed: 53 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,74 @@
11
# Instructions
22

3-
Create an implementation of the affine cipher,
4-
an ancient encryption system created in the Middle East.
3+
Create an implementation of the affine cipher, an ancient encryption system created in the Middle East.
54

65
The affine cipher is a type of monoalphabetic substitution cipher.
7-
Each character is mapped to its numeric equivalent, encrypted with
8-
a mathematical function and then converted to the letter relating to
9-
its new numeric value. Although all monoalphabetic ciphers are weak,
10-
the affine cypher is much stronger than the atbash cipher,
11-
because it has many more keys.
6+
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.
7+
Although all monoalphabetic ciphers are weak, the affine cipher is much stronger than the atbash cipher, because it has many more keys.
8+
9+
[//]: # ' monoalphabetic as spelled by Merriam-Webster, compare to polyalphabetic '
10+
11+
## Encryption
1212

1313
The encryption function is:
1414

15-
`E(x) = (ax + b) mod m`
15+
```text
16+
E(x) = (ai + b) mod m
17+
```
1618

17-
- where `x` is the letter's index from 0 - length of alphabet - 1
18-
- `m` is the length of the alphabet. For the roman alphabet `m == 26`.
19-
- and `a` and `b` make the key
19+
Where:
2020

21-
The decryption function is:
21+
- `i` is the letter's index from `0` to the length of the alphabet - 1
22+
- `m` is the length of the alphabet.
23+
For the Roman alphabet `m` is `26`.
24+
- `a` and `b` are integers which make the encryption key
2225

23-
`D(y) = a^-1(y - b) mod m`
26+
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]).
27+
In case `a` is not coprime to `m`, your program should indicate that this is an error.
28+
Otherwise it should encrypt or decrypt with the provided key.
2429

25-
- where `y` is the numeric value of an encrypted letter, ie. `y = E(x)`
26-
- it is important to note that `a^-1` is the modular multiplicative inverse
27-
of `a mod m`
28-
- the modular multiplicative inverse of `a` only exists if `a` and `m` are
29-
coprime.
30+
For the purpose of this exercise, digits are valid input but they are not encrypted.
31+
Spaces and punctuation characters are excluded.
32+
Ciphertext is written out in groups of fixed length separated by space, the traditional group size being `5` letters.
33+
This is to make it harder to guess encrypted text based on word boundaries.
3034

31-
To find the MMI of `a`:
35+
## Decryption
36+
37+
The decryption function is:
3238

33-
`an mod m = 1`
39+
```text
40+
D(y) = (a^-1)(y - b) mod m
41+
```
3442

35-
- where `n` is the modular multiplicative inverse of `a mod m`
43+
Where:
3644

37-
More information regarding how to find a Modular Multiplicative Inverse
38-
and what it means can be found [here.](https://en.wikipedia.org/wiki/Modular_multiplicative_inverse)
45+
- `y` is the numeric value of an encrypted letter, i.e., `y = E(x)`
46+
- it is important to note that `a^-1` is the modular multiplicative inverse (MMI) of `a mod m`
47+
- the modular multiplicative inverse only exists if `a` and `m` are coprime.
3948

40-
Because automatic decryption fails if `a` is not coprime to `m` your
41-
program should return status 1 and `"Error: a and m must be coprime."`
42-
if they are not. Otherwise it should encode or decode with the
43-
provided key.
49+
The MMI of `a` is `x` such that the remainder after dividing `ax` by `m` is `1`:
4450

45-
The Caesar (shift) cipher is a simple affine cipher where `a` is 1 and
46-
`b` as the magnitude results in a static displacement of the letters.
47-
This is much less secure than a full implementation of the affine cipher.
51+
```text
52+
ax mod m = 1
53+
```
4854

49-
Ciphertext is written out in groups of fixed length, the traditional group
50-
size being 5 letters, and punctuation is excluded. This is to make it
51-
harder to guess things based on word boundaries.
55+
More information regarding how to find a Modular Multiplicative Inverse and what it means can be found in the [related Wikipedia article][mmi].
5256

5357
## General Examples
5458

55-
- Encoding `test` gives `ybty` with the key a=5 b=7
56-
- Decoding `ybty` gives `test` with the key a=5 b=7
57-
- Decoding `ybty` gives `lqul` with the wrong key a=11 b=7
58-
- Decoding `kqlfd jzvgy tpaet icdhm rtwly kqlon ubstx`
59-
- gives `thequickbrownfoxjumpsoverthelazydog` with the key a=19 b=13
60-
- Encoding `test` with the key a=18 b=13
61-
- gives `Error: a and m must be coprime.`
62-
- because a and m are not relatively prime
63-
64-
## Examples of finding a Modular Multiplicative Inverse (MMI)
65-
66-
- simple example:
67-
- `9 mod 26 = 9`
68-
- `9 * 3 mod 26 = 27 mod 26 = 1`
69-
- `3` is the MMI of `9 mod 26`
70-
- a more complicated example:
71-
- `15 mod 26 = 15`
72-
- `15 * 7 mod 26 = 105 mod 26 = 1`
73-
- `7` is the MMI of `15 mod 26`
59+
- Encrypting `"test"` gives `"ybty"` with the key `a = 5`, `b = 7`
60+
- Decrypting `"ybty"` gives `"test"` with the key `a = 5`, `b = 7`
61+
- Decrypting `"ybty"` gives `"lqul"` with the wrong key `a = 11`, `b = 7`
62+
- Decrypting `"kqlfd jzvgy tpaet icdhm rtwly kqlon ubstx"` gives `"thequickbrownfoxjumpsoverthelazydog"` with the key `a = 19`, `b = 13`
63+
- Encrypting `"test"` with the key `a = 18`, `b = 13` is an error because `18` and `26` are not coprime
64+
65+
## Example of finding a Modular Multiplicative Inverse (MMI)
66+
67+
Finding MMI for `a = 15`:
68+
69+
- `(15 * x) mod 26 = 1`
70+
- `(15 * 7) mod 26 = 1`, ie. `105 mod 26 = 1`
71+
- `7` is the MMI of `15 mod 26`
72+
73+
[mmi]: https://en.wikipedia.org/wiki/Modular_multiplicative_inverse
74+
[coprime-integers]: https://en.wikipedia.org/wiki/Coprime_integers

exercises/practice/all-your-base/.docs/instructions.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,32 @@
22

33
Convert a number, represented as a sequence of digits in one base, to any other base.
44

5-
Implement general base conversion. Given a number in base **a**,
6-
represented as a sequence of digits, convert it to base **b**.
5+
Implement general base conversion.
6+
Given a number in base **a**, represented as a sequence of digits, convert it to base **b**.
77

88
## Note
99

1010
- Try to implement the conversion yourself.
1111
Do not use something else to perform the conversion for you.
1212

13-
## About [Positional Notation](https://en.wikipedia.org/wiki/Positional_notation)
13+
## About [Positional Notation][positional-notation]
1414

15-
In positional notation, a number in base **b** can be understood as a linear
16-
combination of powers of **b**.
15+
In positional notation, a number in base **b** can be understood as a linear combination of powers of **b**.
1716

1817
The number 42, _in base 10_, means:
1918

20-
(4 _ 10^1) + (2 _ 10^0)
19+
`(4 * 10^1) + (2 * 10^0)`
2120

2221
The number 101010, _in base 2_, means:
2322

24-
(1 _ 2^5) + (0 _ 2^4) + (1 _ 2^3) + (0 _ 2^2) + (1 _ 2^1) + (0 _ 2^0)
23+
`(1 * 2^5) + (0 * 2^4) + (1 * 2^3) + (0 * 2^2) + (1 * 2^1) + (0 * 2^0)`
2524

2625
The number 1120, _in base 3_, means:
2726

28-
(1 _ 3^3) + (1 _ 3^2) + (2 _ 3^1) + (0 _ 3^0)
27+
`(1 * 3^3) + (1 * 3^2) + (2 * 3^1) + (0 * 3^0)`
2928

3029
I think you got the idea!
3130

3231
_Yes. Those three numbers above are exactly the same. Congratulations!_
32+
33+
[positional-notation]: https://en.wikipedia.org/wiki/Positional_notation

exercises/practice/allergies/.docs/instructions.md

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22

33
Given a person's allergy score, determine whether or not they're allergic to a given item, and their full list of allergies.
44

5-
An allergy test produces a single numeric score which contains the
6-
information about all the allergies the person has (that they were
7-
tested for).
5+
An allergy test produces a single numeric score which contains the information about all the allergies the person has (that they were tested for).
86

97
The list of items (and their value) that were tested are:
108

@@ -24,7 +22,6 @@ Now, given just that score of 34, your program should be able to say:
2422
- Whether Tom is allergic to any one of those allergens listed above.
2523
- All the allergens Tom is allergic to.
2624

27-
Note: a given score may include allergens **not** listed above (i.e.
28-
allergens that score 256, 512, 1024, etc.). Your program should
29-
ignore those components of the score. For example, if the allergy
30-
score is 257, your program should only report the eggs (1) allergy.
25+
Note: a given score may include allergens **not** listed above (i.e. allergens that score 256, 512, 1024, etc.).
26+
Your program should ignore those components of the score.
27+
For example, if the allergy score is 257, your program should only report the eggs (1) allergy.

exercises/practice/alphametics/.docs/instructions.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
Write a function to solve alphametics puzzles.
44

5-
[Alphametics](https://en.wikipedia.org/wiki/Alphametics) is a puzzle where
6-
letters in words are replaced with numbers.
5+
[Alphametics][alphametics] is a puzzle where letters in words are replaced with numbers.
76

87
For example `SEND + MORE = MONEY`:
98

@@ -23,10 +22,10 @@ Replacing these with valid numbers gives:
2322
1 0 6 5 2
2423
```
2524

26-
This is correct because every letter is replaced by a different number and the
27-
words, translated into numbers, then make a valid sum.
25+
This is correct because every letter is replaced by a different number and the words, translated into numbers, then make a valid sum.
2826

29-
Each letter must represent a different digit, and the leading digit of
30-
a multi-digit number must not be zero.
27+
Each letter must represent a different digit, and the leading digit of a multi-digit number must not be zero.
3128

3229
Write a function to solve alphametics puzzles.
30+
31+
[alphametics]: https://en.wikipedia.org/wiki/Alphametics

exercises/practice/armstrong-numbers/.docs/instructions.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Instructions
22

3-
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.
3+
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.
44

55
For example:
66

@@ -10,3 +10,5 @@ For example:
1010
- 154 is _not_ an Armstrong number, because: `154 != 1^3 + 5^3 + 4^3 = 1 + 125 + 64 = 190`
1111

1212
Write some code to determine whether a number is an Armstrong number.
13+
14+
[armstrong-number]: https://en.wikipedia.org/wiki/Narcissistic_number

exercises/practice/atbash-cipher/.docs/instructions.md

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@
22

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

5-
The Atbash cipher is a simple substitution cipher that relies on
6-
transposing all the letters in the alphabet such that the resulting
7-
alphabet is backwards. The first letter is replaced with the last
8-
letter, the second with the second-last, and so on.
5+
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.
6+
The first letter is replaced with the last letter, the second with the second-last, and so on.
97

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

@@ -14,16 +12,16 @@ Plain: abcdefghijklmnopqrstuvwxyz
1412
Cipher: zyxwvutsrqponmlkjihgfedcba
1513
```
1614

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

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

2522
## Examples
2623

2724
- Encoding `test` gives `gvhg`
25+
- Encoding `x123 yes` gives `c123b vh`
2826
- Decoding `gvhg` gives `test`
2927
- Decoding `gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt` gives `thequickbrownfoxjumpsoverthelazydog`
Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,12 @@
11
# Instructions
22

3-
Simulate a bank account supporting opening/closing, withdrawals, and deposits
4-
of money. Watch out for concurrent transactions!
3+
Simulate a bank account supporting opening/closing, withdrawals, and deposits of money.
4+
Watch out for concurrent transactions!
55

6-
A bank account can be accessed in multiple ways. Clients can make
7-
deposits and withdrawals using the internet, mobile phones, etc. Shops
8-
can charge against the account.
6+
A bank account can be accessed in multiple ways.
7+
Clients can make deposits and withdrawals using the internet, mobile phones, etc.
8+
Shops can charge against the account.
99

10-
Create an account that can be accessed from multiple threads/processes
11-
(terminology depends on your programming language).
10+
Create an account that can be accessed from multiple threads/processes (terminology depends on your programming language).
1211

13-
It should be possible to close an account; operations against a closed
14-
account must fail.
15-
16-
## Instructions
17-
18-
Run the test file, and fix each of the errors in turn. When you get the
19-
first test to pass, go to the first pending or skipped test, and make
20-
that pass as well. When all of the tests are passing, feel free to
21-
submit.
22-
23-
Remember that passing code is just the first step. The goal is to work
24-
towards a solution that is as readable and expressive as you can make
25-
it.
26-
27-
Have fun!
12+
It should be possible to close an account; operations against a closed account must fail.

exercises/practice/beer-song/.docs/instructions.md

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -305,17 +305,3 @@ Take it down and pass it around, no more bottles of beer on the wall.
305305
No more bottles of beer on the wall, no more bottles of beer.
306306
Go to the store and buy some more, 99 bottles of beer on the wall.
307307
```
308-
309-
## For bonus points
310-
311-
Did you get the tests passing and the code clean? If you want to, these
312-
are some additional things you could try:
313-
314-
- Remove as much duplication as you possibly can.
315-
- Optimize for readability, even if it means introducing duplication.
316-
- If you've removed all the duplication, do you have a lot of
317-
conditionals? Try replacing the conditionals with polymorphism, if it
318-
applies in this language. How readable is it?
319-
320-
Then please share your thoughts in a comment on the submission. Did this
321-
experiment make the code better? Worse? Did you learn anything from it?

exercises/practice/binary-search-tree/.docs/instructions.md

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,22 @@
22

33
Insert and search for numbers in a binary tree.
44

5-
When we need to represent sorted data, an array does not make a good
6-
data structure.
7-
8-
Say we have the array `[1, 3, 4, 5]`, and we add 2 to it so it becomes
9-
`[1, 3, 4, 5, 2]` now we must sort the entire array again! We can
10-
improve on this by realizing that we only need to make space for the new
11-
item `[1, nil, 3, 4, 5]`, and then adding the item in the space we
12-
added. But this still requires us to shift many elements down by one.
13-
14-
Binary Search Trees, however, can operate on sorted data much more
15-
efficiently.
16-
17-
A binary search tree consists of a series of connected nodes. Each node
18-
contains a piece of data (e.g. the number 3), a variable named `left`,
19-
and a variable named `right`. The `left` and `right` variables point at
20-
`nil`, or other nodes. Since these other nodes in turn have other nodes
21-
beneath them, we say that the left and right variables are pointing at
22-
subtrees. All data in the left subtree is less than or equal to the
23-
current node's data, and all data in the right subtree is greater than
24-
the current node's data.
25-
26-
For example, if we had a node containing the data 4, and we added the
27-
data 2, our tree would look like this:
5+
When we need to represent sorted data, an array does not make a good data structure.
6+
7+
Say we have the array `[1, 3, 4, 5]`, and we add 2 to it so it becomes `[1, 3, 4, 5, 2]`.
8+
Now we must sort the entire array again!
9+
We can improve on this by realizing that we only need to make space for the new item `[1, nil, 3, 4, 5]`, and then adding the item in the space we added.
10+
But this still requires us to shift many elements down by one.
11+
12+
Binary Search Trees, however, can operate on sorted data much more efficiently.
13+
14+
A binary search tree consists of a series of connected nodes.
15+
Each node contains a piece of data (e.g. the number 3), a variable named `left`, and a variable named `right`.
16+
The `left` and `right` variables point at `nil`, or other nodes.
17+
Since these other nodes in turn have other nodes beneath them, we say that the left and right variables are pointing at subtrees.
18+
All data in the left subtree is less than or equal to the current node's data, and all data in the right subtree is greater than the current node's data.
19+
20+
For example, if we had a node containing the data 4, and we added the data 2, our tree would look like this:
2821

2922
4
3023
/

0 commit comments

Comments
 (0)