This is a question Im trying to figure out for a job pre-interview questioniar. and i dont know python that well so im struggling with this problem.

the real questions for the job are big-o performance and making it better but i can do that i just need to understand this code

question A: in is_bar they compare the iterator to the array (if i == b). is this the equivalent of (if i == b.length)?

question B:
so following the flow of the function and assuming the above and (a = [1,2,3] b=[3,4,5]) is true the first comparison is comparing two iterators????

so it would be:

is_bar(b, 0)

for i in b (says a but its b from above)

if 0 == 0

so it evaluates to true and does c.append(0)

does this 3 more times and since its comparing iterators it would append 1,2 than unique (c)
6.which makes a new array the same size as c and makes every values = 1

but that wouldnt make sense. Appearently there is somthing im missing because a function that takes 2 arrays, then creates an array of equal size of the second array and makes all values = 1 would be stupid.

question C: what does b.keys() return exactly (i looked up the function but it says "list of all the keys used" but that doesnt make sense since it is a one-dimension array)

def foo(a, b):
""" a and b are both lists """
c = []
for i in a:
if is_bar(b, i):
c.append(i)
return unique(c)
def is_bar(a, b):
for i in a:
if i == b:
return True
return False
def unique(arr):
b = {}
for i in arr:
b[i] = 1
return b.keys()

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
If this question can be reworded to fit the rules in the help center, please edit the question.

4

I guess the principle here is that, if it is a question for an interview, they are interested in discovering your knowledge, analytical skills and programming ability - not the collective ability of Stackover flow users.
–
AndrewJul 23 '11 at 19:15

its not a python job, its an entry level java position. they know my skill are in java this is just a basic questioniar. i have done all the other three questions no problem (in java). i just dont understand the code im not asking for the answer. asking for the answer would be asking for the big-0 performance of the code and a better solution.
–
owen gerigJul 23 '11 at 19:21

The name b is used in three different namespaces here, so has three different meanings. keys is called on it when it refers to a dict.
–
katrielalexJul 23 '11 at 20:59

A: i wish i could see how/where its doing that (not that im arguing i just dont see it, but this is what it has to be) i figured it was a union. what would b.keys() return then, all values in the b array? which would be all the unique entries of a and b combined
–
owen gerigJul 23 '11 at 19:28

"i wish i could see how/where its doing that": for i in a (see here). "what would b.keys() return then, all values in the b array? which would be all the unique entries of a and b combined": yes.
–
truthealityJul 23 '11 at 19:37

thank you for that link. wow thats different then all the languages ive seen. this was why i didnt understand the code. i thought "i" was an iterator
–
owen gerigJul 25 '11 at 1:35

This construct is very popular with scripting languages and dynamically typed languages (sometimes called a foreach loop). Java has it too since 1.5
–
truthealityJul 25 '11 at 2:09

thank you but i didnt want this answer, although it is the answer. i really am just trying to understand the code. but thank you
–
owen gerigJul 23 '11 at 19:23

and although im waiting for a response i dont think there are duplicate entries in the array, which this would do.
–
owen gerigJul 23 '11 at 19:31

i was slightly more confident about my understanding than i should have been but that error has been corrected. the function indeed computes the intersection of two sets as shown by assert foo([1,2],[2,3]) == [2]
–
Dan D.Jul 23 '11 at 19:39