Add exercise 6

This commit is contained in:
Oystein Kristoffer Tveit 2020-10-12 17:23:02 +02:00
parent a2ea83fcbb
commit 15fed3634d
5 changed files with 121 additions and 0 deletions

32
Exercise 6/11.py Normal file
View File

@ -0,0 +1,32 @@
from random import randint
def random_matrise(x, y):
return [[randint(0, 9) for _ in range(x)] for _ in range(y)]
def print_matrise(matrix, title):
print(f'{title}=[')
for row in matrix:
print('\t', row)
print(']')
def matrise_addisjon(A, B):
try:
assert len(A) == len(B) and len(A[0]) == len(B[0])
newMatrix = [[A[x][y] + B[x][y] for y in range(len(A[0]))] for x in range(len(A))]
return newMatrix
except:
print('Matrisene er ikke av samme dimensjon')
def main():
A = random_matrise(4,3)
print_matrise(A, 'A')
B = random_matrise(3,4)
print_matrise(B, 'B')
C = random_matrise(3,4)
print_matrise(C, 'C')
D = matrise_addisjon(A,B)
E = matrise_addisjon(B,C)
print_matrise(E, 'B+C' )
if __name__ == "__main__":
main()

11
Exercise 6/7.py Normal file
View File

@ -0,0 +1,11 @@
def separate(numbers, threshold):
return(
[num for num in numbers if num < threshold],
[num for num in numbers if num >= threshold],
)
def multiplication_table(n):
return [[(x+1)*(y+1) for x in range(n)] for y in range(n)]
if __name__ == "__main__":
print(multiplication_table(4))

60
Exercise 6/8.py Normal file
View File

@ -0,0 +1,60 @@
from random import sample
import threading
def guessInput(amount):
while True:
try:
answer = input(f'Gjett {amount} tall\n').split(' ')
assert len(answer) == amount
return [int(num) for num in answer]
except:
print(f'Er du sikker på at du har skrevet inn {amount} tall? Prøv igjen.')
drawNumber = lambda numList, amount: sample(numList, amount)
compList = lambda list1, list2: len([1 for elem in list1 if elem in list2])
def Winnings(rightNums, rightExtraNums):
isBiggerThan = lambda rn, ren: rightNums >= rn and rightExtraNums >= ren
if isBiggerThan(7,0): return 2749455
elif isBiggerThan(6,1): return 102110
elif isBiggerThan(6,0): return 3385
elif isBiggerThan(5,0): return 95
elif isBiggerThan(4,1): return 45
else: return 0
numbers = list(range(1,35))
calculateRightNumbers = lambda list: compList(list, drawNumber(numbers, len(list)))
combinedRightNumbers = lambda list1, list2: Winnings(calculateRightNumbers(list1), calculateRightNumbers(list2))
def main():
myGuess = guessInput(7)
print('Nå skal du gjette ekstra-tall')
myExtraGuess = guessInput(3)
print('Du vant {} kroner'.format(combinedRightNumbers(myGuess, myExtraGuess) - 5))
# g)
def calculateMillionSum(threadNum):
timesPerTread = int(1000000/threadNum)
results = []
def threadTarget():
partialSum = sum([combinedRightNumbers(sample(numbers, 7), sample(numbers, 3)) - 5 for _ in range(timesPerTread)])
results.append(partialSum)
threads = [threading.Thread(target=threadTarget) for _ in range(threadNum)]
for thread in threads:
thread.start()
for thread in threads:
thread.join()
return sum(results)
if __name__ == "__main__":
# main()
result = calculateMillionSum(64)
print(result)

18
Exercise 6/9.py Normal file
View File

@ -0,0 +1,18 @@
def tooth(g):
result = []
coins = [20, 10, 5, 1]
def findCoinAmount(gramsLeft, coinIndex):
result.append(gramsLeft//coins[coinIndex])
if coinIndex != len(coins) - 1:
findCoinAmount(gramsLeft%coins[coinIndex], coinIndex+1)
findCoinAmount(g, 0)
return result
if __name__ == "__main__":
teeth = [95,103,71,99,114,64,95,53,97,114,109,11,2,21,45,2,26,81,54,14,118,108,117,27,115,43,70,58,107]
prettyPrint = lambda tooth: print(f'20: {tooth[0]}, 10: {tooth[1]}, 5: {tooth[2]}, 1: {tooth[3]}')
for grams in teeth:
prettyPrint(tooth(grams))

0
Exercise 6/__init__.py Normal file
View File