-
Notifications
You must be signed in to change notification settings - Fork 1
/
RailWay.py
96 lines (74 loc) · 2.65 KB
/
RailWay.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
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
import string
class RailRoadHedge():
def NormilizeText(self, text):
text = text.lower()
text = text.replace(' ', '')
return ''.join(c for c in text if c in string.ascii_letters)
def Encode(self, key, E_text):
key = int(key)
# initializtion
if key == 0 or key == 1:
print('Roflanebalo')
return E_text
else:
key_rez = key
key_check = key
flag = True
#i = 0
k = 0
encodemessage = ''
while key_rez != 0:
while k < len(E_text):
if key_rez == key_check:
encodemessage += E_text[k]
if key_check == 1:
flag = False
if key_check == key:
flag = True
if flag == True:
key_check = key_check - 1
else:
key_check = key_check + 1
# print('key_rez = ',key_rez,'key_check = ',key_check,'encode = ',encodemessage)
# a = input()
k += 1
key_rez -= 1
key_check = key_rez
k = key - key_rez
print(encodemessage)
return encodemessage
def Decode(self, key, D_text):
def Distance(self, size, row, iteration):
if ((size == 0) or (size == 1)):
return 1
if ((row == 0) or (row == size - 1)):
return (size - 1) * 2
if (iteration % 2 == 0):
return (size - 1 - row) * 2
return 2 * row
key = int(key)
if key < 0:
print("Error")
else:
# print(len(D_text))
decodedmessage = ''
CurrPosition = 0
row = 0
word_list = list(D_text)
while row < key:
iter = 0
i = row
while i < len(D_text):
# print('i: ',i,'row: ', row,'iter: ', iter, CurrPosition,'list: ',word_list)
# word_list.insert(i, D_text[CurrPosition])
word_list[i] = D_text[CurrPosition]
CurrPosition += 1
i = i + Distance(self, key, row, iter)
iter += 1
# print('i: ', i, 'row: ', row, 'iter: ', iter, CurrPosition,'list: ',word_list)
# a = input()
row += 1
for letter in word_list:
decodedmessage += letter
print(decodedmessage)
return decodedmessage