From 31b3497a753904d5a894909adcca2071ba01694d Mon Sep 17 00:00:00 2001 From: Anna Yasenova Date: Tue, 9 Jan 2018 23:42:49 +0200 Subject: [PATCH] Python examples. --- Python/1-simple.py | 23 +++++++++++++ Python/2-class.py | 10 ++++++ Python/2-closure.py | 7 ++++ Python/3-sort.py | 82 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 122 insertions(+) create mode 100644 Python/1-simple.py create mode 100644 Python/2-class.py create mode 100644 Python/2-closure.py create mode 100644 Python/3-sort.py diff --git a/Python/1-simple.py b/Python/1-simple.py new file mode 100644 index 0000000..31bfdc9 --- /dev/null +++ b/Python/1-simple.py @@ -0,0 +1,23 @@ +class Accumulator(object): + ''' + __call__() allows the class instance to be called as a function + ''' + def __init__(self, number=0): + self.number = number + def __call__(self, x): + self.number += x + return self.number + + +item = Accumulator(1) + +#remember and add +print(item(5)) # 6 + +print(item(7)) # 13 + +print(item(6)) # 19 + +# a bit more complex. +# 19 + 4 = 23, 23 + 23 = 46 +print(item(item(4))) # 46 \ No newline at end of file diff --git a/Python/2-class.py b/Python/2-class.py new file mode 100644 index 0000000..c19a8dd --- /dev/null +++ b/Python/2-class.py @@ -0,0 +1,10 @@ +class Strip: + + def __init__(self, chars): + self.chars = chars + + def __call__(self, string): + return string.strip(self.chars) + +item = Strip('!?.,:;') +print(item.__call__(';!?First Second Third;:.')) \ No newline at end of file diff --git a/Python/2-closure.py b/Python/2-closure.py new file mode 100644 index 0000000..d3bf244 --- /dev/null +++ b/Python/2-closure.py @@ -0,0 +1,7 @@ +def createStrip(chars): + def stripFunc(string): + return string.strip(chars) + return stripFunc + +myStrip = createStrip('!?.,:;') +print(myStrip(';!?First Second Third;:.')) # First Second Third \ No newline at end of file diff --git a/Python/3-sort.py b/Python/3-sort.py new file mode 100644 index 0000000..ae86936 --- /dev/null +++ b/Python/3-sort.py @@ -0,0 +1,82 @@ +class Functor(object): + + def __init__(self): + pass + + def __call__(self, x) : + x_first = x[0] + if type(x_first) is int: + return self. __mergeSort(x) + if type(x_first) is float: + return self. __heapSort(x) + else: + return self.__noSort(x) + + def __mergeSort(self, lst): + print("Data is Merge sorted") + result = [] + if len(lst) < 20: + return sorted(lst) + mid = int(len(lst) / 2) + y = self.__mergeSort(lst[:mid]) + z = self.__mergeSort(lst[mid:]) + i = 0 + j = 0 + while i < len(y) and j < len(z): + if y[i] > z[j]: + result.append(z[j]) + j += 1 + else: + result.append(y[i]) + i += 1 + result += y[i:] + result += z[j:] + return result + + def __heapSort(self, x): + + def swap(lst,i, j): + lst[i], lst[j] = lst[j], lst[i] + + def heapify(lst, end, pos): + l = 2 * pos + 1 + r = 2 * (pos + 1) + max = pos + if l < end and lst[pos] < lst[l]: + max = l + if r < end and lst[max] < lst[r]: + max = r + if max != pos: + swap(lst, pos, max) + heapify(lst, end, max) + + def heap_sort(lst): + end = len(lst) + start = end // 2 - 1 + for i in range(start, -1, -1): + heapify(lst, end, i) + for i in range(end-1, 0, -1): + swap(lst, i, 0) + heapify(lst, i, 0) + + print("Data is Heap sorted") + heap_sort(x) + + return x + + def __noSort(self, lst): + print("Data isn't sorted") + return lst + +class Run(object): + def __init__(self): + self.sort = Functor() + + def mainMeth(self, x): + return self.sort(x) + +start = Run() + +print(start.mainMeth([5, 4, 6, 0, -6, 8])) # Mergesort +print(start.mainMeth([4.256, 2.23, 3.45, 5.65])) # heapsort +print(start.mainMeth(['a','m', 'r', 's','b','q'])) # without sort \ No newline at end of file