-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathlinq-aggregate.py
205 lines (124 loc) · 4.78 KB
/
linq-aggregate.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import shared
from types import SimpleNamespace
from itertools import groupby
from functools import reduce
import operator
def linq73():
factors_of_300 = [2, 2, 3, 5, 5]
unique_factors = len(set(factors_of_300))
print("There are %d unique factors of 300." % unique_factors)
def linq74():
numbers = [5, 4, 1, 3, 9, 8, 6, 7, 2, 0]
odd_numbers = sum(n % 2 == 1 for n in numbers)
print("There are %d odd numbers in the list." % odd_numbers)
def linq76():
customers = shared.getCustomerList()
order_counts = map(lambda cust: SimpleNamespace(CustomerID=cust.CustomerID,
OrderCount=len(cust.Orders)),
customers)
shared.print_namespace(order_counts)
def linq77():
products = shared.getProductList()
sorted_by_category = sorted(products, key=lambda p: p.Category)
grouped_by_category = groupby(sorted_by_category, key=lambda p: p.Category)
category_counts = map(lambda g: SimpleNamespace(Category=g[0],
ProductCount=len(list(g[1]))),
grouped_by_category)
shared.print_namespace(category_counts)
def linq78():
numbers = [5, 4, 1, 3, 9, 8, 6, 7, 2, 0]
num_sum = sum(numbers)
print("The sum of the numbers is %d." % num_sum)
def linq79():
words = ["cherry", "apple", "blueberry"]
total_chars = sum(len(w) for w in words)
print("There are a total of %d characters in these words." % total_chars)
def linq80():
products = shared.getProductList()
sorted_by_category = sorted(products, key=lambda p: p.Category)
grouped_by_category = groupby(sorted_by_category, key=lambda p: p.Category)
category_counts = map(lambda g: SimpleNamespace(Category=g[0],
TotalUnitsInStock=sum(p.UnitsInStock for p in g[1])),
grouped_by_category)
shared.print_namespace(category_counts)
def linq81():
numbers = [5, 4, 1, 3, 9, 8, 6, 7, 2, 0]
min_num = min(numbers)
print("The minimum number is %d" % min_num)
def linq82():
words = ["cherry", "apple", "blueberry"]
shortest_word = min(len(w) for w in words)
print("The shortest word is %d characters long." % shortest_word)
def linq83():
products = shared.getProductList()
sorted_by_category = sorted(products, key=lambda p: p.Category)
grouped_by_category = groupby(sorted_by_category, key=lambda p: p.Category)
category_cheapest_price = map(lambda g: SimpleNamespace(Category=g[0],
CheapestPrice=min(p.UnitPrice for p in g[1])),
grouped_by_category)
shared.print_namespace(category_cheapest_price)
def linq84():
pass
def linq85():
numbers = [5, 4, 1, 3, 9, 8, 6, 7, 2, 0]
max_num = max(numbers)
print("The maximum number is %d." % max_num)
def linq86():
words = ["cherry", "apple", "blueberry"]
longest_word = max(len(w) for w in words)
print("The longest word is %d characters long." % longest_word)
def linq87():
products = shared.getProductList()
sorted_by_category = sorted(products, key=lambda p: p.Category)
grouped_by_category = groupby(sorted_by_category, key=lambda p: p.Category)
category_expensive_price = map(
lambda g: SimpleNamespace(Category=g[0],
MostExpensive=max(p.UnitPrice for p in g[1])),
grouped_by_category)
shared.print_namespace(category_expensive_price)
def linq88():
pass
def linq89():
numbers = [5, 4, 1, 3, 9, 8, 6, 7, 2, 0]
average_num = sum(numbers) / float(len(numbers))
print("The average number is %f." % average_num)
def linq90():
words = ["cherry", "apple", "blueberry"]
average_length = sum(len(w) for w in words) / float(len(words))
print("The average word length is %f characters." % average_length)
def linq91():
pass
def linq92():
doubles = [1.7, 2.3, 1.9, 4.1, 2.9]
product = reduce(operator.mul, doubles)
#or
#product = reduce(lambda running_product, next_factor: running_product * next_factor, doubles)
print("Total product of all numbers: %f" % product)
def linq93():
start_balance = 100.0
attempted_withdrawals = [20, 10, 40, 50, 10, 70, 30]
end_balance = reduce(
lambda runningBalance, nextWithDrawal: runningBalance - nextWithDrawal if nextWithDrawal <= runningBalance else runningBalance,
attempted_withdrawals,
start_balance)
print("Ending balance: %f" % end_balance)
linq73()
# linq74()
# linq76()
# linq77()
# linq78()
# linq79()
# linq80()
# linq81()
# linq82()
# linq83()
# linq84()
# linq85()
# linq86()
# linq87()
# linq88()
# linq89()
# linq90()
# linq91()
# linq92()
# linq93()