TDT4109/Exercise 3/11 - Doble lokker/task11a.py

35 lines
723 B
Python
Raw Permalink Normal View History

2020-09-23 11:09:25 +02:00
try:
from common.inputChecking.typeCheck import inputTypeCheck
except ModuleNotFoundError:
print('Sjekk README.md for hvilke flagg python trenger')
exit(1)
2020-09-15 09:27:47 +02:00
2020-09-14 14:34:03 +02:00
def numberPyramid(length):
for i in range(length):
row = ''
2020-09-14 16:36:09 +02:00
for k in range(i + 1):
2020-09-14 14:34:03 +02:00
row += f'{k+1} '
print(row)
2020-09-14 16:36:09 +02:00
2020-09-14 14:34:03 +02:00
def numberPyramidGenerator():
currentList = ['1']
while True:
yield ' '.join(currentList)
currentList.append(str(int(currentList[-1]) + 1))
def solutionWithForLoops(n):
return numberPyramid(n)
2020-09-14 16:36:09 +02:00
2020-09-14 14:34:03 +02:00
def solutionWithGenerator(n):
myGenerator = numberPyramidGenerator()
for i in range(n):
print(next(myGenerator))
2020-09-14 16:36:09 +02:00
2020-09-14 14:34:03 +02:00
if __name__ == "__main__":
2020-09-23 11:09:25 +02:00
n = inputTypeCheck('n: ', int)
2020-09-14 16:36:09 +02:00
print(solutionWithForLoops(n))