-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcaesar_cipher.py
More file actions
96 lines (68 loc) · 2.57 KB
/
caesar_cipher.py
File metadata and controls
96 lines (68 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
from typing import List
def encrypt(text: str, shift: int) -> str:
charsLower = "abcdefghijklmnopqrstuvwxyz"
charsUpper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
newString: str = ""
for char in text:
if char in charsLower:
newString += charsLower[(charsLower.index(char) + shift) % 26]
elif char in charsUpper:
newString += charsUpper[(charsUpper.index(char) + shift) % 26]
else:
newString += char
return newString
def encrypt2(text: str, shift: int) -> str:
result: List[str] = []
for char in text:
if "a" <= char <= "z":
base = ord("a")
result.append(chr((ord(char) - base + shift) % 26 + base))
elif "A" <= char <= "Z":
base = ord("A")
result.append(chr((ord(char) - base + shift) % 26 + base))
else:
result.append(char)
return "".join(result)
def encrypt3(text: str, shift: int) -> str:
return "".join(shift_char(c, shift) for c in text)
def shift_char(char: str, shift: int) -> str:
if "a" <= char <= "z":
base = ord("a")
return chr((ord(char) - base + shift) % 26 + base)
elif "A" <= char <= "Z":
base = ord("A")
return chr((ord(char) - base + shift) % 26 + base)
else:
return char
def decrypt(text: str, shift: int) -> str:
charsLower = "abcdefghijklmnopqrstuvwxyz"
charsUpper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
newString: str = ""
for char in text:
if char in charsLower:
newString += charsLower[(charsLower.index(char) - shift) % 26]
elif char in charsUpper:
newString += charsUpper[(charsUpper.index(char) - shift) % 26]
else:
newString += char
return newString
print(encrypt3("abc", 2)) # "cde"
print(encrypt3("Zebra!", 1)) # "Afcsb!"
print(encrypt3("Hello, World!", 3)) # "Khoor, Zruog!"
print(decrypt("Khoor, Zruog!", 3)) # "Hello, World!"
print(decrypt(encrypt("Python", 5), 5)) # "Python"
def count_words(text: str) -> dict[str, int]:
newString: str = "".join(c for c in text if c.isalpha() or c == " ")
newString = newString.lower().strip()
strings: List[str] = newString.split(" ")
findings: dict[str, int] = {}
for word in strings:
count: int = newString.count(word)
findings[word] = count
return findings
import re
from collections import Counter
def count_words2(text: str) -> dict[str, int]:
words = re.findall(r"[a-zA-Z]+", text.lower())
return dict(Counter(words))
print(count_words("Das hier ist ist ist, ein Text, mal sehen !"))