11/15 count


#1

def count(sequence,item):
found=0
if type(item) in ["int","float","str"]:
item=int(item)
for num in sequence:
if num==item:
found+=1
else:
return found
elif type(item)=="list" :
for num in sequence:
if num in item:
found+=1
return found

what is wrong with those codes?


#2

@cssrockstar07957

Take a look at this post and you will know what calling return in a function does to that function

also take a look at this thread on a suggested implementation of the count method
Click here to post

Hope this helped?


#3

Yo,

def purify(numbers):
    for i in range(len(numbers) -1, -1, -1): # loops list from highest to lowest
        if numbers[i] % 2 != 0: # if position does not = 0 when % 2 then,
            del numbers[i] # delete it
        
    return numbers

#4

Or this:

def count(sequence, item):
    num = 0
    for i in sequence:
        if i == item:
            num += 1
    # return the nummber of times the item occurs in a list.
    return num

print count ([1, 2, 2, 2, 3, 4, 5, 10, 10, 10, 11, 10], 10)

#5
def count(sequence,item):
    return sequence.count(item)

#6

I'm a little stuck, my code passes, but it won't identify strings and add to "found", so it always returns 0 for strings. I'm doing something wrong but I'm not sure what. Could anybody help?

def count(sequence, item):
        len_seq = len(sequence) - 1 #number of items in list
        found = 0                   #variable stores amt of times item is found
        for i in range(len_seq+1):      #iterate through sequence
            if sequence[i] == item:     #if item (i) in sequence is found,
                found += 1              #increase found
                #print found += 1
        return found 
print count("hey hey hello", "hey")

It returns 0 instead of 2. Do I need to separate strings and integers and handle them separately, or can I do this with a modification to what I've got?

Thanks!


#7

Try this

def count(sequence, item):
        #len_seq = len(sequence) - 1 #number of items in list
        found = 0                 #variable stores amt of times item is found
        #here yo were not iterating through the sequence but numbers
        #thats why nothing matched the item and found was 0
        for i in sequence.split():      #iterate through sequence
            if i == item:     #if item (i) in sequence is found,
                found += 1              #increase found
                #print found += 1
        return found 
print count("hey hey hello", "hey")

#8

Thanks. With that code, now it returns the correct number for using a string as the first argument. However, trying to call up a list with that version brings up an error "AttributeError: 'list' object has no attribute 'split'". So, when I call:

count([1,2,3,1,2,3,1,4,5], 2)

the program fails. So I can't use the .split function to split the list the same way I would split a string. I was able to combine the two for something that works:

def count(sequence, item):
    len_seq = len(sequence) - 1 #number of items in list
    found = 0                   #variable stores amt of                                    #times item is found
    if type(sequence) == list:
        for i in range(len_seq+1):
            if sequence[i] == item:
                found += 1          
    elif type(sequence) == str:   
        for i in sequence.split():
            if i == item:                                
                found += 1              #increase found
                                        #print found += 1
    return found

print count([3.14, 2.5, 7.6], 3.14)

Thanks for the help!


#9

@pymaster68316 you can shorten this code too. One suggestion is

def count(sequence,item):
    return len([i for i in sequence if i == item]) or len(sequence.split(item))-1

take a look at this post


#10

def count(sequence,item):
items = 0
for i in range(len(sequence)):
if sequence[i] == item:
items += 1
return items

print count([1,2,1,1],1)

It works well.


#11

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


#12

why is it that i cant get the answer when i input - print num ????


#13

so what's the function of len_seq() ?
it haven't appeared...


#14

@longoodeye yea that variable was never used it was never necessary in the implementation. it would been useful if we used a while loop like this

def count(sequence, item):
    
        #break the sequence into a list using spaces
        sequence = sequence.split()
        
        len_seq = len(sequence) - 1
    
        found,i = 0 ,0
        while(i<= len_seq):
            if sequence[i] == item:
                found += 1
            i+=1        
     
        return found 
print count("hey hey hello", "hey")

#15

Can anyone tell me why this doesn't work? The message is:
Oops, try again. Your code looks a bit off--it threw a "'list' object has no attribute 'split'" error.

The counter verifies that it is counting occurrences properly.

def count(sequence,item):
sequence2 = sequence.split() #translates to a list
print sequence2 #visual verification of list
counter = 0 # sets counter to zero
for x in range(0,len(sequence2)): # for loop from 0 to #items in list
if sequence2[x] == item: #comparison of item in list to word
counter += 1 #incriments counter by one if true
print "counter:" , counter #visual verification of counter
return counter
count("how how how is how long how","how")


#16

easiest way to do this the long way

def count(lst,item):
    res = 0
    for x in lst:
        if x == item:
            res += 1
    return res

#17
def count(sequence, item):
    found = 0 
    for i in range(0, len(sequence)):
        if item == sequence[i]:
            found += 1
    return found

print count( ["a", "b", "c", "a", "a"], "a")

#18

@david_hill_phd take a look at the code excerpt below i have commented why you get that error

# you are getting that error because your code assumes that
# the sequence that will be given is a string
#but when codeacademy tests your code they use a list
# as the sequence.
#and you cannot call the split() method on list
#i.e sequence2 = sequence.split() 
#hence you get that error

# to fix that you need to put a little handler in place
def count(sequence,item):
    #first define sequence2 an empty
    sequence2 = ""
    
    #if the sequence given is not a list then 
    #then we can store the result of splitting
    #sequence in sequence2
    if type(sequence) != list:
        sequence2 = sequence.split() #translates to a list
    
    #else if the sequnce is a list the there is no need to 
    #split it we just store it in sequence2
    else:
        sequence2 = sequence
    
    print sequence2 #visual verification of list
    counter = 0 # sets counter to zero
    for x in range(0,len(sequence2)): # for loop from 0 to #items in list
        if sequence2[x] == item: #comparison of item in list to word
            counter += 1 #incriments counter by one if true
            print "counter:" , counter #visual verification of counter
    return counter 
count("how how how is how long how","how")

#19

Thanks admerolli, your code actually worked!

But it would be even more helpful if you could tell me how your code works. Again, thank you so much!


#20

I'm guessing because your string isn't a list of words but a whole string. So, list-wise, each item in the list is one of the letters. You could probably put in a string.split() if it is a sentence, but I'm not sure how you'd determine that. Maybe someone else can help?