-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLabfour.py
151 lines (88 loc) · 3.1 KB
/
Labfour.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
from functools import reduce
import operator
'''
Map
Recall from class that map(func, iterable) applies a function over elements of an iterable.
'''
listone = ["12","-2","0"]
newlistone = list(map(int,listone))
# print(newlistone)
listtwo = ["hello","world"]
newlisttwoone = list(map(len,listtwo))
# print(newlisttwoone)
def reverse(L):
return L[::-1]
newlisttwotwo = list(map(reverse,listtwo))
# print(newlisttwotwo)
'''
Filter
Recall from class that map(func, iterable) applies a function over elements of an iterable.
'''
listthree = ['12', '-2', '0']
newlist = list(map(int,listthree))
newlistthree = list(filter(lambda x: x>=0,newlist))
# print(newlistthree)
# print(operator.add(1, 2))
# print(operator.mul(3, 10))
# print(operator.pow(2, 3))
# def fact(n):
# return functools.reduce(operator.mul, range(1, n+1))
# print(fact(3))
'''
words = ['pear', 'cabbage', 'apple', 'bananas']
min(words) # => 'apple'
words.sort(key=lambda s: s[-1]) # Alternatively, key=operator.itemgetter(-1)
words # => ['cabbage', 'apple', 'pear', 'bananas'] ... Why 'cabbage' > 'apple'?
max(words, key=len) # 'cabbage' ... Why not 'bananas'?
min(words, key=lambda s: s[1::2]) # What will this value be?
'''
'''
Replacing Control Flow
The first thing that needs to go are control flow statements - if/elif/else. Luckily, Python, like many other languages, short circuits boolean expressions. This means that we can rewrite
if <cond1>: func1()
elif <cond2>: func2()
else: func3()
as the equivalent expression
(<cond1> and func1()) or (<cond2> and func2()) or (func3())
Recalling Python's rules for short-circuiting boolean expressions, why does the above expression (usually) result in the same output as the procedural control flow case?
Note: The above works if and only if all of the functions return truthy values. In order to guarantee that these expressions are actually the same, you might have to write something like the following, because all two-element tuples are truthy regardless of their content.
((<cond1> and (func1(), 0)) or (<cond2> and (func1(), 0)) or ((func1(), 0)))[0]
'''
#Example
'''
if score == 1:
return "Winner"
elif score == -1:
return "Loser"
else:
return "Tied"
'''
echo = lambda arg: arg
result = lambda score: (score == 1 and echo("Winner")) or (score == -1 and echo("Loser")) or echo("Tied")
# print(result(-1))
it = iter(range(100))
import itertools
import operator
for el in itertools.permutations('XKCD', 2):
print(el, end=', ')
def parent(num):
def first_child():
return "Hi, I am Emma"
def second_child():
return "Call me Liam"
if num == 1:
return first_child
else:
return second_child
def my_decorator(func):
def wrapper():
print("Something is happening before the function is called.")
func()
print("Something is happening after the function is called.")
return wrapper
def say_whee():
print("Whee!")
say_whee = my_decorator(say_whee)
print(say_whee)
# Put simply: decorators wrap a function, modifying its behavior.
# https://realpython.com/primer-on-python-decorators/