Practice Problem "Object Oriented Python", Page 5/8

My Code:

class FreqTable:
    
    def __init__(self):
        self.count = {}
        
    def add(self, element):
        if not element in self.count:
            self.count[element] = 0
        else:
            self.count[element] += 1
        return self.count[element]
    
    def get_count(self, element):
        if element in self.count:
            return self.count[element]
        else:
            return 0

freq_table = FreqTable()
for _ in range(3):
    freq_table.add(0)

print(freq_table.get_count(0))
print(freq_table.get_count(1))

Expected Result:

3
0

Actual Result:

2
0

Why does only following code return the expected result:

class FreqTable:
    
    def __init__(self):
        self.count = {}
        
    def add(self, element):
        if not element in self.count:
            self.count[element] = 0
        self.count[element] += 1
        return self.count[element]
    
    def get_count(self, element):
        if element in self.count:
            return self.count[element]
        else:
            return 0

freq_table = FreqTable()
for _ in range(3):
    freq_table.add(0)

print(freq_table.get_count(0))
print(freq_table.get_count(1))

First, spend some time on the differences between the two solutions.

Yours -

if not element in self.count:
    self.count[element] = 0
else:
    self.count[element] += 1

The other -

if not element in self.count:
    self.count[element] = 0
self.count[element] += 1

Focus on the differences above, then work through how count gets updated in each of the above and see why it’s different. Relate that to what you are trying to calculate with the above as well.

If you get stuck and have specific questions beyond the above, feel free to ask.

1 Like

Thanks for your reply!

The only difference is “else” and the indentation of the second self.count[element], which I already noticed.

I still don’t see why the result is 2 and 0 with the else clause.

If you could explain the concepts behind it, it would be appreciated.
Thanks

1 Like

You are trying to find the count here of something. If I give you the following list - a = [0, 0, 0], you will say that there are 3 0s in that list.

So, now look at your code again and see from where you start your counting. You always start from 0. So, your count only updates till 2. You are still correct because you are counting from 0 to 2, that’s still a total of 3 (0, 1, 2). But since you are trying to find the number of times an element occurs (the count), you need to start from 1 instead.

That’s the difference that else makes. In the alternative code, the first count is set to 0, but it is till updated to 1 after that because that indicates the correct number of times that element occurs.

2 Likes

Thanks for your reply. So in the end, I could also write the code as follows:

class FreqTable():
    
    def __init__(self):
        self.count = {}
        
    def add(self, element):
        if element in self.count:
            self.count[element] += 1
        else:
            self.count[element] = 1
        #if not element in self.count:
        #    self.count[element] = 0
        #self.count[element] += 1
        return self.count[element]
    
    def get_count(self, element):
        if element in self.count:
            return self.count[element]
        else:
            return 0

freq_table = FreqTable()
for _ in range(3):
    freq_table.add(0)

print(freq_table.get_count(0))
print(freq_table.get_count(1))

This will also output

3
0

Please let me know, if that’s not correct.
If it’s correct, this makes it much clearer to me and connect the practice problems to the mission even better.

1 Like

Yes, your approach is correct.

I am, honestly, not sure why DQ’s solution set it to 0 first and then updates it. Given the context of the problem, yours is a more straightforward approach.

2 Likes