-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
54 lines (38 loc) · 2.13 KB
/
test.py
File metadata and controls
54 lines (38 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
from functools import partial
#=======================================================================================================================
def selection_sort(l, f):
length = len(l)
for current_index in range(0, length):
index_min = current_index
for j in range(current_index + 1, length):
if f(l[index_min], l[j]):
index_min = j
if index_min != current_index:
temp = l[current_index]
l[current_index] = l[index_min]
l[index_min] = temp
return l
#***********************************************************************************************************************
def reduce(initial, l, f):
return reduce(f(initial, l[0]), l[1:], f) if len(l) > 0 else initial
#***********************************************************************************************************************
def fMap(l, f):
return reduce([], l, lambda x, y: x + [f(y)])
#***********************************************************************************************************************
def fFilter(l, f):
return reduce([], l, lambda x, y: x + [y] if f(y) else x)
#***********************************************************************************************************************
def fProperty(l, f):
return reduce(False, l, lambda x, y: x or f(y))
#**********************************************************************************************************************
def fSommeSquaresEven (l):
return reduce(0, fMap(fFilter(l, lambda x : x%2 == 0), lambda x : x*x), lambda x, y : x+y)
#**********************************************************************************************************************
def fMapPartial (integers):
return fMap(integers, lambda x : partial(lambda y : y+x))
#=======================================================================================================================
print(selection_sort([3, 7, 1], lambda x, y: x > y))
print(reduce(1, [2, 2, 3], lambda x, y: x + y))
print(fMap([2, 2, 4], lambda x: x + 1))
print(fFilter([2, 3, 4, 5, 2], lambda x : x > 3))
print (fSommeSquaresEven([1, 3, 2]))