Skip to content

Commit c035daa

Browse files
committed
updated pdf
1 parent 22f8aac commit c035daa

File tree

5 files changed

+64
-0
lines changed

5 files changed

+64
-0
lines changed

‎Scripts/chapter3/looping/Util/24_coupons.py‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,17 @@
55
dict(id=4, total=110, coupon_code='F15'), # F15: fixed, $15
66
]
77

8+
forcustomerincustomers:
9+
code=customer['coupon_code']
10+
ifcode=='F20':
11+
customer['discount'] =20.0
12+
elifcode=='F15':
13+
customer['discount'] =15.0
14+
elifcode=='P30':
15+
customer['discount'] =customer['total'] *0.3
16+
elifcode=='P50':
17+
customer['discount'] =customer['total'] *0.5
18+
else:
19+
customer['discount'] =0.0
20+
forcustomerincustomers:
21+
print(customer['id'], customer['total'], customer['discount'])
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
customers= [
2+
dict(id=1, total=200, coupon_code='F20'), # F20: fixed, $20
3+
dict(id=2, total=150, coupon_code='P30'), # P30: percent, 30%
4+
dict(id=3, total=100, coupon_code='P50'), # P50: percent, 50%
5+
dict(id=4, total=110, coupon_code='F15'), # F15: fixed, $15
6+
]
7+
8+
discounts={
9+
'F20': (0.0, 20.0), # each valuen is (percent, fixed)
10+
'P30': (0.3, 0.0),
11+
'P50': (0.5, 0.0),
12+
'F15': (0.0, 15.0)
13+
}
14+
15+
# for customer in customers:
16+
# code = customer['coupon_code']
17+
# percent, fixed = discounts.get(code,(0.0, 0.0))
18+
# customer['discount'] = percent * customer['total'] + fixed
19+
# for customer in customers:
20+
# print(customer['id'], customer['total'], customer['discount'])
21+
22+
23+
foriincustomers:
24+
code=i['coupon_code']
25+
percent, fixed=discounts.get(code,(0.0, 0.0))
26+
i['discount'] =percent*i['total'] +fixed
27+
forcustomerincustomers:
28+
print(i['id'], i['total'], i['discount'])
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
fromitertoolsimportcount
2+
3+
fornincount(5, 3):
4+
ifn>20:
5+
break
6+
print(n, end=', ') # instead of newline, comma and space
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
fromitertoolsimportcompress
2+
3+
data=range(10)
4+
even_selector= [1, 0] *10
5+
odd_selector= [0, 1] *10
6+
even_numbers=list(compress(data, even_selector))
7+
odd_numbers=list(compress(data, odd_selector))
8+
9+
print(even_selector)
10+
print(odd_selector)
11+
print(list(data))
12+
print(even_numbers)
13+
print(odd_numbers)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fromitertoolsimportpermutations
2+
3+
print(list(permutations('ABC')))

0 commit comments

Comments
(0)