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
+3-4Lines changed: 3 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -22,15 +22,14 @@ It uses numbers to represent 128 different entities including carriage returns,
22
22
In ascii, all the lowercase English letters appear between 97 and 123.
23
23
While the uppercase letters are in the range between 65 and 91.
24
24
25
-
~~~~exercism/caution
25
+
```exercism/caution
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
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.
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
-
~~~~
33
-
32
+
```
34
33
35
34
The approach starts with defining the function `rotate()`, with a variable `result` is assigned to an empty string.
36
35
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
41
40
42
41
If the element is an uppercase letter, [`ord`][ord] is used to convert the letter to an integer.
43
42
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.
45
44
46
45
This is because we want to know which letter of the alphabet the number will become.
47
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.
Copy file name to clipboardExpand all lines: exercises/practice/rotational-cipher/.approaches/introduction.md
+6-17Lines changed: 6 additions & 17 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,7 +3,6 @@
3
3
There are various ways to solve `rotational-cipher`.
4
4
You can for example use [ascii values][ascii], an alphabet `str` or `list`, recursion, or `str.translate`.
5
5
6
-
7
6
## General guidance
8
7
9
8
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.
15
14
It uses the ascii value of the letters to rotate them within the message.
16
15
The numbers 65-91 in the ascii range represent lowercase Latin letters, while 97-123 represent uppercase Latin letters.
17
16
18
-
19
-
~~~~exercism/caution
17
+
```exercism/caution
20
18
21
19
This approach only supports the English alphabet.
22
20
Non-English alphabets are not contiguous in their ascii number ranges, and are not consistently defined across platforms.
23
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.
24
22
This means that a shift for an extended ascii word containing **å** won't result in an accurate alphabet position for a Scandinavian language.
25
23
26
-
~~~~
27
-
24
+
```
28
25
29
26
```python
30
27
defrotate(text, key):
@@ -42,7 +39,6 @@ def rotate(text, key):
42
39
43
40
For more information, check the [ascii values approach][approach-ascii-values].
44
41
45
-
46
42
## Approach: Alphabet
47
43
48
44
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
53
49
Here, if we want to use the scandinavian letter: **å**, we can simply insert it into our string where we want it:
54
50
`abcdefghijklmnopqrstuvwxyzå` and the rotation will work correctly.
55
51
56
-
57
52
```python
58
53
# This only uses English characters
59
54
AlPHABET ="abcdefghijklmnopqrstuvwxyz"
@@ -73,16 +68,14 @@ def rotate(text, key):
73
68
74
69
For more information, check the [Alphabet approach][approach-alphabet].
75
70
76
-
77
71
## Approach: Str translate
78
72
79
73
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.
80
74
The benefit of this approach is that it has no visible loop, making the code more concise.
81
75
82
-
~~~~exercism/note
76
+
```exercism/note
83
77
`str.translate` **still loops over the `string`** even if it is not visibly doing so.
84
-
~~~~
85
-
78
+
```
86
79
87
80
```python
88
81
AlPHABET ="abcdefghijklmnopqrstuvwxyz
@@ -94,19 +87,16 @@ def rotate(text, key):
94
87
95
88
For more information, check the [Str translate approach][approach-str-translate].
96
89
97
-
98
90
## Approach: Recursion
99
91
100
92
This approach uses a recursive function.
101
93
A recursive function is a function that calls itself.
102
94
This approach can be more concise than other approaches, and may also be more readable for some audiences.
103
95
104
-
105
-
~~~~exercism/caution
96
+
```exercism/caution
106
97
Python does not have any tail-call optimization and has a default [recursion limit][recursion-limit] of 1000 calls on the stack.
107
98
Calculate your base case carefully to avoid errors.
108
-
~~~~
109
-
99
+
```
110
100
111
101
```python
112
102
AlPHABET ="abcdefghijklmnopqrstuvwxyz"
@@ -126,7 +116,6 @@ def rotate(text, key):
126
116
127
117
For more information, check the [Recursion approach][approach-recursion].
128
118
129
-
130
119
## Benchmark
131
120
132
121
For more information, check the [Performance article][article-performance].
Copy file name to clipboardExpand all lines: exercises/practice/rotational-cipher/.approaches/str-translate/content.md
+1-2Lines changed: 1 addition & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -30,13 +30,12 @@ The second argument is the `translator` variable + uppercase `translator` variab
30
30
31
31
`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`.
32
32
33
-
34
33
```python
35
34
>>>str.maketrans("abc", "def")
36
35
{97: 100, 98: 101, 99: 102}
37
36
```
38
37
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.
Copy file name to clipboardExpand all lines: exercises/practice/rotational-cipher/.articles/performance/content.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -30,7 +30,7 @@ rotate recursion short : 5.4000120144337416e-06
30
30
## Conclusion
31
31
32
32
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`.
34
34
35
35
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.
36
36
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