11.count in Practice Makes Perfect - expected outcome?


#1

I haven't started my code yet, so I'm not looking for code answers. In thinking about how to approach this, I wasn't sure what the expected outcome would be if the item passed in is a list.

Here's my question with an example: should count([1,2,1,1], [1,2]):

A. return 1 (because the sequence 1, 2 appears exactly like that 1 time)
B. return 3, 1 (because 1 appears 3 times and 2 appears 1 time)
C. return 4 (because 1 appears 3 times plus 2 appears 1 time)

Add your code here, between the ```'s!

#2

The length of an iterable is measured in index positions (essentially, rows). It is always one more than the hightest index. The above has a length of 2. There are two lists. What they contain, or their similarities does not enter into this.


#3

I don't understand. The whole point of the exercise is to compare one argument in a function (which could be a string, int, float, or another list) to the other argument, which is a list.

This is what the instructions say: "Define a function called count that has two arguments called sequence (a list) and item. Return the number of times the item occurs in the list."


#4

Okay, I see what you are going for. So we have a list, called sequence and a value called item. Let's pass them in the argument of the function call, as you have shown, and define the variables locally in the function:

    def count(sequence, item):
        n = 0
        for i in sequence:
            if i == item:
                n += 1
        return n

        print (count([2,3,4,5,2,4,6,7,2], 2))    # 3

Is this what you are working toward?


#5

That's the problem, I don't know what I'm working towards. Like I said, I'm not looking for code suggestions, just and idea of what the function should return if the item argument is a list.

Here's my question with an example: should count([1,2,1,1], [1,2]):

A. return 1 (because the sequence 1, 2 appears exactly like that 1 time)
B. return 3, 1 (because 1 appears 3 times and 2 appears 1 time)
C. return 4 (because 1 appears 3 times plus 2 appears 1 time)


#6

print [1,2] in [1,2,1,1]      # False

so the count will be 0.

print [1,2] in [[1,2],1,1]    # True

If we want to compare two lists of any length and return only the matches, we can use set():

print list(set([1,2]).intersection([1,2,3,4]))    # [1, 2]

Then we can iterate through that list and count the matches in the longer of the two.


#7

I'm sorry, I'm still not sure how that answers my question.


#8

Since you are passing in two or more values as the item, the question now becomes which to count, and if all, then there should be multiple results.

(1,[1,1,1]), (2, [1])  so  1 => 3; 2 => 1

If all you want is a combined total, then,

def count(seq1, seq2):
    n = 0
    for i in seq1:
        for k in seq2:
            if k == i:
                n += 1
    return n

print (count([2,3,4,5,2,4,6,7,2], [2,3]))    # 4

Without any of your working code to look at, your question only has one answer: 0, unless you break down both lists.


#9

Right, which is what I was asking - does the Codecademy exercise expect us to break down both lists or not. I've done the code to count the items without breaking down both lists, which is very simple, and it was accepted.

But at the beginning, I was trying to determine what the expected outcome would be before starting anything. Codecademy exercises seem to be pretty picky sometimes - if a function works but doesn't return the expected result, it won't be accepted.

Though it doesn't matter now, since what I did write was accepted.


#10

Care to share? I'd love a little more closure on this topic, and always open to learning new things.

There's this,

def count(seq1, seq2):
    n = 0
    for i in seq1:
        if i in seq2:
            n += 1
    return n

print (count([2,3,4,5,2,4,6,7,2], [2,3]))

#11

This is what I had, pretty much the same except for using == instead of in. I tried changing it to "if item in sequence", but I got this error and I'm not sure why: "Your function fails on count([4, 'foo', 5, 'foo'],5) It returns 4 when it should return 1". I would think they would work the same.

def count(sequence, item):

counter = 0

for x in sequence:
    if item == x:
        counter += 1
return counter

#12

def count(sequence, item):
    n = 0
    for i in sequence:
        if i == item:
            n += 1
    return n
    
print count([4, 'foo', 5, 'foo'],5)    # 1

Can you please post a link to this exercise? This code looks pretty much like yours and runs fine in the lab. I'd like to check it in the exercise. Thank you.


#13

This is more what I was referring to in my earlier post (#6)...

def _count_(a, b):
    n = list(set(a).intersection(b))
    m = {}
    for i in n:
        m[i] = 0
    for i in a:
        if i in n:
            m[i] += 1
    return m

a = [5,89,34,8,0,29,3,8,1,0,9,84,10,9,8,55,
     98,57,4,3,22,39,89,8,57,43,95,8,75]
b = [0,1,1,2,3,5,8,13,21,34,55]
print list(set(a).intersection(b))
print _count_(a,b)
[0, 1, 34, 3, 5, 8, 55]
{0: 2, 1: 1, 34: 1, 3: 2, 5: 1, 8: 5, 55: 1}

#14

I see. That would have been cool, but the instructions were to not use things like intersection in the code.

Here's the link: Unit 8 Practice Makes Perfect: 11/15 Count


#15

That's pretty clear. We've arrived at a fairly refined simple solution for the exercise. This is purely supplemental to add a takeaway to the topic.

I probably should have sorted the dictionary. Will give that a whirl. We've basically got a frequency table here.

print sorted(_count_(a,b).items(), key=lambda x: x[0])
[(0, 2), (1, 1), (3, 2), (5, 1), (8, 5), (34, 1), (55, 1)]

print sorted(_count_(a,b).items(), key=lambda x: x[1])
[(1, 1), (34, 1), (5, 1), (55, 1), (0, 2), (3, 2), (8, 5)]

#16

The reason it returned 4 was because you were looking in the same sequence you were iterating. Everything got counted. By equating only to one term at a time, you got the correct count.