Nov-24-2020, 10:17 PM
Use combinations to reduce the number of pairs you have to test. You'll have to change the test to check for +/-h. Using list.count() and a list comprehension should speed things up too.
def countit2(values, h=1): return [abs(a-b) for a, b in itertools.combinations(set(values), r=2)].count(h)