From 32dd28a565c8e4513a3b4779a1476102e1e4562e Mon Sep 17 00:00:00 2001 From: h7x4 Date: Wed, 23 Sep 2020 11:09:25 +0200 Subject: [PATCH] Update tasks to use common modules --- Exercise 3/11 - Doble lokker/task11a.py | 8 ++++++-- Exercise 3/11 - Doble lokker/task11b.py | 8 +++++++- Exercise 3/11 - Doble lokker/task11c.py | 7 ++++++- Exercise 3/9 - Alternerende sum/task9a.py | 8 +++++++- Exercise 3/9 - Alternerende sum/task9b.py | 8 +++++++- Exercise 3/task8.py | 8 +++++++- 6 files changed, 40 insertions(+), 7 deletions(-) diff --git a/Exercise 3/11 - Doble lokker/task11a.py b/Exercise 3/11 - Doble lokker/task11a.py index 7932e9c..f6c415c 100644 --- a/Exercise 3/11 - Doble lokker/task11a.py +++ b/Exercise 3/11 - Doble lokker/task11a.py @@ -1,4 +1,8 @@ -from common import inputTypeCheck +try: + from common.inputChecking.typeCheck import inputTypeCheck +except ModuleNotFoundError: + print('Sjekk README.md for hvilke flagg python trenger') + exit(1) def numberPyramid(length): for i in range(length): @@ -26,5 +30,5 @@ def solutionWithGenerator(n): if __name__ == "__main__": - n = int(input('n: ')) + n = inputTypeCheck('n: ', int) print(solutionWithForLoops(n)) diff --git a/Exercise 3/11 - Doble lokker/task11b.py b/Exercise 3/11 - Doble lokker/task11b.py index 88fccf5..08cbc9f 100644 --- a/Exercise 3/11 - Doble lokker/task11b.py +++ b/Exercise 3/11 - Doble lokker/task11b.py @@ -1,3 +1,9 @@ +try: + from common.inputChecking.typeCheck import inputTypeCheck +except ModuleNotFoundError: + print('Sjekk README.md for hvilke flagg python trenger') + exit(1) + def numberPyramid(length): for i in range(length): print('X', ' ' * i + 'X') @@ -10,5 +16,5 @@ def numberPyramidDoubleLoop(length): if __name__ == "__main__": - n = int(input('n: ')) + n = inputTypeCheck('n: ', int) numberPyramidDoubleLoop(n) \ No newline at end of file diff --git a/Exercise 3/11 - Doble lokker/task11c.py b/Exercise 3/11 - Doble lokker/task11c.py index daca35a..57aa3fd 100644 --- a/Exercise 3/11 - Doble lokker/task11c.py +++ b/Exercise 3/11 - Doble lokker/task11c.py @@ -1,5 +1,10 @@ from math import sqrt +try: + from common.inputChecking.typeCheck import inputTypeCheck +except ModuleNotFoundError: + print('Sjekk README.md for hvilke flagg python trenger') + exit(1) # O(√n) def isPrime(n): @@ -37,7 +42,7 @@ def factorize(n): if __name__ == "__main__": - n = int(input('Skriv inn et positivt heltall: ')) + n = inputTypeCheck('Skriv inn et positivt heltall: ', int) factors = factorize(n) if len(factors) == 1: print(f'{n} er et primtall') diff --git a/Exercise 3/9 - Alternerende sum/task9a.py b/Exercise 3/9 - Alternerende sum/task9a.py index 6f9cc1f..1527634 100644 --- a/Exercise 3/9 - Alternerende sum/task9a.py +++ b/Exercise 3/9 - Alternerende sum/task9a.py @@ -1,3 +1,9 @@ +try: + from common.inputChecking.typeCheck import inputTypeCheck +except ModuleNotFoundError: + print('Sjekk README.md for hvilke flagg python trenger') + exit(1) + def alternateSum(n): positiveNumbers = [ num**2 for num in [i for i in range(1, n + 1) if i % 2 != 0] @@ -9,5 +15,5 @@ def alternateSum(n): if __name__ == "__main__": - n = int(input('n: ')) + n = inputTypeCheck('n: ', int) print(alternateSum(n)) \ No newline at end of file diff --git a/Exercise 3/9 - Alternerende sum/task9b.py b/Exercise 3/9 - Alternerende sum/task9b.py index 4b33434..7ebdc25 100644 --- a/Exercise 3/9 - Alternerende sum/task9b.py +++ b/Exercise 3/9 - Alternerende sum/task9b.py @@ -1,3 +1,9 @@ +try: + from common.inputChecking.typeCheck import inputTypeCheck +except ModuleNotFoundError: + print('Sjekk README.md for hvilke flagg python trenger') + exit(1) + def AlternateNumberGenerator(): isEven = lambda n: n % 2 == 0 n = 1 @@ -24,5 +30,5 @@ def alternateSumStopAt(k): if __name__ == "__main__": - k = int(input('k: ')) + k = inputTypeCheck('k: ', int) alternateSumStopAt(k) \ No newline at end of file diff --git a/Exercise 3/task8.py b/Exercise 3/task8.py index c256f0e..1025f96 100644 --- a/Exercise 3/task8.py +++ b/Exercise 3/task8.py @@ -1,3 +1,9 @@ +try: + from common.inputChecking.typeCheck import inputTypeCheck +except ModuleNotFoundError: + print('Sjekk README.md for hvilke flagg python trenger') + exit(1) + def fibonacciIterative(n): k1 = 0 k2 = 1 @@ -12,7 +18,7 @@ fibonacciSum = lambda n: sum([fibonacciIterative(i) for i in range(1, n + 1)]) fibonacciList = lambda n: [fibonacciIterative(i) for i in range(1, n + 1)] if __name__ == "__main__": - n = int(input('n: ')) + n = inputTypeCheck('n: ', int) print(f'a) Fibonacci({n})', fibonacciIterative(n)) print(f'b) Sum av Fibonacci(1..{n})', fibonacciSum(n))