-
Notifications
You must be signed in to change notification settings - Fork 183
/
02 - Keyword Transposition Cipher.py
42 lines (31 loc) · 1.12 KB
/
02 - Keyword Transposition Cipher.py
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
# ========================
# Information
# ========================
# Direct Link: https://www.hackerrank.com/challenges/keyword-transposition-cipher/problem
# Difficulty: Easy
# Max Score: 50
# Language: Python
# ========================
# Solution
# ========================
from string import ascii_uppercase as aaa
def decode(k, m):
# Removes duplicates from key
key = ''.join([x for i, x in enumerate(k) if k.index(x) == i])
# Get aplhabet without chars in key
alph = ''.join([x for x in aaa if x not in key])
# Get base ordered char
dec = key + alph
# Creates columns by indexing: range() with steps of len(key) and
# Increases the start-value from 0 to len(key) + sorts them
columns = sorted([''.join([dec[x] for x in
range(n, len(dec), len(key))])
for n in range(len(key))])
# Creates a decoding dict
d = {a: b for b, a in zip(aaa, ''.join(columns))}
# Decode
return ''.join(d[x] if x in d else ' ' for x in m)
for _ in range(int(input())):
key = input()
mes = input()
print(decode(key, mes))