-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathshapeArea.py
More file actions
executable file
·70 lines (52 loc) · 1.44 KB
/
Copy pathshapeArea.py
File metadata and controls
executable file
·70 lines (52 loc) · 1.44 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
"""
Below we will define an n-interesting polygon. Your task is to find the area of a polygon for a given n.
A 1-interesting polygon is just a square with a side of length 1.
An n-interesting polygon is obtained by taking the n - 1-interesting polygon and
appending 1-interesting polygons to its rim, side by side.
You can see the 1-, 2-, 3- and 4-interesting polygons in the picture below.
X
X XXX
X XXX XXXXX
X XXX
X
n=1 n=2 n=3
Example
For n = 2, the output should be
shapeArea(n) = 5;
For n = 3, the output should be
shapeArea(n) = 13.
Input/Output
[execution time limit] 4 seconds (py3)
[input] integer n
Guaranteed constraints:
1 ≤ n < 104.
[output] integer
The area of the n-interesting polygon.
"""
def shapeArea(n):
if n == 1:
return 1
area = 0
start = n
for i in range(n, 2, -1):
area += 4+(start-2)*4
start -= 1
area += 5
return area
#
# def helper(n):
# if n == 1:
# return 1
#
# if n == 2:
# return 5
# return 4+(n-2)*4 + helper(n-1)
#
# return helper(n)
def shapeArea_redux(n):
return n ** 2 + (n - 1) ** 2
if __name__ == '__main__':
print(shapeArea(1))
print(shapeArea(2))
print(shapeArea(3))
print(shapeArea(4))