Skip to content

Commit 4a3ca80

Browse files
authored
[Rotational Cipher Approaches] Fix ALPHABET capitalization and approaches link (#4184)
* fix ALPHABET capitalization and approaches link * add name to individual article contributor lists
1 parent b6ea39b commit 4a3ca80

11 files changed

Lines changed: 49 additions & 44 deletions

File tree

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
# Alphabet
22

33
```python
4-
AlPHABET = "abcdefghijklmnopqrstuvwxyz"
4+
ALPHABET = "abcdefghijklmnopqrstuvwxyz"
55

66
def rotate(text, key):
77
result = ""
88
for letter in text:
99
if letter.isalpha():
1010
if letter.isupper():
11-
result += AlPHABET[(AlPHABET.index(letter.lower()) + key) % 26].upper()
11+
result += ALPHABET[(ALPHABET.index(letter.lower()) + key) % 26].upper()
1212
else:
13-
result += AlPHABET[(AlPHABET.index(letter) + key) % 26]
13+
result += ALPHABET[(ALPHABET.index(letter) + key) % 26]
1414
else:
1515
result += letter
1616
return result
@@ -22,9 +22,9 @@ The function `rotate()` is then declared, and a variable `result` is defined as
2222
The text argument is then iterated over via a [`for loop`][for-loop].
2323
Each element is checked to make sure it is a letter, and subsequently checked if it is uppercase or lowercase.
2424
Uppercase letters are converted to lowercase.
25-
Then the index of each letter is found in the `AlPHABET` constant.
25+
Then the index of each letter is found in the `ALPHABET` constant.
2626
The numeric key value is added to the letter index and [modulo (`%`)][modulo] 26 is used on the result.
27-
Finally, the new number is used as an index into the `AlPHABET` constant, and the resulting letter is converted back to uppercase.
27+
Finally, the new number is used as an index into the `ALPHABET` constant, and the resulting letter is converted back to uppercase.
2828

2929
Lowercase letters follow the same process without the conversion steps.
3030

@@ -36,7 +36,7 @@ If only English letters are needed, the constant [`string.ascii_lowercase`][asci
3636
```python
3737
from string import ascii_lowercase
3838

39-
AlPHABET = ascii_lowercase
39+
ALPHABET = ascii_lowercase
4040
```
4141

4242
[ascii_lowercase]: https://docs.python.org/3/library/string.html#string.ascii_letters
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
for letter in text:
22
if letter.isalpha():
33
if letter.isupper():
4-
result += AlPHABET[(AlPHABET.index(letter.lower()) + key) % 26].upper()
4+
result += ALPHABET[(ALPHABET.index(letter.lower()) + key) % 26].upper()
55
else:
6-
result += AlPHABET[(AlPHABET.index(letter) + key) % 26]
6+
result += ALPHABET[(ALPHABET.index(letter) + key) % 26]
77
else:
88
result += letter

exercises/practice/rotational-cipher/.approaches/config.json

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"introduction": {
33
"authors": ["meatball133", "bethanyg"],
4-
"contributors": []
4+
"contributors": ["yrahcaz7"]
55
},
66
"approaches": [
77
{
@@ -16,21 +16,24 @@
1616
"slug": "alphabet",
1717
"title": "Alphabet",
1818
"blurb": "Using the alphabet to rotate the alphabet",
19-
"authors": ["meatball133", "bethanyg"]
19+
"authors": ["meatball133", "bethanyg"],
20+
"contributors": ["yrahcaz7"]
2021
},
2122
{
2223
"uuid": "e539d1a5-f497-402b-a232-7e889f4323c1",
2324
"slug": "str-translate",
2425
"title": "Str Translate",
2526
"blurb": "Using str.translate to rotate the alphabet",
26-
"authors": ["meatball133", "bethanyg"]
27+
"authors": ["meatball133", "bethanyg"],
28+
"contributors": ["yrahcaz7"]
2729
},
2830
{
2931
"uuid": "0c74890e-d96e-47a2-a8bf-93c45dd67f94",
3032
"slug": "recursion",
3133
"title": "Recursion",
3234
"blurb": "Using Recursion to rotate the alphabet",
33-
"authors": ["meatball133", "bethanyg"]
35+
"authors": ["meatball133", "bethanyg"],
36+
"contributors": ["yrahcaz7"]
3437
}
3538
]
3639
}

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,16 @@ Here, if we want to use the Scandinavian letter: **å**, we can simply insert it
5454

5555
```python
5656
# This only uses English characters
57-
AlPHABET = "abcdefghijklmnopqrstuvwxyz"
57+
ALPHABET = "abcdefghijklmnopqrstuvwxyz"
5858

5959
def rotate(text, key):
6060
result = ""
6161
for letter in text:
6262
if letter.isalpha():
6363
if letter.isupper():
64-
result += AlPHABET[(AlPHABET.index(letter.lower()) + key) % 26].upper()
64+
result += ALPHABET[(ALPHABET.index(letter.lower()) + key) % 26].upper()
6565
else:
66-
result += AlPHABET[(AlPHABET.index(letter) + key) % 26]
66+
result += ALPHABET[(ALPHABET.index(letter) + key) % 26]
6767
else:
6868
result += letter
6969
return result
@@ -82,11 +82,11 @@ The benefit of this approach is that it has no visible loop, making the code mor
8282

8383

8484
```python
85-
AlPHABET = "abcdefghijklmnopqrstuvwxyz"
85+
ALPHABET = "abcdefghijklmnopqrstuvwxyz"
8686

8787
def rotate(text, key):
88-
translator = AlPHABET[key:] + AlPHABET[:key]
89-
return text.translate(str.maketrans(AlPHABET + AlPHABET.upper(), translator + translator.upper()))
88+
translator = ALPHABET[key:] + ALPHABET[:key]
89+
return text.translate(str.maketrans(ALPHABET + ALPHABET.upper(), translator + translator.upper()))
9090
```
9191

9292
For more information, check out the [Str translate approach][approach-str-translate].
@@ -106,17 +106,17 @@ Calculate your base case carefully to avoid errors.
106106

107107

108108
```python
109-
AlPHABET = "abcdefghijklmnopqrstuvwxyz"
109+
ALPHABET = "abcdefghijklmnopqrstuvwxyz"
110110

111111
def rotate(text, key):
112112
if text == "":
113113
return ""
114114
first_letter, rest = text[0], text[1:]
115115
if first_letter.isalpha():
116116
if first_letter.isupper():
117-
return AlPHABET[(AlPHABET.index(first_letter.lower()) + key) % 26].upper() + rotate(rest, key)
117+
return ALPHABET[(ALPHABET.index(first_letter.lower()) + key) % 26].upper() + rotate(rest, key)
118118
else:
119-
return AlPHABET[(AlPHABET.index(first_letter) + key) % 26] + rotate(rest, key)
119+
return ALPHABET[(ALPHABET.index(first_letter) + key) % 26] + rotate(rest, key)
120120
else:
121121
return first_letter + rotate(rest, key)
122122
```

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
# Recursion
22

33
```python
4-
AlPHABET = "abcdefghijklmnopqrstuvwxyz"
4+
ALPHABET = "abcdefghijklmnopqrstuvwxyz"
55

66
def rotate(text, key):
77
if text == "":
88
return ""
99
first_letter, rest = text[0], text[1:]
1010
if first_letter.isalpha():
1111
if first_letter.isupper():
12-
return AlPHABET[(AlPHABET.index(first_letter.lower()) + key) % 26].upper() + rotate(rest, key)
12+
return ALPHABET[(ALPHABET.index(first_letter.lower()) + key) % 26].upper() + rotate(rest, key)
1313
else:
14-
return AlPHABET[(AlPHABET.index(first_letter) + key) % 26] + rotate(rest, key)
14+
return ALPHABET[(ALPHABET.index(first_letter) + key) % 26] + rotate(rest, key)
1515
else:
1616
return first_letter + rotate(rest, key)
1717
```
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
first_letter, rest = text[0], text[1:]
22
if first_letter.isalpha():
33
if first_letter.isupper():
4-
return AlPHABET[(AlPHABET.index(first_letter.lower()) + key) % 26].upper() + rotate(rest, key)
4+
return ALPHABET[(ALPHABET.index(first_letter.lower()) + key) % 26].upper() + rotate(rest, key)
55
else:
6-
return AlPHABET[(AlPHABET.index(first_letter) + key) % 26] + rotate(rest, key)
6+
return ALPHABET[(ALPHABET.index(first_letter) + key) % 26] + rotate(rest, key)
77
else:
88
return first_letter + rotate(rest, key)

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
# Str Translate
22

33
```python
4-
AlPHABET = "abcdefghijklmnopqrstuvwxyz"
4+
ALPHABET = "abcdefghijklmnopqrstuvwxyz"
55

66
def rotate(text, key):
7-
translator = AlPHABET[key:] + AlPHABET[:key]
8-
return text.translate(str.maketrans(AlPHABET + AlPHABET.upper(), translator + translator.upper()))
7+
translator = ALPHABET[key:] + ALPHABET[:key]
8+
return text.translate(str.maketrans(ALPHABET + ALPHABET.upper(), translator + translator.upper()))
99
```
1010

1111
This approach uses the [`<str>.translate`][translate] method.
@@ -14,18 +14,18 @@ To create a translation table we use [`str.makestrans`][maketrans].
1414

1515
This approach starts with defining a constant of all the lowercase letters in the alphabet.
1616
Then the function `rotate()` is declared.
17-
A `translator` variable defined with the value of the `AlPHABET` constant [sliced][slicing] from the key to the end and then sliced from the start to the key.
17+
A `translator` variable defined with the value of the `ALPHABET` constant [sliced][slicing] from the key to the end and then sliced from the start to the key.
1818

1919
This is done so we have 2 strings which are the same but shifted by the key value.
20-
Say we have the `AlPHABET` constant with the value of `abcdefghijklmnopqrstuvwxyz` and the key is 3.
20+
Say we have the `ALPHABET` constant with the value of `abcdefghijklmnopqrstuvwxyz` and the key is 3.
2121
Then the `translator` variable will have the value of `defghijklmnopqrstuvwxyzabc`.
2222

2323
`str.translate` is then called on the `text` argument.
2424
`str.translate` takes a translation table mapping start values to transformed values as an argument.
2525
To create a translation table, `str.makestrans` is used.
2626
`makestrans` takes 2 arguments: the first is the string to be translated, and the second is the string the first argument should be translated to.
2727

28-
For our solution, the first argument is the `AlPHABET` constant + the `AlPHABET` constant in uppercase.
28+
For our solution, the first argument is the `ALPHABET` constant + the `ALPHABET` constant in uppercase.
2929
The second argument is the `translator` variable + uppercase `translator` variable.
3030

3131
`str.makestrans` takes the [Unicode][unicode] values of the first argument and maps them to the corresponding Unicode values in the second argument, creating a `dict`.
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
AlPHABET = "abcdefghijklmnopqrstuvwxyz"
1+
ALPHABET = "abcdefghijklmnopqrstuvwxyz"
22

33
def rotate(text, key):
4-
translator = AlPHABET[key:] + AlPHABET[:key]
5-
return text.translate(str.maketrans(AlPHABET + AlPHABET.upper(), translator + translator.upper()))
4+
translator = ALPHABET[key:] + ALPHABET[:key]
5+
return text.translate(str.maketrans(ALPHABET + ALPHABET.upper(), translator + translator.upper()))

exercises/practice/rotational-cipher/.articles/config.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
"slug": "performance",
66
"title": "Performance deep dive",
77
"blurb": "Deep dive to find out the performance between different approaches",
8-
"authors": ["meatball133", "bethanyg"]
8+
"authors": ["meatball133", "bethanyg"],
9+
"contributors": ["yrahcaz7"]
910
}
1011
]
1112
}

