-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFunctions.py
106 lines (71 loc) · 1.7 KB
/
Functions.py
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#####Functions#####
#pass variables as an arguments
def add(a,b):
c=a+b
return print(a," + ",b," = ",c)
add(1,2)
#Pass list as an argument
def addlist(n):
a=0
for i in n:
a+=i
return(a)
y=[10,20,30,50,60,80,90,100]
print(addlist(y))
def add(a,b): c=a+b return print(a," + ",b," = ",c)
#Arbitary arguments
def multiply(*z):
x=1
for i in z:
x*=i
return x
multiply(2,4,6,8,10)
#recurive functions
#factorial
def fact(n):
if n == 1:
return n
elif n == 0:
return 1
elif n < 1:
return ("Number should be greater than 1")
else:
return n*fact(n-1)
print(fact(int(0)))
#Map function
strings = ["map","reduce","filter"]
uppercase=list(map(str.upper,strings))
print(uppercase)
#Filter Function
numbers=range(1,100)
def multiples5(n):
if n%5==0:
return n
number_divisibleby5 = list(filter(multiples5, numbers))
print(number_divisibleby5)
#reduce function
from functools import reduce
numbers=range(1,10)
def add(a,b):
return a+b
output= reduce(add,numbers)
print(output)
#with initial value
numbers=range(1,10)
def add(a,b):
return a+b
output= reduce(add,numbers,100)
print(output)
#lambda function
#map with lambda
strings = ["map","reduce","filter"]
uppercase=list(map(lambda x: x.upper(),strings))
print(uppercase)
#filter with lambda
numbers=range(1,100)
number_divisibleby5 = list(filter(lambda n:n%5==0, numbers))
print(number_divisibleby5)
#reduce with lambda
numbers=range(1,10)
output= reduce(lambda a,b:a+b,numbers)
print(output)