added week 4
This commit is contained in:
parent
465dfcb4b5
commit
4f6875ab1b
15
ex_13.py
Normal file
15
ex_13.py
Normal file
|
@ -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)")
|
||||
|
10
ex_14.py
Normal file
10
ex_14.py
Normal file
|
@ -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)
|
||||
|
||||
|
45
ex_16.py
Normal file
45
ex_16.py
Normal file
|
@ -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 ]))
|
Loading…
Reference in New Issue
Block a user