Skip to content

Commit 2f190cd

Browse files
meatball133BethanyG
authored andcommitted
Updated formating
1 parent b6185ec commit 2f190cd

File tree

6 files changed

+13
-28
lines changed

6 files changed

+13
-28
lines changed

exercises/practice/rotational-cipher/.approaches/alphabet/content.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ The result string is returned once the loop finishes.
3333

3434
If only English letters are needed, the constant [`string.ascii_lowercase`][ascii_lowercase] can be imported from the [`string`][string] module.
3535

36-
3736
```python
3837
from string import ascii_lowercase
3938

exercises/practice/rotational-cipher/.approaches/ascii-values/content.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,14 @@ It uses numbers to represent 128 different entities including carriage returns,
2222
In ascii, all the lowercase English letters appear between 97 and 123.
2323
While the uppercase letters are in the range between 65 and 91.
2424

25-
~~~~exercism/caution
25+
```exercism/caution
2626
2727
This approach only supports the English alphabet.
2828
Non-English alphabets are not contiguous in their ascii number ranges, and are not consistently defined across platforms.
2929
For example, the Scandinavian letter: **å** has the extended ascii value of 132, but is used in combination with Latin characters that appear in the 65-91 and 97-123 ranges.
3030
This means that a shift for an extended ascii word containing **å** won't result in an accurate alphabet position for a Scandinavian language.
3131
32-
~~~~
33-
32+
```
3433

3534
The approach starts with defining the function `rotate()`, with a variable `result` is assigned to an empty string.
3635
The elements of the text argument are then iterated over using a [`for loop`][for-loop].
@@ -41,7 +40,7 @@ Unicode's first 128 code points have the same numbers as their ascii counterpart
4140

4241
If the element is an uppercase letter, [`ord`][ord] is used to convert the letter to an integer.
4342
The integer is added to the numeric key and then 65 is subtracted from the total.
44-
Finally, the result is [modulo (`%`)][modulo] 26 (_to put the value within the 2) and 65 is added back.
43+
Finally, the result is [modulo (`%`)][modulo] 26 (_to put the value within the 2_) and 65 is added back.
4544

4645
This is because we want to know which letter of the alphabet the number will become.
4746
And if the new number is over 26 we want to make sure that it "wraps around" to remain in the range of 0-26.

exercises/practice/rotational-cipher/.approaches/introduction.md

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
There are various ways to solve `rotational-cipher`.
44
You can for example use [ascii values][ascii], an alphabet `str` or `list`, recursion, or `str.translate`.
55

6-
76
## General guidance
87

98
The goal of this exercise is to shift the letters in a string by a given integer key between 0 and 26.
@@ -15,16 +14,14 @@ This approach is straightforward to understand.
1514
It uses the ascii value of the letters to rotate them within the message.
1615
The numbers 65-91 in the ascii range represent lowercase Latin letters, while 97-123 represent uppercase Latin letters.
1716

18-
19-
~~~~exercism/caution
17+
```exercism/caution
2018
2119
This approach only supports the English alphabet.
2220
Non-English alphabets are not contiguous in their ascii number ranges, and are not consistently defined across platforms.
2321
For example, the Scandinavian letter: **å** has the extended ascii value of 132, but is used in combination with Latin characters that appear in the 65-91 and 97-123 ranges.
2422
This means that a shift for an extended ascii word containing **å** won't result in an accurate alphabet position for a Scandinavian language.
2523
26-
~~~~
27-
24+
```
2825

2926
```python
3027
def rotate(text, key):
@@ -42,7 +39,6 @@ def rotate(text, key):
4239

4340
For more information, check the [ascii values approach][approach-ascii-values].
4441

45-
4642
## Approach: Alphabet
4743

