## Sollicitatievraag

Sollicitatiegesprek voor de functie Data Scientist

-

# Given an list A of objects and another list B which is identical to A except that one element is removed, find that removed element.

Antwoord

## Antwoorden op sollicitatievragen

19 antwoorden

17

All these supposed answers are missing the point, and this question isn't even worded correctly. It should be lists of NUMBERS, not "objects". Anyway, the question is asking how you figure out the number that is missing from list B, which is identical to list A except one number is missing. Before getting into the coding, think about it logically - how would you find this? The answer of course is to sum all the numbers in A, sum all the numbers in B, subtract the sum of B from the sum of A, and that gives you the number.

Anonymous op

9

select b.element from b left join a on b.element = a.element where a.element is null

Anoniem op

6

In Python: (just numbers) def rem_elem_num(listA,listB): sumA = 0 sumB = 0 for i in listA: sumA += i for j in listB: sumB += j return sumA-sumB (general) def rem_elem(listA, listB): dictB = {} for j in listB: dictB[j] = None for i in listA: if i not in dictB: return i

BurmesePython op

5

How about this in python, will this work? x = set(listA)-set(listB) print(x)

Anoniem op

2

missing_letters = [] for letter in A: if letter in B: pass else: missing_letters.append(letter) print (missing_letters)

Teni op

1

In R: removed_element <- A[which(!A %in% B)] removed_element

Anoniem op

1

Select * from A except Select * from B

Anoniem op

1

I think it is a coding in algorithm rather than SQL query. So here is my take: def ret_miss(A, B): k = len(A) if k == 2: if A == B: return A elif A == B: return A n = k/2 print A[n], B[n] if A[n] == B[n]: A= A[n:] B=B[n:] else: A=A[:n+1] B=B[:n+1] print A,B return ret_miss(A,B) This works nicely actually.

Chaiwala op

1

# python code def missing_obj(original_lst, new_lst): for x in new_lst: original_lst.remove(x) return original_lst

JPC op

2

In SQL: SELECT A.object FROM A LEFT JOIN B ON A.object = B.object WHERE B.object IS NULL;

Morgan op

1

Careful, there could be a repeated object that's being removed. i.e. A = [3, 4, 5, 6, 5] B = [3, 4, 6, 5] This is how I would do it on Python (works for numbers and strings) def missingval(lA, lB): a = sorted(lA) b = sorted(lB) c = None for i in range(len(b)): if a[i] != b[i]: c = a[i] break if c is None: c = a[-1] print(c, "was removed from list A")

Anoniem op

0

SQL: select a.list as a, b.list as b from ListA as a full join ListB as b on a.list = b.list where a.list eq '' OR b.list eq "" ;

Anoniem op

0

XOR all elements

Anoniem op

0

A = [1,2,3,4,5,6,7,8] B = [1,2,3,4,5,6,8] [i for i in A if i not in B]

Anoniem op

0

find the sum of the two list and subtract. ans = sum(a) - sum(b) where a and b are list of numbers.

Anoniem op

0

Depends on the kind of elements in the lists. If they're numbers, sum(A) minus sum(B) will give the missing element. If they're characters/strings, just dump the elements of A into a dictionary and check each element in B for existence in A.

dopey op

0

[i for i in A if i not in [j for j in B]]

Beena op

0

Python: sum(A)-sum(B)

Anoniem op

0

two ways to do it using sql: 1. select * from A where not in (select * from B) -- assuming you know what element you're looking for 2. select * from (select * from A UNION select * from B) having count(element) = 1 -- again assuming you know the element

Anoniem op

## Voeg antwoorden of opmerkingen toe

Meld u aan of registreer u om hier een opmerking over te maken.