-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathac_task3_2.py
93 lines (81 loc) · 3.72 KB
/
ac_task3_2.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
import sys
def main():
inputNumber = 368078
squareSize = 5 # incremented by 2 every four sides
distToLeftVal = 9 # incremented by 2 after switch to next side of a square; (currentIndex - distToLeftVal) gives an index of a left neighbour
currentIndex = 9
nodesNumberTypeC = 0
memoryList = [1,1,2,4,5,10,11,23,25] # we start with square size 3
# nodes' types explanation (node of type A is the first one processed in each square):
# square with size 7:
# e d c c c b e
# b d
# c c
# c c
# c b
# d a
# e b c c c c d
# change of square's size means incrementing/decrementing number of C type nodes by 2 on each side
flag = True
while (flag):
for i in range(0, 4):
if i == 0:
# first side of current square - process a node of type A
nextValue = getNodeTypeA(memoryList, distToLeftVal, currentIndex, inputNumber)
currentIndex += 1
memoryList.append(nextValue)
# process a node of type B
nextValue = getNodeTypeB(memoryList, distToLeftVal, currentIndex, inputNumber)
currentIndex += 1
memoryList.append(nextValue)
# process all nodes of type C
for j in range(0, nodesNumberTypeC):
nextValue = getNodeTypeC(memoryList, distToLeftVal, currentIndex, inputNumber)
currentIndex += 1
memoryList.append(nextValue)
# process a node of type D
nextValue = getNodeTypeD(memoryList, distToLeftVal, currentIndex, inputNumber)
currentIndex += 1
memoryList.append(nextValue)
if i < 3:
# process a node of type E
nextValue = getNodeTypeE(memoryList, distToLeftVal, currentIndex, inputNumber)
currentIndex += 1
memoryList.append(nextValue)
if i == 0 or i == 2:
# after first side is processed increment node number of type C by 1
nodesNumberTypeC += 1
distToLeftVal += 2
if nextValue > inputNumber:
#print(memoryList)
flag = False
break
squareSize += 2
def getNodeTypeA(memoryList, distToLeftNode, idx, inputNumber):
calculatedValue = memoryList[idx - 1] + memoryList[idx + 1 - distToLeftNode]
validateCurrentValue(calculatedValue, inputNumber)
return calculatedValue
def getNodeTypeB(memoryList, distToLeftNode, idx, inputNumber):
calculatedValue = memoryList[idx - 1] + memoryList[idx - 2] + memoryList[idx - distToLeftNode] + memoryList[idx + 1 - distToLeftNode]
validateCurrentValue(calculatedValue, inputNumber)
return calculatedValue
def getNodeTypeC(memoryList, distToLeftNode, idx, inputNumber):
calculatedValue = memoryList[idx - 1] + memoryList[idx - distToLeftNode - 1] + memoryList[idx - distToLeftNode] + memoryList[idx + 1 - distToLeftNode]
validateCurrentValue(calculatedValue, inputNumber)
return calculatedValue
def getNodeTypeD(memoryList, distToLeftNode, idx, inputNumber):
calculatedValue = memoryList[idx - 1] + memoryList[idx - distToLeftNode - 1] + memoryList[idx - distToLeftNode]
validateCurrentValue(calculatedValue, inputNumber)
return calculatedValue
def getNodeTypeE(memoryList, distToLeftNode, idx, inputNumber):
calculatedValue = memoryList[idx - 1] + memoryList[idx - 1 - distToLeftNode]
validateCurrentValue(calculatedValue, inputNumber)
return calculatedValue
def validateCurrentValue(cV, inputNr):
if cV > inputNr:
print("THE ANSWER IS: " + str(cV))
return True
else:
return False
if __name__=="__main__":
main()