-1

I have an exam in two days and we an example from teacher, but I'm new to Python, and I kind of blocked at this

This is the problem statement:

Write an algorithm which will get the parameters two array of integers x[1..n] if y[1..n] which returns number of common elements in the two arrays.

def apartine(a,e):
    e=1
    gasit=False
    n=len(a)
    i=0
    while i<=n and gasit==False:
        if a[i]==e:
            gasit=True
        else:
            i=i+1
    return gasit

def intersectie(a,b,c):
    e=0
    k=len(c)
    m=len(b)
    for i in range(1,m):
        if apartine(a,e)==True:
            k=k+1
            c.append(b[i])
            print(c)
    return c

a=[1,3,5,7]
b=[2,3,4,6]
c=[]
print intersectie(a,b,c)

I'm stuck with this.

I need some help to find out what I'm doing wrong.

Vlad
  • 18,195
  • 4
  • 41
  • 71
Darius
  • 9
  • 3

4 Answers4

3

A simple list comprehension can do that :

c=[i for i in a if i in b]
CoMartel
  • 3,521
  • 4
  • 25
  • 48
0

One issue you have in apartine() is that a[len(a)] (a[i] in the last run through the while loop) will raise an exception. Valid indices are from 0 to len(a)-1.

e=1 means apartine() will only look to see if 1 is in a, rather than whatever was passed.

You want to call apartine(a, b[i]), e doesn't seem to have any meaningful value.

You're not using k.

A very simple way to implement the whole thing in Python would be:

c = list(set(a) & set(b))

but I assume that's not what you're looking for.

Vlad
  • 18,195
  • 4
  • 41
  • 71
0

If the position in the lists doesn't matter you can get the common elements with a simple comprehension list

commons = [x for x in a if x in b]

If the position matters, you could use the index

commons = [a[i] for i in range(len(a)) if a[i] == b[i]]

The number of elements in common would be the length of this list

print(len(commmons))
Mr. E
  • 2,070
  • 11
  • 23
0

If you're having difficulty following the list comprehensions, here's basically the same thing written as a for loop:

def intersect(a, b):
    """Return a list of all elements common to a & b
    """
    intersection = []
    for element in a:
        if element in b and element not in intersection:
            intersection.append(element)
    return intersection


if __name__ == '__main__':
    test_pairs = (
        ([1, 3, 5, 7], [2, 3, 4, 6]),
        ([3], [1, 2, 3]),
        ([1, 2, 3], [3]),
        ([3, 3, 3], [3]),
        ([3], [3, 3, 3]),
        ([1, 2, 3], [4, 5, 6])
    )
    for a, b in test_pairs:
        print "Intersection of %s and %s is %s" % (a, b, intersect(a,b))

The output is:

Intersection of [1, 3, 5, 7] and [2, 3, 4, 6] is [3]
Intersection of [3] and [1, 2, 3] is [3]
Intersection of [1, 2, 3] and [3] is [3]
Intersection of [3, 3, 3] and [3] is [3]
Intersection of [3] and [3, 3, 3] is [3]
Intersection of [1, 2, 3] and [4, 5, 6] is []

The methodology is to take each element from one list (it doesn't matter which one) and see if it exists in the other list. Collect these elements in a third list which is your result. As you can see, this duplicates are removed by the element not in intersection clause.

Mark Tozzi
  • 10,353
  • 5
  • 22
  • 30