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

16
10.py
View File

@ -1,5 +1,6 @@
import os
class game():
def __init__(self):
self.secret_word = input('Skriv inn det hemmelige ordet: ')
@ -8,15 +9,17 @@ class game():
os.system('clear')
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):
self.lettersLeft = list(filter(lambda leftLetter: leftLetter != letter, self.lettersLeft))
self.lettersLeft = list(
filter(lambda leftLetter: leftLetter != letter, self.lettersLeft))
def gameOver(self):
print('Du har ingen liv igjen.')
exit(0)
def gameWon(self):
print(f'Gratulerer. Ordet var {self.secret_word}')
exit(0)
@ -41,13 +44,14 @@ class game():
self.rightLetter(letter)
else:
self.wrongLetter(letter)
def loop(self):
while True:
os.system('clear')
self.update()
input("Trykk enter for å fortsette...")
if __name__ == "__main__":
myGame = game()
myGame.loop()
myGame = game()
myGame.loop()

View File

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

View File

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

View File

@ -1,9 +1,13 @@
def alternateSum(n):
positiveNumbers = [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]]
positiveNumbers = [
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)
if __name__ == "__main__":
n = int(input('n: '))
print(alternateSum(n))

View File

@ -5,19 +5,23 @@ def AlternateNumberGenerator():
while True:
yield n
n = n + (-counter**2 if isEven(counter) else counter**2)
counter+=1
counter += 1
def alternateSumStopAt(k):
numGen = AlternateNumberGenerator()
previousN = None
n = next(numGen)
iterations = 0
while n<k:
while n < k:
previousN = n
n = next(numGen)
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__":
k = int(input('k: '))