Content-Length: 355236 | pFad | http://github.com/farhanjaved47/python/commit/9f6535a28accd8a5193dcb25182cba19d2821410

92 Working with filter and map functions · farhanjaved47/python@9f6535a · GitHub
Skip to content

Commit 9f6535a

Browse files
committed
Working with filter and map functions
1 parent 8ce04af commit 9f6535a

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
"""
2+
Farhan Javed
3+
farhan.javed47@gmail.com
4+
12/30/2019
5+
Part 2 of built in functions in the Python standard library
6+
"""
7+
8+
9+
def filterFunc(x):
10+
if x % 2:
11+
return True
12+
return False
13+
14+
15+
def filterFunc2(x):
16+
if x.isupper():
17+
return False
18+
return True
19+
20+
21+
def squareFunc(x):
22+
return pow(x, 2)
23+
24+
25+
def toGrade(x):
26+
if x >= 90:
27+
return 'A'
28+
elif x >= 80:
29+
return 'B'
30+
elif x >= 70:
31+
return 'C'
32+
elif x >= 60:
33+
return 'D'
34+
elif x >= 50:
35+
return 'E'
36+
return 'F'
37+
38+
39+
def main():
40+
nums = (1, 8, 45, 11, 221, 57, 72, 89, 92)
41+
chars = 'abCDEFghIJKlMNopQRstuvWXyz'
42+
grades = (81, 89, 52, 12, 65, 99, 72, 25, 44, 33)
43+
44+
# TODO : use filter to remove even numbers from a list
45+
odds = list(filter(filterFunc, nums))
46+
print(sorted(odds))
47+
odds_alt = [filterFunc(x) for x in nums]
48+
print(sorted(odds_alt)) # generate a list of booleans using list comprehension
49+
50+
# TODO : use filter on non-numeric sequences
51+
lowers = list(filter(filterFunc2, chars))
52+
print(lowers)
53+
54+
# TODO : use map to create a new sequence of values after applying a given function to each item
55+
squares = list(map(squareFunc, nums))
56+
print(sorted(squares))
57+
squares_alt = [pow(i, 2) for i in nums]
58+
print(sorted(squares_alt)) # can use list comprehension to achieve the same..
59+
60+
# TODO : use sorted and map to convert marks into alphabetical grades
61+
grades = sorted(grades)
62+
letter_grades = list(map(toGrade, grades))
63+
print(letter_grades)
64+
65+
66+
if __name__ == '__main__': main()

0 commit comments

Comments
 (0)








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/farhanjaved47/python/commit/9f6535a28accd8a5193dcb25182cba19d2821410

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy