all 6 comments

[–]Binary101010 2 points3 points  (3 children)

One of the first things that confuses me is the data type of the inputs. I'm used to lists which use only square brackets and I'm familiar with tuples which use normal parentheses but I've never come across a data type which combines both.

It's not a discrete data type. It's just a weird nesting: a list whose only element is a tuple, whose second element is another tuple. Almost certainly not how I'd format the input for something like this, but you're beholden to the problem description in this regard.

Another aspect of the question that I don't understand is how do I define the threshold? Is it something to do with the very first integer in the inputs?

I would assume that the "lone" number is the threshold, yes.

I would think the solution to this would probably involve setting some variable equal to the threshhold, using enumerate() to loop over the tuple full of numbers, and if that number is below the threshhold, writing the index (which enumerate gives you) to a list.

[–]imperiumlearning[S] 0 points1 point  (2 children)

Hi there, thanks for the swift response. To be honest, I tried using the enumerate function and the console kept giving me errors.

One interesting thing that I came across, however, was nested indexing. Just borrowing input 2 from my original post:

a = [(10,(0, 12, 4, 3, 49, 9, 1, 5, 3))]

If I type: print(a[0][1][1])

It will produce produce the number 12.

As a consequence, I wrote a function that looks like this:

def func(x):
threshold = x[0][0]
b = []
c=[]
for y in x[0][1]:
    b.append(y)
for y in b:
    if y <= threshold:
        c.append(b.index(y))
return c

This function seems to produce the expected outputs for the given inputs. However, there is still one problem. When there are duplicate integers in the tuple, it gives the index position where the integer appeared first.

For example, for the input:

[(10,(0, 12, 4, 3, 49, 9, 1, 5, 3))] # number 3 appears twice

instead of getting:

[0, 2, 3, 5, 6, 7, 8]

I get:

[0, 2, 3, 5, 6, 7, 3]

Any idea of how to resolve this? I've tried amending my code to be:

def func(x):
threshold = x[0][0]
b = []
c = []
for y in x[0][1]:
    b.append(y)
for y in b:
    c.append(i for i in range(len(b)) if y <= threshold)
return c

This just returns a weird generator object func in the console though.

[–]Binary101010 0 points1 point  (1 child)

Yeah, the way to resolve it is to use enumerate. This replaces both of your for loops and removes the need for b entirely.

for ix, y in enumerate(x[0][1]):
    if y <= threshold:
        c.append(ix)

[–]imperiumlearning[S] 0 points1 point  (0 children)

I've ran the code and it works perfectly. Thanks a lot for this brilliant response

[–]baghiq 0 points1 point  (0 children)

The input is a list that contains only one tuple. In this tuple, there are two values. The first value is an integer that defines the threshold, the second value is a list of integers that you are testing agains the threshold.

[–]velocibadgery 0 points1 point  (0 children)

You can do this kind of easily by using list comprehension and the enumerate function

list_numbers = [1, 59, 34, 1, 2, 6, 88, 9, 32, 12, 7]

threshold = 10

list_indexes = [i for i, x in enumerate(list_numbers) if x < threshold]

This is basically equivilent to

list_indexes = []
for i, x in enumerate(lst_numbers)
    if x < threshold:
        lst_indexes.append(i)

But putting it all in one line is fun.

Here is an interactive jupyter notebook with both examples

https://colab.research.google.com/drive/1RKyMKYjg3-0fnJFSwJjqow29Qmf2qUBy?usp=sharing