-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCaesar_Cipher.py
More file actions
29 lines (20 loc) · 905 Bytes
/
Caesar_Cipher.py
File metadata and controls
29 lines (20 loc) · 905 Bytes
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
# You are given a list of string, group them if they are same after using Caeser Cipher Encryption.
# Definition of "same", "abc" can right shift 1, get "bcd", here you can shift as many time as you want,
# the string will be considered as same.
from collections import defaultdict
alphabet = 'abcdefghijklmnopqrstuvwxyz'
class CaesarCipher(object):
def group_caesar(self, los):
storage = defaultdict(list)
for i, string in enumerate(los):
position = [alphabet.index(c) for c in string]
minPosition = min(position)
position = [num - minPosition for num in position]
storage[tuple(position)].append(string)
return storage.values()
def main():
caesarCipher = CaesarCipher()
args = ["abc", "bcd", "acd", "dfg", "ace", "bdf", "random"]
print(caesarCipher.group_caesar(args))
if __name__ == "__main__":
main()