-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathDisordered_First_Contact.py
More file actions
156 lines (131 loc) · 4.03 KB
/
Disordered_First_Contact.py
File metadata and controls
156 lines (131 loc) · 4.03 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import sys
import math
# Auto-generated code below aims at helping you parse
# the standard input according to the problem statement.
def step_one(soa, temp):
step1 = ""
step1 += soa.pop(0)
temp += step1
return temp
def step_two(sta, temp, count):
step2 = ""
for i in range(2 + count):
if len(sta) != 0:
step2 += sta.pop(0)
else:
break
temp = "".join(step2 + temp)
return temp
def step_three(sta, temp, count):
step3 = ""
for i in range(3 + count):
if len(sta) != 0:
step3 += sta.pop(0)
else:
break
temp = temp + step3
return temp
def encode(string, time):
temp = ""
arr = []
for s in string:
arr.append(s)
# step 1
temp = step_one(arr, temp)
while time != 0:
time -= 1
count = 0
while (len(arr) != 0):
# step 2
temp = step_two(arr, temp, count)
# step3
temp = step_three(arr, temp, count)
# increase count
count += 2
if time != 0:
arr = []
for t in temp:
arr.append(t)
temp = ""
temp = step_one(arr, temp)
print(temp)
def dcstep_two(sta, temp, value):
step2 = ""
for a in sta[0:value]:
step2 += a
del sta[0:value]
temp = "".join(step2 + temp)
return temp
def dcstep_three(sta, temp, value):
step3 = ""
for a in sta[(len(sta) - value):len(sta)]:
step3 += a
del sta[(len(sta) - value):len(sta)]
temp = "".join(step3 + temp)
return temp
def decode(string, time):
arr = []
temp = ""
for s in string:
arr.append(s)
number_steps = 1
# check how many steps are there to
for i in range(len(arr)):
check = sum(range(1, number_steps, 1))
if check >= len(arr):
# break loop as have needed about of steps
break
# if steps not enough increase
number_steps += 1
# check if perfect fit or not
check = sum(range(1, number_steps, 1))
# store the orginal value
n_s = number_steps
flag_init = True
if check >= len(arr):
while time != 0:
time -= 1
# need to find it is more by how much
# note value of steps is one more for the loop so actually it is using one less.
# convert to real number of steps
number_steps = n_s
number_steps -= 1
# gotten real number of steps now to check which step is it at.
# if it is even it is at the 2nd step however since it over the number we will
# need to substrate the missing value from it. first and only once.
while number_steps >= 1:
if (number_steps % 2 == 0) and number_steps != 1:
if flag_init:
value = number_steps - (check - len(arr))
temp = dcstep_two(arr, temp, value)
flag_init = False
else:
temp = dcstep_two(arr, temp, number_steps)
number_steps -= 1
elif (number_steps % 2 == 1) and number_steps != 1:
if flag_init:
value = number_steps - (check - len(arr))
temp = dcstep_three(arr, temp, value)
flag_init = False
else:
temp = dcstep_three(arr, temp, number_steps)
number_steps -= 1
elif number_steps == 1:
temp = "".join(arr.pop() + temp)
number_steps -= 1
if time != 0:
arr = []
for t in temp:
arr.append(t)
temp = ""
flag_init = True
print(temp)
n = int(input())
message = input()
# Write an action using print
# To debug: print >> sys.stderr, "Debug messages..."
if n < 0:
n = n * -1
encode(message, n)
else:
decode(message, n)