Pregunta de entrevista

Entrevista de Data Scientist

-

Meta

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.

Respuesta

Respuestas de entrevistas

19 respuestas

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 en

9

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

Anónimo en

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 en

5

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

Anónimo en

2

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

Teni en

1

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

Anónimo en

1

Select * from A except Select * from B

Anónimo en

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[1] == B[0]: return A[0] elif A[0] == B[0]: return A[1] 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 en

1

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

JPC en

2

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

Morgan en

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")

Anónimo en

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 "" ;

Anónimo en

0

XOR all elements

Anónimo en

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]

Anónimo en

0

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

Anónimo en

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 en

0

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

Beena en

0

Python: sum(A)-sum(B)

Anónimo en

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

Anónimo en

Añadir respuestas o comentarios

Para publicar un comentario sobre esto, inicia sesión o regístrate.