-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathL4ProblemSet1.py
74 lines (59 loc) · 1.56 KB
/
L4ProblemSet1.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
s = 'ohjelewfhricdfwiukkmauoi'
n = 0 # number of vowels
for letter in s:
letter = letter.lower()
if letter in ['a', 'e', 'i', 'o', 'u']:
n += 1
result = 'Number of vowels: %u' % n
print(result)
print('=======')
s = 'azcbobobegghakl'
n = 0 # number of times bob occurs
i = 0 # last location bob was found
found = True # was bob found?
while found:
i = s.find('bob', i)
if i >= 0:
n += 1
else:
found = False
i += 1
print('Number of times bob occurs is: %u' % n)
print('=======')
def item_order(order):
"""
Counts the number of each menu item based on a space separated string of
customer orders
:rtype: None
:param: Str - a space separated list of customer orders
"""
salad = 0
hamburger = 0
water = 0
salad = countSubString(order, 'salad')
hamburger = countSubString(order, 'hamburger')
water = countSubString(order, 'water')
print('salad:%u hamburger:%u water:%u' % (salad, hamburger, water))
return
def countSubString(s, ss):
"""
Counts the
:rtype: int
:param s: str - a string that may contain the substring
:param ss: str - a substring to look for
"""
n = 0 # number of times bob occurs
i = 0 # last location bob was found
found = True # was bob found?
while found:
i = s.find(ss, i)
if i >= 0:
n += 1
else:
found = False
i += 1
return n
order = "salad water hamburger salad hamburger"
item_order(order)
order = "hamburger water hamburger"
item_order(order)