From 15fed3634dbd0e1355635030e766f8a9b196df24 Mon Sep 17 00:00:00 2001 From: h7x4 Date: Mon, 12 Oct 2020 17:23:02 +0200 Subject: [PATCH] Add exercise 6 --- Exercise 6/11.py | 32 ++++++++++++++++++++++ Exercise 6/7.py | 11 ++++++++ Exercise 6/8.py | 60 ++++++++++++++++++++++++++++++++++++++++++ Exercise 6/9.py | 18 +++++++++++++ Exercise 6/__init__.py | 0 5 files changed, 121 insertions(+) create mode 100644 Exercise 6/11.py create mode 100644 Exercise 6/7.py create mode 100644 Exercise 6/8.py create mode 100644 Exercise 6/9.py create mode 100644 Exercise 6/__init__.py diff --git a/Exercise 6/11.py b/Exercise 6/11.py new file mode 100644 index 0000000..0d2ab3a --- /dev/null +++ b/Exercise 6/11.py @@ -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() \ No newline at end of file diff --git a/Exercise 6/7.py b/Exercise 6/7.py new file mode 100644 index 0000000..2acbdc6 --- /dev/null +++ b/Exercise 6/7.py @@ -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)) \ No newline at end of file diff --git a/Exercise 6/8.py b/Exercise 6/8.py new file mode 100644 index 0000000..f4a0ea1 --- /dev/null +++ b/Exercise 6/8.py @@ -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) \ No newline at end of file diff --git a/Exercise 6/9.py b/Exercise 6/9.py new file mode 100644 index 0000000..b66ed68 --- /dev/null +++ b/Exercise 6/9.py @@ -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)) \ No newline at end of file diff --git a/Exercise 6/__init__.py b/Exercise 6/__init__.py new file mode 100644 index 0000000..e69de29