4844
This approach is similar to the ascii one, but it uses the index number of each letter in an alphabet string.
@@ -53,7 +49,6 @@ The big advantage of this approach is the ability to use any alphabet (_although
5349
Here, if we want to use the scandinavian letter: **å**, we can simply insert it into our string where we want it:
5450
`abcdefghijklmnopqrstuvwxyzå` and the rotation will work correctly.
5551

56-
5752
```python
5853
# This only uses English characters
5954
AlPHABET = "abcdefghijklmnopqrstuvwxyz"
@@ -73,16 +68,14 @@ def rotate(text, key):
7368

7469
For more information, check the [Alphabet approach][approach-alphabet].
7570

76-
7771
## Approach: Str translate
7872

7973
This approach uses the [`str.translate`][str-translate] method to create a mapping from input to shifted string instead of using the index of an alphabet string to calculate the shift.
8074
The benefit of this approach is that it has no visible loop, making the code more concise.
8175

82-
~~~~exercism/note
76+
```exercism/note
8377
`str.translate` **still loops over the `string`** even if it is not visibly doing so.
84-
~~~~
85-
78+
```
8679

8780
```python
8881
AlPHABET = "abcdefghijklmnopqrstuvwxyz
@@ -94,19 +87,16 @@ def rotate(text, key):
9487

9588
For more information, check the [Str translate approach][approach-str-translate].
9689

97-
9890
## Approach: Recursion
9991

10092
This approach uses a recursive function.
10193
A recursive function is a function that calls itself.
10294
This approach can be more concise than other approaches, and may also be more readable for some audiences.
10395

104-
105-
~~~~exercism/caution
96+
```exercism/caution
10697
Python does not have any tail-call optimization and has a default [recursion limit][recursion-limit] of 1000 calls on the stack.
10798
Calculate your base case carefully to avoid errors.
108-
~~~~
109-
99+
```
110100

111101
```python
112102
AlPHABET = "abcdefghijklmnopqrstuvwxyz"
@@ -126,7 +116,6 @@ def rotate(text, key):
126116

127117
For more information, check the [Recursion approach][approach-recursion].
128118

129-
130119
## Benchmark
131120

132121
For more information, check the [Performance article][article-performance].

exercises/practice/rotational-cipher/.approaches/recursion/content.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,12 @@ Then the `rotate` function can execute the same code again with new values.
2828
We can build a long chain or "stack" of `<letter> + rotate(rest)` calls until the `rest` variable is exhausted and the code adds `""`.
2929
That translates to something like this: `<letter> + <letter> + <letter> + <letter> + ""`.
3030

31-
32-
~~~~exercism/note
31+
```exercism/note
3332
By default, we can't have a function call itself more than 1000 times.
3433
Code that exceeds this recursion limit will throw a RecursionError.
3534
While it is possible to adjust the recursion limit, doing so risks crashing Python and may also crash your system.
3635
Casually raising the recursion limit is not recommended.
37-
~~~~
36+
```
3837

3938
[clojure]: https://exercism.org/tracks/clojure
4039
[elixir]: https://exercism.org/tracks/elixir

exercises/practice/rotational-cipher/.approaches/str-translate/content.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,12 @@ The second argument is the `translator` variable + uppercase `translator` variab
3030

3131
`makestrans` does is that it takes the [Unicode][unicode] values of the first argument and maps them to the corresponding Unicode values in the second argument, creating a `dict`.
3232

33-
3433
```python
3534
>>> str.maketrans("abc", "def")
3635
{97: 100, 98: 101, 99: 102}
3736
```
3837

39-
`str.translate` takes the `dict` created by `str.makestrans` and uses it to translate the characters in the `text` argument.
38+
`str.translate` takes the `dict` created by `str.makestrans` and uses it to translate the characters in the `text` argument.
4039

4140
```python
4241
>>> "abc".translate({97: 100, 98: 101, 99: 102})

exercises/practice/rotational-cipher/.articles/performance/content.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ rotate recursion short : 5.4000120144337416e-06
3030
## Conclusion
3131

3232
For a long string as input, the `str.translate` approach the fastest, followed by ascii, alphabet, and finally recursion.
33-
For a short string as input, is the alphabet approach the is the fastest, followed by ascii, recursion and finally `str.translate`.
33+
For a short string as input, is the alphabet approach the is the fastest, followed by ascii, recursion and finally `str.translate`.
3434

3535
This means that if you know the input is a short string, the fastest approach is to use the alphabet, and forgo the overhead of making and saving a translation dictionary.
3636
On the other hand, if the input is a long string, the overhead of making a dictionary is amortized over the length of the text to be translated, and the fastest approach becomes `str.translate`.

0 commit comments

Comments
 (0)