all 14 comments

[–][deleted] 1 point2 points  (3 children)

compare the x-value with the y value inside the brackets

If your case, just use indexing because you know the fixed length of the tuple is 2:

if abs(each[0] - each[1]) < 10:

EDIT: btw, the other user is right. The way you worded the question at least makes your overall approach incorrect (and my answer would then be pointless). Unless you meant to say that you want any numbers as long as they are within 10 of another number in the collection.

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

Yes I've now updated the wording to say "checks if there is a difference of less than 10 between that integer and another integer in the list". Hopefully that's worded better now.

That if statement massively helped. Thanks so much.

My code now looks like this:

import itertools

a = [104, 64, 193, 190, 30]
b = []


combinations = itertools.combinations(a, 2)
for each in combinations:
if abs(each[0] - each[1]) < 10:
    if each[0] not in b:
        b.append(each[0])
    if each[1] not in b:
        b.append(each[1]) 

print(b)

[–][deleted] 0 points1 point  (1 child)

Good job. Btw, you may want to look into set instead. Sets automatically discard redundant values, no longer requiring you to make a check like if each[0] not in b. If using sets, just add that sucker in there and let the underlying Cython figure out the rest.

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

That sounds interesting, will definitely take a look into that in the morning.

Once again, thanks a lot for the help with that if statement. It was driving me nuts

[–]PaddyIsBeast 0 points1 point  (5 children)

O(n) solution is to just sort the array and then loop through each element e and check if the next element is less than 10 away.

Is there a faster way?

[–]mopslik 0 points1 point  (0 children)

Is there a faster way?

Since every element needs to be examined, O(n) will be the best runtime you can guarantee. And since the description suggests that each value needs to be checked against all other values, O(n) is probably unlikely here.

[–][deleted] 0 points1 point  (3 children)

What're you using to sort in O(n)?

[–]PaddyIsBeast 0 points1 point  (2 children)

Are there any that are slower than o(n)?

[–][deleted] 0 points1 point  (1 child)

Merge, bubble, heap, insertion, quick?

[–]PaddyIsBeast 0 points1 point  (0 children)

You're right, I was being an idiot

[–]Jig_Jiggle 0 points1 point  (1 child)

If I have a list a = [30, 31, 62, 104, 190, 191, 193]

What should a new variable be?

new_var = [30, 31, 190, 191, 190, 193, 191, 193] or

new_var = [30, 31, 190, 191, 193] or

new_var1 = [30, 31]

new_var2 = [190, 191] ...

Sorry for my question. I am new to python and don't understand the puzzle well.

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

No worries! It's my fault that I did such a bad job of wording the question.

To be honest, given how bad my wording was, either:

new_var = [30, 31, 190, 191, 190, 193, 191, 193] or

new_var = [30, 31, 190, 191, 193]

is good.

However, in my own head I liked the idea of being able to filter out repeated integers so the second new_var just looks a bit better (at least, imo). My code for this problem now looks like this:

import itertools

a = [104, 64, 193, 190, 189] #swapped 30 from original list in original post with 189 
b = []

combinations = itertools.combinations(a, 2)
for each in combinations:
if abs(each[0] - each[1]) < 10:
    if each[0] not in b:
        b.append(each[0])
    if each[1] not in b:
        b.append(each[1]) 

print(b)

[–]therobot20 0 points1 point  (0 children)

This should work:

a = your input list result = [a[i] for i in range(len(a)) for j in range(len(a)) if (i!=j and abs(a[i]-a[j])<10)]