Can find any error?

Here is another problem that I encountered on codewars:

And here is my solution:

And here is the error

Although my code is working with negative numbers but still I am getting an error.
Thanks in advance

@cpradio

Did you change anything, as when I run it, it says it passes all tests.

Well, it passes all the example tests but when I click the attempt button it gives me an error. it would be great if you could provide a screenshot because I am still getting the error.

This is what I see

1 Like

Well, I found something strange. I removed line 8 and line 9 of my code and then ran it. And surprisingly it worked. Can you please explain the logic behind it.?

I had taken 3 different difference between two consecutive numbers because I thought what if I encounter an array like this:

1,3,4,5,6

Over here if we subtract the first two digits then we will get a difference of 2 however the real difference is of 1. So I had taken 2 more difference and then selected the minimum of all the 3 difference and taken it as the final difference.

I don’t understand why I was getting and error previously.

Yeah, that is a fluke that removing difference 2 and 3 cause it to work. It shouldn’t, for the reasons you outlined.

I did just find an error in your logic though.

With input of [-1, -4, -10, -13, -16, -19, -22, -25, -28], it fails. The diffs it generates are [ -3, -6, -3 ], and min() is choosing -6 when -3 should be utilized.

I think instead of min(), you want to use the number that is found twice in the array of differences.

So I came up with the following:

import collections
def find_missing(sequence):
    a = []
    c = 0
    difference = sequence[1] - sequence[0]
    difference_2 = sequence[2] - sequence[1]
    difference_3 = sequence[3] - sequence[2]
    a.append(difference)
    a.append(difference_2)
    a.append(difference_3)
    
    (b, count) = collections.Counter(a).most_common(1)[0]
    
    while c<= len(sequence) - 2:
        if sequence[c+1] - sequence[c] != b:
            return sequence[c+1] - b
        c = c + 1

It uses the collections class in Python so it can figure out which difference is the most common, and then use that for your loop below. You will see I used (b, count) = collections.Counter(a).most_common(1)[0], the {b, count} is because collections.Counter(a).most_common(1)[0] returns a list, designating the number that reoccurs the most and how many times it reoccurs (which should always be 2, since there are only 3 items in the array).

It solves the issue you thought up with 1,3,4,5,6 and it solves negative incrementing numbers too.

Didn’t know about collections. Thanks a lot. Btw can’t we count method here?

I’m assuming you are referring to https://www.tutorialspoint.com/python/list_count.htm

And yes, you could, but you’d have to loop over the collection, read the count, and then determine which has the higher count.

Granted, you can do it this way using count since we know the array only has 3 items.

def find_missing(sequence):
    a = []
    c = 0
    difference = sequence[1] - sequence[0]
    difference_2 = sequence[2] - sequence[1]
    difference_3 = sequence[3] - sequence[2]
    a.append(difference)
    a.append(difference_2)
    a.append(difference_3)
    
    first_item = a.count(a[0])
    second_item = a.count(a[1])
    third_item = a.count(a[2])
    if first_item == second_item:
        b = a[0]
    elif second_item == third_item:
        b = a[1]
    else:
        b = a[0]
    
    while c<= len(sequence) - 2:
        if sequence[c+1] - sequence[c] != b:
            return sequence[c+1] - b
        c = c + 1
1 Like

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