This repository was archived by the owner on Jan 5, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ2.py
More file actions
89 lines (68 loc) · 2.46 KB
/
Q2.py
File metadata and controls
89 lines (68 loc) · 2.46 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
def generateAlphabet():
return list(map(chr, range(65, 91))) # Generates alphabet (uppercase)
def generateDial(n):
alphabet = generateAlphabet()
dial1 = alphabet.copy()
dial2 = {} # Will hold new dial
n = n % len(dial1) # Ensure n is a valid index
current = 0 # Will hold out last position
for _ in range(len(dial1)): # Loop through every letter in dial1
index = n + current - 1 # Start from where we were before, add n
index = index % len(dial1) # Ensure valid index
current = index # Save our position for next loop
letter = dial1[index] # Find the letter
dial1.remove(letter) # Remove from dial1
dial2[alphabet[_]] = letter # Add to dial2
return dial2
def encrypt(n, word):
dial = list(generateDial(n).values()) # Get a list of dial values
dial6 = dial[0:6] # Save the first 6
alphabet = generateAlphabet() # Get an alphabet for lookup
enc = [] # Will hold our message
for char in word: # Loop over each chat
alphabetIndex = alphabet.index(char) # Find its alphabet index
dialChar = dial[alphabetIndex] # Lookup that index on the dial
enc.append(dialChar) # Save the encoded char
dial = [dial[((f+1)%len(dial))] for f in range(len(dial))] # Shift dial by 1
return ["".join(dial6), "".join(enc)]
def partatest():
print("\n-- 2(a) --")
n = None
while not n or n < 1 or n > 1000000:
n = int(input("n: "))
w = None
while not w or len(w) < 1 or len(w) > 8:
w = input("word: ").upper()
print("\n{}\n{}".format(*encrypt(n, w)))
def partbtest():
print("\n-- 2(b) --")
print("".join(generateDial(1000000000).values()))
def partctest():
print("\n-- 2(c) --\n(Brute force, takes some time...)")
result = None
alphabet = "".join(generateAlphabet())
test = False
n = -1
def uniquetest(string):
return sorted(set(list(string))) == sorted(list(string))
while not test and n < 1000000:
n += 1
result = encrypt(n, alphabet)
test = uniquetest(result[1])
if test:
print(n, result)
else:
print(test)
# 2(a)
# Demonstrates the program in operation with user inputs.
partatest()
# 2(b)
# Returns the second dial order for n = 1,000,000,000
partbtest()
# 2(c)
# Brute force test (do not suggest running)
# partctest()
# Result = False
# (No value of n between 0 and 1,000,000 generates a unique encoding of the alphabet)
# 2(d)
# Unsure