-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhomework1_andor.py
83 lines (68 loc) · 3.36 KB
/
homework1_andor.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
# Here are some issue objects, like we use in issues and ambulancia
issues = [
{'id': 1, 'description': "Hello", 'category': 'Dengue', 'created_by': 'Peter'},
{'id': 2, 'description': "Hello World", 'category': 'Diabetes', 'created_by': 'Anders'},
{'id': 3, 'description': "Bondia World", 'category': 'Drogue', 'created_by': 'Ony'},
{'id': 4, 'description': "Bondia Mundo", 'category': 'Malaria', 'created_by': 'Ano'},
{'id': 5, 'description': "Goodbye", 'category': 'Dengue', 'created_by': 'Nando'},
{'id': 6, 'description': "Ate aban", 'category': 'Diabetes', 'created_by': 'Niko'},
{'id': 7, 'description': "Ate hasoru malu", 'category': 'Drogue', 'created_by': 'Mario'},
{'id': 8, 'description': "Hello", 'category': 'Malaria', 'created_by': 'Peter'},
{'id': 9, 'description': "Asu", 'category': 'Ran Fakar', 'created_by': 'Anders'},
{'id': 10, 'description': "Busa", 'category': 'PArtus', 'created_by': 'David'},
]
# Write a function that counts issues in a category
def count_category(issues, category):
count = 0
for issue in issues:
if str(issue['category']) == category:
count += 1
def test_count_category():
assert count_category(issues, 'Dengue') == 2
assert count_category(issues, 'Ran Fakar') == 1
assert count_category(issues[3:9], 'Diabetes') == 1
# Write a function that counts issues created by a person
def count_created_by(issues, created_by):
count=0
for issue in issues:
if created_by == str(issue['created_by']):
count += 1
if created_by.count(created_by) == count:
return created_by.count(created_by)
else:
return count
def test_count_created_by():
assert count_created_by(issues, 'Peter') == 2
assert count_created_by(issues, 'David') == 1
assert count_created_by(issues, 'David') == 1
assert count_created_by(issues, 'Wicher') == 0
# Write a function that reurns the number of issues that are of a category AND created by someone
def count_category_and_created_by(issues, category, created_by):
count = 0
for issue in issues:
if (created_by == str(issue['created_by'])) & (category == str(issue['category'])):
count += 1
if category.count(category) == count:
return category.count(category)
else:
return count
def test_count_category_and_created_by():
assert count_category_and_created_by(issues, 'Dengue', 'Nando') == 1
assert count_category_and_created_by(issues, 'Dengue', 'David') == 0
assert count_category_and_created_by(issues, 'Drogue', 'Ony') == 1
assert count_category_and_created_by(issues, 'Drogue', 'Mario') == 1
# Write a function that reurns the number of issues that are of a category OR created by someone
def count_category_or_created_by(issues, category, created_by):
count = 0
for issue in issues:
if (created_by == str(issue['created_by'])) | (category == str(issue['category'])):
count += 1
if category.count(category) == count:
return category.count(category)
else:
return count
def test_count_category_or_created_by():
assert count_category_or_created_by(issues, 'Dengue', 'Nando') == 2
assert count_category_or_created_by(issues, 'Dengue', 'David') == 3
assert count_category_or_created_by(issues, 'Drogue', 'Ony') == 2
assert count_category_or_created_by(issues, 'Drogue', 'Mario') == 2