-
Notifications
You must be signed in to change notification settings - Fork 12
/
Python15_Closures_and_Decorators
61 lines (48 loc) · 1.34 KB
/
Python15_Closures_and_Decorators
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
## Python
## Closures and Decorators
## Standardize Mobile Number Using Decorators
"""
Understanding Python Decorators in 12 Easy Steps!
http://simeonfranklin.com/blog/2012/jul/1/python-decorators-in-12-steps/
"""
# # Option 1
# def wrapper(f):
# def fun(l):
# f(['+91 '+c[-10:-5]+' '+c[-5:] for c in l])
# return fun
# Option 2
def wrapper(f):
def fun(l):
f(['+91 '+i[::-1][:10][::-1][:5]+' '+i[::-1][:10][::-1][5:11] for i in l])
return fun
@wrapper
def sort_phone(l):
print(*sorted(l), sep='\n')
if __name__ == '__main__':
l = [input() for _ in range(int(input()))]
sort_phone(l)
## Decorators 2 - Name Directory
import operator
# Option 1
def person_lister(f):
def inner(people):
# complete the function
return map(f, sorted(people, key=lambda x: int(x[2])))
return inner
# # Option 2
# def person_lister(f):
# def inner(people):
# g=[]
# people= sorted (people, key=lambda x : int(x[2]))
# for i in people:
# g.append(f(i))
# people=g
# return people
# return inner
@person_lister
def name_format(person):
return ("Mr. " if person[3] == "M" else "Ms. ") + person[0] + " " + person[1]
if __name__ == '__main__':
people = [input().split() for i in range(int(input()))]
print(*name_format(people), sep='\n')
## end ##