exercises/practice/rotational-cipher/.articles/performance/code/Benchmark.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
print(sys.version)
77

88

9-
AlPHABET = "abcdefghijklmnopqrstuvwxyz"
10-
COMBINATIONS = itertools.combinations_with_replacement(f"{AlPHABET[:13]}{AlPHABET[:13].upper()} 12,", 2)
9+
ALPHABET = "abcdefghijklmnopqrstuvwxyz"
10+
COMBINATIONS = itertools.combinations_with_replacement(f"{ALPHABET[:13]}{ALPHABET[:13].upper()} 12,", 2)
1111
TEST_TEST = "".join([element for sublist in COMBINATIONS for element in sublist])
1212

1313
def rotate_ascii(text, key):
@@ -28,17 +28,17 @@ def rotate_alphabet(text, key):
2828
for letter in text:
2929
if letter.isalpha():
3030
if letter.isupper():
31-
result += AlPHABET[(AlPHABET.index(letter.lower()) + key) % 26].upper()
31+
result += ALPHABET[(ALPHABET.index(letter.lower()) + key) % 26].upper()
3232
else:
33-
result += AlPHABET[(AlPHABET.index(letter) + key) % 26]
33+
result += ALPHABET[(ALPHABET.index(letter) + key) % 26]
3434
else:
3535
result += letter
3636
return result
3737

