-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
86 lines (63 loc) · 2.52 KB
/
main.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
DOCSTRING FOR MODULE
"""
# py_ver : [3.5.2]
# date : [03.11.2016]
# author : Aleksey Yakovlev
# email : [email protected]
# contacts : github: freenoth
# license : WTFPL v2
# [ LICENSE_COMMENT ]
# This program is free software. It comes without any warranty, to the extent
# permitted by applicable law. You can redistribute it and/or modify it
# under the terms of the Do What The Fuck You Want To Public License Version 2
# See http://www.wtfpl.net/ for more details.
# imports
import sys
import time
class Timer(object):
""" Service class, needed to control running time. """
def __init__(self):
self.start_time = time.time()
def __str__(self):
current_time = time.time()
return '{:.3f} sec'.format(current_time - self.start_time)
def calc(n):
# n - количество квадратов в сетке, вершин для переходов на одну больше
points = n + 1
# сначала рассчитаем количество возможных путей в точках на главной диагонали
# двигаться будем по диагоналям, начиная с конечной вершины
diagonal = [1]
for i in range(0, points - 1):
next_diagonal = [0 for x in range(0, len(diagonal) + 1)]
for j in range(0, len(diagonal)):
next_diagonal[j] += diagonal[j]
next_diagonal[j+1] += diagonal[j]
diagonal = next_diagonal
# теперь двигаемся от главной диагонали к начальной точке, схлапывая суммы
while len(diagonal) > 1:
next_diagonal = [0 for x in range(0, len(diagonal) - 1)]
for j in range(0, len(next_diagonal)):
next_diagonal[j] = diagonal[j] + diagonal[j + 1]
diagonal = next_diagonal
return diagonal[0]
def _main():
print('Print "quit" for exiting...\n')
while True:
s = input('n = ')
if s == 'quit':
break
try:
n = int(s)
timer = Timer()
result = calc(n)
print('result = {0}\ncalculated over {1}\n'.format(result, timer))
except ValueError as err:
print(err)
if __name__ == '__main__':
print('using python {0}.{1}.{2}\n'.format(sys.version_info[0],
sys.version_info[1],
sys.version_info[2]))
_main()