This is my first attempt at C, although I have Python experience.
I wrote a Python program but it is too slow and I think C might help me. How do I convert it into C?
The problem:
I have a bunch of triple numbers, like so
I want to find those pairs of triples that share 2-components. For example. Triple2 and triple9 intersect because they share n=2 components: (6,7,2) and (6,2,3) share 6 and 2Code:[[0, 1, 3], [4, 5, 7], [6, 7, 2], [5, 4, 1], [0, 2, 7], [4, 6, 3], [1, 4, 3], [5, 0, 7], [5, 1, 0], [6, 2, 3], [4, 7, 6], [0, 3, 2]]
Now, in Python, this is a solution:
In Python terms: It returns a tuple with indices of those pairs that have n-components in common. So we find that it contains the pair (2, 9), meaning lst[2] and lst[9] because they share n=2 components:Code:def condition_shared(prim1,prim2,n):
return len(set(prim1).intersection(set(prim2))) == n
def searchlist(myList):
r = range(len(myList))
result = []
for i in r:
for j in r:
if condition_shared(myList[i],myList[j],2):
result.append((i,j))
return tuple(result)
print searchlist(lst)
How would I go about writing this in C? (Could it even be multithreaded?)