-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathodd_triangle_sum.py
More file actions
50 lines (43 loc) · 1 KB
/
Copy pathodd_triangle_sum.py
File metadata and controls
50 lines (43 loc) · 1 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
def odd_triangle(n):
# edge case
if n == 1:
return 1
# get length of list
lst = []
counter_a = 0
base = 0
while counter_a < n:
base = base + 1
lst.append(base)
counter_a = counter_a + 1
length_triangle = sum(lst)
# generate list of odd numbers of this length
counter_b = 0
next = 1
odd_numbers = []
while counter_b < length_triangle:
next = next + 2
odd_numbers.append(next)
counter_b = counter_b + 1
final_list = odd_numbers[-n-1:-1]
return sum(final_list)
print("My Solution:")
print(odd_triangle(1))
print(odd_triangle(2))
print(odd_triangle(3))
print(odd_triangle(4))
print(odd_triangle(5))
print(odd_triangle(7))
print(odd_triangle(12))
print()
def odd_triangle2(n):
# your code here
return n ** 3
print("Easy/short solution:")
print(odd_triangle2(1))
print(odd_triangle2(2))
print(odd_triangle2(3))
print(odd_triangle2(4))
print(odd_triangle2(5))
print(odd_triangle2(7))
print(odd_triangle2(12))