-5

You are presented with two arrays, all containing positive integers. One of the arrays will have one extra number, see below:

[1,2,3] and [1,2,3,4] should return 4

[4,66,7] and [66,77,7,4] should return 77

My code:

def find_missing(arr1, arr2):  
  if len(arr1) != len(arr2):  
    for i in arr1 and arr2:  
        if arr1[i] != arr2[i]:  
          return i

Is producing this error:

Traceback (most recent call last): File "python", line 1, 
in <module> File "python", line 4, in find_missing 
IndexError: list index out of range
jwpfox
  • 5,124
  • 11
  • 45
  • 42
  • I don't think `for i in arr1 and arr2` does what you expect it to do. `and` short-circuits by returning one of its arguments (the second one in this case), so you're iterating over the values of the second list. Not over indexes, which is how you're using `i`. – Blckknght Nov 02 '16 at 07:43
  • Both lists can have the same size and contain completely different values, so `if len(arr1) != len(arr2)` does not the job either... – wiltomap Nov 02 '16 at 07:45
  • 4
    OP should read up on basic Python as the usage of `for` is completely wrong (and the logic of the code is as well). Slightly related: If all the elements of the list are unique this can be solved in a single line (using `set`s) – UnholySheep Nov 02 '16 at 07:49

3 Answers3

0

Your code seems not to perform what you mean - it is another problem.

Error is here:

for i in arr1 and arr2:
    if arr1[i] != arr2[i]:  

i is not an index in arr1 but directly its element. (Length of array may be 4 but element in it may be 66.)

Solution may be as simple as this:

def find_missing(arr1, arr2):
    diff = [i for i in arr1 if i not in arr2] + [i for i in arr2 if i not in arr1]
    return diff[0] if len(diff) > 0  else None
MarianD
  • 13,096
  • 12
  • 42
  • 54
  • I changed in my answer the return value from the simple `diff` (which is the **list**) to the expression which return the `first element of it` (or special value `None` in the case of no difference) - to be in accordance with your request. – MarianD Nov 02 '16 at 08:19
0

This could be one way of doing it:

def find_missing(arr1, arr2):

    # Set longer array to lst1, shorter to lst2 
    if len(arr1) > len(arr2):
        lst1 = arr1
        lst2 = arr2
    else:
        lst1 = arr2
        lst2 = arr1

    # Go through elements in longer list
    for element in lst1:

        # If this element is not in lst2, we found it, return result
        if element not in lst2:
            return element
Fejs
  • 2,734
  • 3
  • 21
  • 40
0
def find_missing(list1, list2):
    try:        
        if len(list1) > len(list2):
            return [element for element in list1 if element not in list2][0]
        else:
            return [element for element in list2 if element not in list1][0]
    except IndexError:
        return None
Stam Kaly
  • 668
  • 1
  • 11
  • 26
  • i tested your reply on repl its returning a name error any ideas? – Martin Mungai Nov 02 '16 at 08:09
  • It will produce an error in the case of **no difference** - it is not possible to get element [0] from an **empty list**. – MarianD Nov 02 '16 at 08:25
  • Name error is indeed from *your code* - **out of the definition of the function** - it works fine if there is a difference (extra number in one of the lists). – MarianD Nov 02 '16 at 08:30
  • okay, i understand, the code has other logic to handle the cases of same lists and empty lists, its just that i didn't post that coz it was working properly – Martin Mungai Nov 02 '16 at 08:42