I need to create a function that can takes a list of tuples and a number. Example:
if the list of tuples is [(2,5),(8,9),(11,19),(22,43),(47,50)], and the number is 14, the it should return 18.
The reason for this is at number 13 in the list 2,3,4,5,8,9,11,12,13,14,15,16,17,18,19... is 18 if all numbers are included:
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18.
I now have:
def converting(tuples,index):
values = [] #I will get [(2,3,4,5,6),(8,9),(11,12,13,14,15,16,17,18,19),(22,..,43),(47,48,49,50)]
for tuple in tuples:
tupleValues = range(tuple[0], tuple[1]+1)
values.extend(tupleValues) #ex(2,3,4,5,6)
if index <= len(values): #If 14 in the example is lower than len of the list, eg 42
return values[index-1] #return value of 14-1, which is 16?
print converting(14,[(2,5),(8,9),(11,19),(22,43),(47,50)])
When I print this I get the message:
for tuple in tuples:
TypeError: ‘int’ object is not iterable
If I understand the problem correctly, you have a sequence of intervals and you need to extract the n-th number from those intervals. Here is a different solution regarding the algorithm used. Just count all the missing numbers from your interval sequence and add it to your value: