Update tasks to use common modules

This commit is contained in:
Oystein Kristoffer Tveit 2020-09-23 11:09:25 +02:00
parent 67a3e75cee
commit 32dd28a565
6 changed files with 40 additions and 7 deletions

View File

@ -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): def numberPyramid(length):
for i in range(length): for i in range(length):
@ -26,5 +30,5 @@ def solutionWithGenerator(n):
if __name__ == "__main__": if __name__ == "__main__":
n = int(input('n: ')) n = inputTypeCheck('n: ', int)
print(solutionWithForLoops(n)) print(solutionWithForLoops(n))

View File

@ -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): def numberPyramid(length):
for i in range(length): for i in range(length):
print('X', ' ' * i + 'X') print('X', ' ' * i + 'X')
@ -10,5 +16,5 @@ def numberPyramidDoubleLoop(length):
if __name__ == "__main__": if __name__ == "__main__":
n = int(input('n: ')) n = inputTypeCheck('n: ', int)
numberPyramidDoubleLoop(n) numberPyramidDoubleLoop(n)

View File

@ -1,5 +1,10 @@
from math import sqrt 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) # O(√n)
def isPrime(n): def isPrime(n):
@ -37,7 +42,7 @@ def factorize(n):
if __name__ == "__main__": if __name__ == "__main__":
n = int(input('Skriv inn et positivt heltall: ')) n = inputTypeCheck('Skriv inn et positivt heltall: ', int)
factors = factorize(n) factors = factorize(n)
if len(factors) == 1: if len(factors) == 1:
print(f'{n} er et primtall') print(f'{n} er et primtall')

View File

@ -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): def alternateSum(n):
positiveNumbers = [ positiveNumbers = [
num**2 for num in [i for i in range(1, n + 1) if i % 2 != 0] 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__": if __name__ == "__main__":
n = int(input('n: ')) n = inputTypeCheck('n: ', int)
print(alternateSum(n)) print(alternateSum(n))

View File

@ -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(): def AlternateNumberGenerator():
isEven = lambda n: n % 2 == 0 isEven = lambda n: n % 2 == 0
n = 1 n = 1
@ -24,5 +30,5 @@ def alternateSumStopAt(k):
if __name__ == "__main__": if __name__ == "__main__":
k = int(input('k: ')) k = inputTypeCheck('k: ', int)
alternateSumStopAt(k) alternateSumStopAt(k)

View File

@ -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): def fibonacciIterative(n):
k1 = 0 k1 = 0
k2 = 1 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)] fibonacciList = lambda n: [fibonacciIterative(i) for i in range(1, n + 1)]
if __name__ == "__main__": if __name__ == "__main__":
n = int(input('n: ')) n = inputTypeCheck('n: ', int)
print(f'a) Fibonacci({n})', fibonacciIterative(n)) print(f'a) Fibonacci({n})', fibonacciIterative(n))
print(f'b) Sum av Fibonacci(1..{n})', fibonacciSum(n)) print(f'b) Sum av Fibonacci(1..{n})', fibonacciSum(n))