Help on count


#1



What is wrong?


def count(sequence,item):
    sequence = []
    result = 0
    for sth in sequence:
        if sth == item:
            result += 1 
            sequence.remove(item)
        else:
            return 0
    return result


#2

immediately in the function you are erasing "sequence" o.O

you don't need the sequence = [] line

edit: also your else return 0 doesn't seem to be needed but maybe i misunderstand your code


#3

Thanks could you help me on the next lesson:
Purify

def purify(a):
    for x in a:
        if x % 2 != 0:
            a.remove(x)
    return a

Whats wrong


#4

You have to add a par = [] ( most likely, but I am not entirely sure ).

Also in your if statement you have to check if the 2 equals the 0.

Afer this you need an .append statement instead of a remove one.

Last but not least you have to return par and not a.


#5

i have no experience programming but his worked for me:
1. set a variable to zero - this variable will represent "how many times the element is counted"
2. the for loop starts the iteration (for i in sequence:), if an individual iteration value matches the item value (if i == item:), then add one to your variable
3. then return the new value of the variable you assigned

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


#6

Honestly I feel like this should work.... o.O

In my own solution as nilswanicki said, I made a separate "pure" list and just added elements if they weren't already in it (used an "if not in") but tracing yours by hand I can't actually figure out why it won't work :confused: I'm doing it in my own interpreter to see what happens

It appears to only be removing every OTHER odd number. So if 1 odd, it removes it. If 2, it only removes 1, and leaves 1. If 3, it leaves 1. If 4 or 5 it leaves 2 behind. If 6 or 7 it leaves 3 behind. :thinking:


#7

I think I got it by researching a bit (here) what the for-each does.

It keeps an index to itself to step through the list one by one. When we remove something, everything left afterward shifts one index down to fill the empty spot created. But the for-each doesn't realize that, so its internal index is pointing one AFTER the originally intended spot (thus leaving the next odd behind, if two odds are consecutive). A bunch of odd numbers consecutively thus means that every other one gets skipped!

edit: I guess it doesn't crash when the list ends earlier than expected because for-each uses an iterator which just checks to see if something exists next to be processed, if not then leave the loop. so changing the size of the list does not confuse the loop

Using your purify():

I tested this with purify([2,3,3,3,3,3,2]); It left half the 3s and got rid of half.

Then I tested with purify([1, 2, 3, 4, 5, 6, 7, 8, 9]) and it worked just fine

Tested with purify([1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9]) and it left one of each of the odds.

Tested with purify([1, 1, 1, 2]), it left [1, 2]

Tested with purify([1, 1, 1, 1, 2]), it left [1, 1, 2]

ANYWAY I guess a for-each isn't good if we're adding or removing from the thing that we're looping through. Gotta do our adding/removing on a separate list, so you can make a copy of the input list and remove from that copy, or make an empty list and add to that.

If adding to it, check for evens. If removing from a copy, check for odds (as you currently do)


#8

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.