You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: exercises/practice/rotational-cipher/.approaches/ascii-values/content.md
+4-4Lines changed: 4 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -26,24 +26,24 @@ While the uppercase letters are in the range between 65 and 91.
26
26
27
27
This approach only supports the English alphabet.
28
28
Non-English alphabets are not contiguous in their ascii number ranges, and are not consistently defined across platforms.
29
-
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.
29
+
For example, the Scandinavian letter **å** has the extended ascii value of **134**, but is used in combination with Latin-1 characters that appear in the 65-91 and 97-123 ranges.
30
30
This means that a shift for an extended ascii word containing **å** won't result in an accurate alphabet position for a Scandinavian language.
31
31
32
32
~~~~
33
33
34
34
The approach starts with defining the function `rotate()`, with a variable `result` is assigned to an empty string.
35
35
The elements of the text argument are then iterated over using a [`for loop`][for-loop].
36
-
Each element is checked to see if it is a letter, and then is checked if it is an uppercase letter.
36
+
Each element is checked to see if it is a letter, and if it is a lower or uppercase letter.
37
37
38
38
Python has a builtin function called `ord` that converts a [unicode][unicode] symbol to an integer representation.
39
39
Unicode's first 128 code points have the same numbers as their ascii counterparts.
40
40
41
41
If the element is an uppercase letter, [`ord`][ord] is used to convert the letter to an integer.
42
42
The integer is added to the numeric key and then 65 is subtracted from the total.
43
-
Finally, the result is [modulo (%)][modulo] 26 (_to put the value within the 0-26 range_) and 65 is added back.
43
+
Finally, the result is [modulo (%)][modulo] 26 to put the value within the 0-26 range, and 65 is added back.
44
44
45
45
This is because we want to know which letter of the alphabet the number will become.
46
-
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.
46
+
If the new number is over 26 we want to make sure that it "wraps around" to remain in the range of 0-26.
47
47
To properly use modulo for a range we have to make sure that it starts at zero, so we subtract 65.
48
48
To get back to a letter in the ascii range we add 65 and use the [`chr`][chr] method to convert the value to a letter.
Copy file name to clipboardExpand all lines: exercises/practice/rotational-cipher/.approaches/introduction.md
+21-13Lines changed: 21 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,13 +1,14 @@
1
1
# Introduction
2
2
3
-
There are various ways to solve `rotational-cipher`.
4
-
You can for example use [ascii values][ascii], an alphabet `str` or `list`, recursion, or `str.translate`.
3
+
There are various ways to solve `Rotational Cipher`.
4
+
For example, you can use [ascii values][ascii], an alphabet `str`/`list`, recursion, or `str.translate`.
5
5
6
6
## General guidance
7
7
8
8
The goal of this exercise is to shift the letters in a string by a given integer key between 0 and 26.
9
9
The letter in the encrypted string is shifted for as many values (or "positions") as the value of the key.
10
10
11
+
11
12
## Approach: Using ascii values
12
13
13
14
This approach is straightforward to understand.
@@ -18,11 +19,12 @@ The numbers 65-91 in the ascii range represent lowercase Latin letters, while 97
18
19
19
20
This approach only supports the English alphabet.
20
21
Non-English alphabets are not contiguous in their ascii number ranges, and are not consistently defined across platforms.
21
-
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.
22
+
For example, the Scandinavian letter **å** has the extended ascii value of **134**, but is used in combination with Latin-1 characters that appear in the 65-91 and 97-123 ranges.
22
23
This means that a shift for an extended ascii word containing **å** won't result in an accurate alphabet position for a Scandinavian language.
23
24
24
25
~~~~
25
26
27
+
26
28
```python
27
29
defrotate(text, key):
28
30
result =""
@@ -37,17 +39,18 @@ def rotate(text, key):
37
39
return result
38
40
```
39
41
40
-
For more information, check the [ascii values approach][approach-ascii-values].
42
+
For more information, check out the [ascii values approach][approach-ascii-values].
41
43
42
44
## Approach: Alphabet
43
45
44
-
This approach is similar to the ascii one, but it uses the index number of each letter in an alphabet string.
46
+
This approach is similar to the ascii one, but it uses the index number of each letter in a defined alphabet string.
45
47
It requires making a string for all the letters in an alphabet.
46
48
And unless two strings are used, you will have to convert individual letters from lower to upper case (or vice-versa).
47
49
48
-
The big advantage of this approach is the ability to use any alphabet (_although there are some issues with combining characters in Unicode._).
49
-
Here, if we want to use the scandinavian letter: **å**, we can simply insert it into our string where we want it:
50
-
`abcdefghijklmnopqrstuvwxyzå` and the rotation will work correctly.
50
+
The big advantage of this approach is the ability to use _any_ alphabet (_although there are some issues with combining characters in Unicode._).
51
+
Here, if we want to use the Scandinavian letter: **å**, we can simply insert it into our string where we want it:
52
+
`abcdefghijklmnopqrstuvwxyzå` and the key rotation will work correctly.
53
+
51
54
52
55
```python
53
56
# This only uses English characters
@@ -66,7 +69,7 @@ def rotate(text, key):
66
69
return result
67
70
```
68
71
69
-
For more information, check the [Alphabet approach][approach-alphabet].
72
+
For more information, see the [Alphabet approach][approach-alphabet].
70
73
71
74
## Approach: Str translate
72
75
@@ -77,6 +80,7 @@ The benefit of this approach is that it has no visible loop, making the code mor
77
80
`str.translate` **still loops over the `string`** even if it is not visibly doing so.
Copy file name to clipboardExpand all lines: exercises/practice/rotational-cipher/.approaches/recursion/content.md
+7-5Lines changed: 7 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -18,7 +18,7 @@ def rotate(text, key):
18
18
19
19
This approach uses the same logic as that of the `alphabet` approach, but instead of a `loop`, [concept:python/recursion]() is used to parse the text and solve the problem.
20
20
21
-
Recursion is a programming technique where a function calls itself.
21
+
[Recursion][recursion] is a programming technique where a function calls itself.
22
22
It is powerful, but can be more tricky to implement than a `while loop` or `for loop`.
23
23
Recursive techniques are more common in functional programming languages like [Elixir][elixir], [Haskell][haskell], and [Clojure][clojure] than they are in Python.
24
24
@@ -28,16 +28,18 @@ Then the `rotate` function can execute the same code again with new values.
28
28
We can build a long chain or "stack" of `<letter> + rotate(rest)` calls until the `rest` variable is exhausted and the code adds `""`.
29
29
That translates to something like this: `<letter> + <letter> + <letter> + <letter> + ""`.
30
30
31
+
31
32
~~~~exercism/note
32
33
By default, we can't have a function call itself more than 1000 times.
33
-
Code that exceeds this recursion limit will throw a RecursionError.
34
-
While it is possible to adjust the recursion limit, doing so risks crashing Python and may also crash your system.
34
+
Code that exceeds this recursion limit will throw a [`RecursionError`][recursion-error].
35
+
While it is possible to [adjust the recursion limit][recursion-limit], doing so risks crashing Python and may also crash your system.
35
36
Casually raising the recursion limit is not recommended.
0 commit comments