3838

3939
def rotate_translate(text, key):
40-
translator = AlPHABET[key:] + AlPHABET[:key]
41-
return text.translate(str.maketrans(AlPHABET + AlPHABET.upper(), translator + translator.upper()))
40+
translator = ALPHABET[key:] + ALPHABET[:key]
41+
return text.translate(str.maketrans(ALPHABET + ALPHABET.upper(), translator + translator.upper()))
4242

4343

4444
def rotate_recursion(text, key):
@@ -47,9 +47,9 @@ def rotate_recursion(text, key):
4747
first_letter, rest = text[0], text[1:]
4848
if first_letter.isalpha():
4949
if first_letter.isupper():
50-
return AlPHABET[(AlPHABET.index(first_letter.lower()) + key) % 26].upper() + rotate_recursion(rest, key)
50+
return ALPHABET[(ALPHABET.index(first_letter.lower()) + key) % 26].upper() + rotate_recursion(rest, key)
5151
else:
52-
return AlPHABET[(AlPHABET.index(first_letter) + key) % 26] + rotate_recursion(rest, key)
52+
return ALPHABET[(ALPHABET.index(first_letter) + key) % 26] + rotate_recursion(rest, key)
5353
else:
5454
return first_letter + rotate_recursion(rest, key)
5555

0 commit comments

Comments
 (0)