-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path9CountPets.html
More file actions
94 lines (68 loc) · 6.27 KB
/
9CountPets.html
File metadata and controls
94 lines (68 loc) · 6.27 KB
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
<h2 id="0-read-problem">0) Read Problem</h2>
<p>Write a function called <code>count_pets</code> that consumes a list of strings of pet types owned and returns the number of each type of pet as a dictionary. Use the Dictionary Counting pattern. Call your function with the following list to test it. </p>
<pre class="codehilite"><code>["Dog", "cat", "Cat", "Snake", "mouse", "snake", "dog", "dog"]</code></pre>
<p>Notice the list is written somewhat sloppy. The capitilization is inconsistent. Your function needs to be able to fix this so that “Dog” and “dog” go into the same dictionary entry. </p>
<h2 id="1-interpret-the-problem">1) Interpret the Problem</h2>
<p>This problem requires us to write a function that uses the Dictionary Counting pattern to create a dictionary that maps pet type to number of pets of that type. The problem also says the words in the list can have inconsistent capitalization and our function has to treat two of the same word as one key in the dictionary. </p>
<h2 id="2-write-test">2) Write Test</h2>
<p>Once we understand the problem, it helps to think of the possible cases we can test. If we do these first, we can run our code as we go to make sure we’re catching every possibility. </p>
<p>Here is a basic case with a list of strings with consistent capitalization.</p>
<pre class="codehilite"><code>pets1 = ["dog", "cat", "snake", "dog", "lizard", "cat", "cat"]
print(count_pets(pets1))
# => "{"DOG": 2, "CAT": 3, "SNAKE": 1, "LIZARD": 1]</code></pre>
<p>The case given in the problem, where the capitalization is inconsistent</p>
<pre class="codehilite"><code>pets2 = ["Dog", "cat", "Cat", "Snake", "mouse", "snake", "dog", "dog"]
print(count_pets(pets2))
# => "{"DOG": 3, "CAT": 2, "SNAKE": 2, "MOUSE": 1]</code></pre>
<p>A case with an empty list</p>
<pre class="codehilite"><code>print(count_pets([])
# => "{}"</code></pre>
<h2 id="3-create-function-header">3) Create function header</h2>
<p>Our function should be called <code>count_pets</code> and take 1 parameter. The parameter holds a list of strings representing types of pets so a good name would be <code>pets</code></p>
<pre class="codehilite"><code>def count_pets(pets):
...</code></pre>
<h2 id="4-initialize-the-dictionary">4) Initialize the Dictionary</h2>
<p>The first thing we need to do is create a dictionary to add entries to</p>
<pre class="codehilite"><code>pets_count = {}</code></pre>
<h2 id="5-iterate-over-the-list">5) Iterate over the list</h2>
<p>To check at each entry in the pets list, we should use a for loop. This for loop will look at every string in the list, one at a time. The “item” variable is the variable that will hold each value in the list as your loop repeats. Since we have a pets list, we should name our item variable pet.</p>
<pre class="codehilite"><code>for pet in pets:
...</code></pre>
<h2 id="6-normalizing-strings">6) Normalizing Strings</h2>
<p>Since the strings in the list could have inconsistent capitalization, we need a way to “normalize” the data. In programming, “normalize” usually means to take data that is in some way inconsistent and change it in a way that makes it easier to process. </p>
<p>The easiest way to normalize strings that have inconsistent capitalization is to change them all to the same capitalization. This can be done using the <code>upper</code> or <code>lower</code> functions that return the string in all caps and all lowercase, respectively. Since <code>"Dogs".upper()</code> is <code>"DOGS"</code> and <code>"dogs".upper()" is also</code>“DOGS”`, these two strings can now be seen as the same. </p>
<p>It doesn’t matter which of these we use, so we’ll use <code>upper</code>. We’ll save this uppercase string in a new variable to use later.</p>
<pre class="codehilite"><code>petUpper = pet.upper()</code></pre>
<h2 id="7-implementing-the-dictionary-count-patter">7) Implementing the Dictionary Count Patter</h2>
<p>Now we need to update our dictionary for each pet using the Dictionary Count pattern. If the dictionary already contains a key matching the current pet, we should increment the value of the counter value for that key. </p>
<pre class="codehilite"><code>if petUpper in pets_count:
pets_count[petUpper] = pets_count[petUpper] + 1</code></pre>
<p>If the key doesn’t exist, we should create it and give its counter value an initial value of 1</p>
<pre class="codehilite"><code>if petUpper in pets_count:
pets_count[petUpper] = pets_count[petUpper] + 1
else:
pets_count[petUpper] = 1</code></pre>
<p>Notice we use petUpper as the key. This makes sure that we’re not adding “dog” and “Dog” as separate keys. Both string will become a single key “DOG”. </p>
<h2 id="8-returning-the-result">8) Returning the Result</h2>
<p>Once our loop finishes, all we have left to do is return the resulting dictionary</p>
<pre class="codehilite"><code>return pets_count</code></pre>
<h2 id="solution">Solution</h2>
<p>With all the pieces put together we get our solution, which passes the tests we wrote when run! </p>
<pre class="codehilite"><code>def count_pets(pets):
pets_count = {}
for pet in pets:
petUpper = pet.upper()
if petUpper in pets_count:
pets_count[petUpper] = pets_count[petUpper] + 1
else:
pets_count[petUpper] = 1
return pets_count
# Tests
pets1 = ["dog", "cat", "snake", "dog", "lizard", "cat", "cat"]
print(count_pets(pets1))
# => "{"DOG": 2, "CAT": 3, "SNAKE": 1, "LIZARD": 1]
pets2 = ["Dog", "cat", "Cat", "Snake", "mouse", "snake", "dog", "dog"]
print(count_pets(pets2))
# => "{"DOG": 3, "CAT": 2, "SNAKE": 2, "MOUSE": 1]
print(count_pets([])
# => "{}"</code></pre>