-
Notifications
You must be signed in to change notification settings - Fork 0
/
S1630_pyramid_solution.py
78 lines (65 loc) · 3.37 KB
/
S1630_pyramid_solution.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
"""Opgave "Number pyramid" (løsning):
Inspicer følgende kode i detaljer. Især hvis du ikke selv har fundet på en løsning.
Find ud af, hvad hver række kode gør. F.eks. ved at ændre koden en smule og derefter2
køre/debugge programmet.
Send derefter denne Teams-meddelelse til din lærer: <filename> færdig
Fortsæt derefter med den næste fil."""
def pyramid2(lines, start):
strings = start.split(" ") # split start into a list of strings
numbers = [] # numbers is a list and will later contain numbers
for s in strings:
if len(s) > 0:
numbers.append(int(s.strip())) # strip removes white spaces and similar characters from the beginning and end of string
number_lists = [numbers] # number_lists is a list and will later contain lists of numbers
for line in range(lines):
number_lists.append(number_lists[line].copy()) # copy the last element in number_lists and appended it to number_lists
print("row " + str(line+1), end=": ")
print(number_lists[line])
index_shift = 0 # the last line keeps growing while we edit it. this variable helps us to keep track where we have to insert the next number.
for n in range(len(number_lists[line])-1):
if number_lists[line][n] + number_lists[line][n + 1] == line + 2: # is the criterion for insertion of a new number met?
number_lists[line+1].insert(n + index_shift + 1, line + 2)
index_shift += 1
# some alternative solutions:
def pyramid2a(lines, start):
numbers = [int(i) for i in str(start)]
numbers2 = [i for i in numbers]
for line in range(lines):
print("row " + str(line+1), end=": ")
print(numbers2)
index_shift = 0
for n in range(len(numbers)-1):
if numbers[n] + numbers[n + 1] == line + 2:
numbers2.insert(n + index_shift + 1, line + 2)
index_shift += 1
numbers = [i for i in numbers2]
def pyramid2b(lines, start):
number_lists = [[int(i) for i in str(start)]]
for line in range(lines):
number_lists.append([i for i in number_lists[line]])
print("row " + str(line+1), end=": ")
print(number_lists[line])
index_shift = 0
for n in range(len(number_lists[line])-1):
if number_lists[line][n] + number_lists[line][n + 1] == line + 2:
number_lists[line+1].insert(n + index_shift + 1, line + 2)
index_shift += 1
def pyramid2c(lines, start):
number_lists = [[int(i) for i in str(start)]]
for line in range(lines):
number_lists.append(number_lists[line].copy())
print("row " + str(line+1), end=": ")
print(number_lists[line])
index_shift = 0
for n in range(len(number_lists[line])-1):
if number_lists[line][n] + number_lists[line][n + 1] == line + 2:
number_lists[line+1].insert(n + index_shift + 1, line + 2)
index_shift += 1
# start_input = int(input("Enter the first row of the pyramid: "))
# lines_input = int(input("Enter the number of rows to print: "))
# pyramid2a(lines_input, start_input)
# pyramid2b(lines_input, start_input)
# pyramid2c(lines_input, start_input)
start_input = input("Enter the first row of the pyramid. (Separate the individual numbers with spaces.): ")
lines_input = int(input("Enter the number of rows to print: "))
pyramid2(lines_input, start_input)