This commit is contained in:
Oystein Kristoffer Tveit 2020-09-14 16:36:09 +02:00
parent 1b152f4482
commit cc7533e152
5 changed files with 35 additions and 20 deletions

8
10.py
View File

@ -1,5 +1,6 @@
import os import os
class game(): class game():
def __init__(self): def __init__(self):
self.secret_word = input('Skriv inn det hemmelige ordet: ') self.secret_word = input('Skriv inn det hemmelige ordet: ')
@ -8,10 +9,12 @@ class game():
os.system('clear') os.system('clear')
def getWord(self): def getWord(self):
return ''.join([('*' if (ch in self.lettersLeft) else ch) for ch in list(self.secret_word)]) return ''.join([('*' if (ch in self.lettersLeft) else ch)
for ch in list(self.secret_word)])
def removeLetterFromLettersLeft(self, letter): def removeLetterFromLettersLeft(self, letter):
self.lettersLeft = list(filter(lambda leftLetter: leftLetter != letter, self.lettersLeft)) self.lettersLeft = list(
filter(lambda leftLetter: leftLetter != letter, self.lettersLeft))
def gameOver(self): def gameOver(self):
print('Du har ingen liv igjen.') print('Du har ingen liv igjen.')
@ -48,6 +51,7 @@ class game():
self.update() self.update()
input("Trykk enter for å fortsette...") input("Trykk enter for å fortsette...")
if __name__ == "__main__": if __name__ == "__main__":
myGame = game() myGame = game()
myGame.loop() myGame.loop()

View File

@ -1,11 +1,11 @@
def numberPyramid(length): def numberPyramid(length):
for i in range(length): for i in range(length):
row = '' row = ''
for k in range(i+1): for k in range(i + 1):
row += f'{k+1} ' row += f'{k+1} '
print(row) print(row)
def numberPyramidGenerator(): def numberPyramidGenerator():
currentList = ['1'] currentList = ['1']
while True: while True:
@ -16,11 +16,13 @@ def numberPyramidGenerator():
def solutionWithForLoops(n): def solutionWithForLoops(n):
return numberPyramid(n) return numberPyramid(n)
def solutionWithGenerator(n): def solutionWithGenerator(n):
myGenerator = numberPyramidGenerator() myGenerator = numberPyramidGenerator()
for i in range(n): for i in range(n):
print(next(myGenerator)) print(next(myGenerator))
if __name__ == "__main__": if __name__ == "__main__":
n = int(input('n: ')) n = int(input('n: '))
print(solutionWithForLoops(n)) print(solutionWithForLoops(n))

View File

@ -1,14 +1,15 @@
def fibonacciIterative(n): def fibonacciIterative(n):
k1 = 0 k1 = 0
k2 = 1 k2 = 1
for i in range(n-1): for i in range(n - 1):
previousK2 = k2 previousK2 = k2
k2 = k1 + k2 k2 = k1 + k2
k1 = previousK2 k1 = previousK2
return k1 return k1
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)] 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__": if __name__ == "__main__":
n = int(input('n: ')) n = int(input('n: '))

View File

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

View File

@ -5,19 +5,23 @@ def AlternateNumberGenerator():
while True: while True:
yield n yield n
n = n + (-counter**2 if isEven(counter) else counter**2) n = n + (-counter**2 if isEven(counter) else counter**2)
counter+=1 counter += 1
def alternateSumStopAt(k): def alternateSumStopAt(k):
numGen = AlternateNumberGenerator() numGen = AlternateNumberGenerator()
previousN = None previousN = None
n = next(numGen) n = next(numGen)
iterations = 0 iterations = 0
while n<k: while n < k:
previousN = n previousN = n
n = next(numGen) n = next(numGen)
iterations += 1 iterations += 1
print(f'Summen av tallene før summen blir større enn k er {previousN}. Antall iterasjoner: {iterations}') print(
f'Summen av tallene før summen blir større enn k er {previousN}. Antall iterasjoner: {iterations}'
)
if __name__ == "__main__": if __name__ == "__main__":
k = int(input('k: ')) k = int(input('k: '))