From 4f6875ab1b26cd20ed480f872f2e7bbb0ac3bf65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Kn=C3=BCttel?= Date: Wed, 14 Nov 2018 14:10:50 +0100 Subject: [PATCH] added week 4 --- ex_13.py | 15 +++++++++++++++ ex_14.py | 10 ++++++++++ ex_16.py | 45 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+) create mode 100644 ex_13.py create mode 100644 ex_14.py create mode 100644 ex_16.py diff --git a/ex_13.py b/ex_13.py new file mode 100644 index 0000000..4b43af7 --- /dev/null +++ b/ex_13.py @@ -0,0 +1,15 @@ +from math import log10 + +def recursive_loss(n): + if(n == 1): + return 1 + else: + return 2**(n - 1) + recursive_loss(n - 1) + +if( __name__ == "__main__"): + + print("On an 8x8 board you loose:", recursive_loss(8*8)) + print("that is a number with " + , int(log10(recursive_loss(8*8) / 100)) + , "zeros (in Euros)") + diff --git a/ex_14.py b/ex_14.py new file mode 100644 index 0000000..0edc0c5 --- /dev/null +++ b/ex_14.py @@ -0,0 +1,10 @@ +from collections import Counter + +names = "Faramarz Christian Mathias Johannes Benedikt Lukas Markus André Fe- lix Maria Christian Georg Luca Niklas Pauline Simon Teresa Robert Muriel Corinna Christina Ahmet Johannes Daniel Patrick Dominik Fabian Florian Urs 1Benedikt Christoph Caspar Alexander Thomas Birgit Leonard Joachim Ca- rina Lisa Daniel Christina Sabrina Lea Nhat Verena Denise Marlene Vincent Maximilian Daniel Niklas Martin Maximilian Enrico Michael Barbara Jakob Luis Cedrik Ronja Elena Alex Furkan Stefano Andriy Valentina Katharina Eva Veronika Kenneth Rudolf Elisabeth Christoph Laurin Daniel Johannes Simon Florian Maximilian Patrick Sebastian Fabian Anastasia Stefan Stefan Jonathan Nico Juliane Niclas Martino Jonas Markus Tuan Maximilian Adrian Stefan Jan- nis Verena Emily Simon Alexander Kilian Tina Viola" + +counter = Counter(names.split()) + +for k,v in counter.most_common(): + print(k, ":", v) + + diff --git a/ex_16.py b/ex_16.py new file mode 100644 index 0000000..7a889e3 --- /dev/null +++ b/ex_16.py @@ -0,0 +1,45 @@ +from collections import deque + + + +def mergesort(iterable): + if(len(iterable) == 1): + return iterable + + mid = len(iterable) // 2 + left = mergesort(iterable[:mid]) + right = mergesort(iterable[mid:]) + + result = deque() + while(left or right): + if(left): + cmp_left = left[0] + else: + cmp_left = None + if(right): + cmp_right = right[0] + else: + cmp_right = None + + if(cmp_left != None + and cmp_right != None): + if(cmp_left < cmp_right): + result.append(left.pop(0)) + else: + result.append(right.pop(0)) + continue + if(cmp_left != None): + result.append(left.pop(0)) + continue + if(cmp_right != None): + result.append(right.pop(0)) + continue + return list(result) + +if( __name__ == "__main__"): + + test_data = [0, 1, 4, 5, 2, 9, 12, 3] + + assert mergesort(test_data) == list(sorted(test_data)) + print(mergesort([ 5 , 4 , 21 , 11 , 1 , 17 , 20 , 2 , 3 , 7 , 8 , 22 , 6 , + 10 , 13 , 14 , 18 , 15 , 16 , 19 , 12 , 9 , 0 ]))