Hamad Hassan - 4 months ago 4

Linux Question

Here in the input and statement of its explanatioon

`a= [( 0.1, 0.2, 0.3), (0.2, 0.8, 0.9, 4.5), ( 0.11, 0.22, 0.33), (0.2, 0.46, 0.475, 1.8, 1.95)]`

Each element of the tuple represents a length of chord. Like the first tuple consists of 3 chords representing a triangle with length of sides 0.1, 0.2 and 0.3. The second one is a rectangle and so on. In each tuple there can be a variable number of elements. Like in the first tuple there are 3 element, in the second there are 4 element, in the third there are again 3 elements while in the fourth there are 5 elements. So it can be variable too.

Now comes the task.

In SOME tuples the length of its one of the elements CAN BE half or double of the length of element of another tuple. In that case the ID of the (couple) tuples needs to be output and NOT the length it self. The ID of tuple would be helpful to identify if it was the first tuple, the second or another. The ID of the element is not required just the ID of the tuples.

Like for the given case the result would be

`1 2 because 0.1 exists in tuple 1 and 0.2 exists in tuple 2`

1 4 because 0.1 exists in tuple 1 and 0.2 exists in tuple 4

2 4 because 0.9 exists in tuple 2 and 1.8 exists in tuple 4

Please note that

`0.1 and 0.2 exist in tuple 1`

but it would not be output as 1 1 because it is in the same tuple. This should not excluded if the half or double length exists in the same tuple.

is it possible to apply a barrier of +-5% of the length, just to consider the numerical and round-off error?

I just learnt concatenated use of tuple so a try was

`couples = []`

for lengths in a:

for length in lengths:

if [length==i*2 for i in tup) for tup in b)]:

couples.append(length,i)

elif [length==i*0.5 for i in tup) for tup in b)]:

couples.append(length,tup)

but I am unaware of its proper use for comparison.

regards

Answer

The solution works like following. Although it is not an elegant way but from the scratch and simple commands it would look like below

```
a= [( 0.1, 0.2, 0.3), (0.2, 0.8, 0.9, 4.5), ( 0.11, 0.22, 0.33), (0.2, 0.46, 0.475, 1.8, 1.95)]
couples = [] # define an empty list
totalTupleIDs= len(a) # get the length of list of tuples to loop over tuples
for i in range(totalTupleIDs): # Start the loop over tuples
totalElementsInTuple=len(a[i]) # get the length of tuple to loop over its elements
for j in range(totalElementsInTuple): # start loop over elements of tuples
currentElement1= a[i][j] # get the current element inside tuple
print currentElement1
for k in range(totalTupleIDs): # Start the second loop for comparison purpose over the same list of tuples!
print a[k]
totalElementsInTuple=len(a[k])
print totalElementsInTuple
for l in range(totalElementsInTuple): #start the second loop for comparison over the elements of tuples
currentElement2= a[k][l]
print currentElement2
if currentElement1==currentElement2: # compare the elements
if i==k: # if the IDs of tuples are same, its a redundant result, leave!
print "Redundant"
else:
print " Similar at %d %d " % (i , k) # if there is no self comparison , append the result
couples.append((i, k))
print couples # output and compare the result
```

Source (Stackoverflow)

Comments