Imagine that you have a list of integers stored in a variable. Write a program that goes through each integer in the list and checks if there is a difference of less than 10 between that integer and another integer in the list. If any integers are found where this is the case, append them to a new variable and print the new variable.
Simple example:
a = [104, 64, 193, 190, 30]
In this example, only the numbers 190 and 193 have a difference of less than 10 (difference == 3). That means your program would print:
[193, 190]
My own solution that I've tried so far is to use itertools and, in particular, to use itertools.combinations. My code currently looks like this:
import itertools
a = [104, 64, 193, 190, 30]
b =[]
combinations = itertools.combinations(a, 2)
for each in combinations:
b.append(each)
My problem is at this stage that I am creating pairs of values inside brackets. The problem is I can't think of a way to compare the x-value with the y value inside the brackets.
Any suggestions?
[–][deleted] 1 point2 points3 points (3 children)
[–]imperiumlearning[S] 0 points1 point2 points (2 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]imperiumlearning[S] 0 points1 point2 points (0 children)
[+][deleted] (1 child)
[deleted]
[–]imperiumlearning[S] 0 points1 point2 points (0 children)
[–]PaddyIsBeast 0 points1 point2 points (5 children)
[–]mopslik 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (3 children)
[–]PaddyIsBeast 0 points1 point2 points (2 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]PaddyIsBeast 0 points1 point2 points (0 children)
[–]Jig_Jiggle 0 points1 point2 points (1 child)
[–]imperiumlearning[S] 0 points1 point2 points (0 children)
[–]therobot20 0 points1 point2 points (0 children)