-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathPython12_Python_Functionals
More file actions
114 lines (96 loc) · 2.79 KB
/
Python12_Python_Functionals
File metadata and controls
114 lines (96 loc) · 2.79 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
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
107
108
109
110
111
112
113
114
## Python
## Python Functionals
## Map and Lambda Function
cube = lambda x:x*x*x # complete the lambda function
# # Option 1
# def fibonacci(n):
# # return a list of fibonacci numbers
# arr = []
# a = 0
# b = 1
# for i in range(n):
# arr.append(a)
# a,b = b,a+b
# return arr
# Option 2
def fibonacci(n):
arr = []
for i in range(n):
if i<=1:
arr.append(i)
else:
arr.append(arr[-1] + arr[-2])
return arr
if __name__ == '__main__':
n = int(input())
print(list(map(cube, fibonacci(n))))
## Validating Email Addresses With a Filter
# # Option 1
# def fun(s):
# # return True if s is a valid email, else return False
# if ('@' not in s) or ('.' not in s) or (' ' in s):
# return False
# if s.count('@')>1:
# return False
# if s.count('.')>1:
# return False
# s = s.replace('@',' ')
# s = s.replace('.',' ')
# l = list(map(str, s.split()))
# if len(l)<3:
# return False
# for j in l[0]:
# if j.isalnum()==False and j!='_' and j!='-':
# return False
# for j in l[1]:
# if j.isalnum()==False:
# return False
# if len(l[2])>3 or len(l[2])==0:
# return False
# return True
# # Option 2
# def fun(s):
# # return True if s is a valid email, else return False
# if not (s.count('@')==1 and s.count('.')==1):
# return False
# s = s.replace('@','.')
# l,m,n = s.split('.')
# l = l.replace('_','').replace('-','')
# if (l.isalnum() and m.isalnum() and n.isalpha() and 0<len(n)<4 and len(l)>0 ):
# return True
# else:
# return False
# Option 3
import re
def fun(s):
return (True if re.match('[a-zA-Z0-9_-]+@[a-zA-Z0-9]+\.[a-zA-Z]{1,3}$',s) is not None else False)
def filter_mail(emails):
return list(filter(fun, emails))
if __name__ == '__main__':
n = int(input())
emails = []
for _ in range(n):
emails.append(input())
filtered_emails = filter_mail(emails)
filtered_emails.sort()
print(filtered_emails)
## Reduce Function
"""
The reduce() function applies a function of two arguments cumulatively on a list of objects in succession from left to right to reduce it to one value. Say you have a list, say [1,2,3] and you have to find its sum.
>>> reduce(lambda x, y : x + y,[1,2,3])
6
>>> reduce(lambda x, y : x + y, [1,2,3], -3)
3
"""
from fractions import Fraction
from functools import reduce
def product(fracs):
t = reduce(lambda x,y: x*y, fracs) # complete this line with a reduce statement
return t.numerator, t.denominator
if __name__ == '__main__':
fracs = []
for _ in range(int(input())):
fracs.append(Fraction(*map(int, input().split())))
result = product(fracs)
print(*result)
## end ##