Skip to content

Implemented python examples. #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions Python/1-simple.py
Original file line number Diff line number Diff line change
@@ -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
10 changes: 10 additions & 0 deletions Python/2-class.py
Original file line number Diff line number Diff line change
@@ -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;:.'))
7 changes: 7 additions & 0 deletions Python/2-closure.py
Original file line number Diff line number Diff line change
@@ -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
82 changes: 82 additions & 0 deletions Python/3-sort.py
Original file line number Diff line number Diff line change
@@ -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