# Task
 Let's say that number a feels comfortable with number b if a ≠ b and b lies in the segment` [a - s(a), a + s(a)]`, where `s(x)` is the sum of x's digits.

 How many pairs (a, b) are there, such that a < b, both a and b lie on the segment `[L, R]`, and each number feels comfortable with the other?

# Example

 For `L = 10 and R = 12`, the output should be `2`

 Here are all values of s(x) to consider:
```
s(10) = 1, so 10 is comfortable with 9 and 11;
s(11) = 2, so 11 is comfortable with 9, 10, 12 and 13;
s(12) = 3, so 12 is comfortable with 9, 10, 11, 13, 14 and 15.
Thus, there are 2 pairs of numbers comfortable 
with each other within the segment [10; 12]: 
(10, 11) and (11, 12).
```
# Input/Output

 - `[input]` integer `L`

    Constraints: `1 ≤ L ≤ R ≤ 1000`

 - `[input]` integer `R`

    Constraints: `1 ≤ L ≤ R ≤ 1000`

 - `[output]` an integer

    The number of pairs satisfying all the above conditions.

Source: codewars | Difficulty: EASY
Tags: Ad-hoc

Starter code: def comfortable_numbers(l, r):
	

Solutions:
def comfortable_numbers(l, r):
	s = [sum(map(int, str(n))) for n in range(l, r + 1)]
	return sum((s[i] >= i - j <= s[j] for i in range(1, len(s)) for j in range(i)))


from collections import defaultdict

def comfortable_numbers(l, r):
	(D, res) = (defaultdict(set), 0)
	for i in range(l, r + 1):
		s = sum(map(int, str(i)))
		res += sum((i in D[j] for j in range(max(l, i - s), i)))
		D[i] = set(range(i + 1, min(r, i + s) + 1))
	return res


def comfortable_numbers(l, r):

	def digit(n):
		sum = 0
		while n > 9:
			sum += n % 10
			n //= 10
		return sum + n
	a = [(x, y) for x in range(l, r) for y in range(x + 1, r + 1) if x <= y + digit(y) and x >= y - digit(y) and (y <= x + digit(x)) and (y >= x - digit(x))]
	return len(a)


def comfortable_numbers(l, r):
	ct = 0
	for i in range(l, r):
		for j in range(i + 1, r + 1):
			it = sum((int(c) for c in str(i)))
			jt = sum((int(c) for c in str(j)))
			if i - jt <= j <= i + jt and j - it <= i <= j + it:
				ct += 1
	return ct


def comfortable_numbers(l, r):
	used = {l}
	s = {l: sum((int(i) for i in str(l)))}
	count = 0
	for a in range(l, r):
		sa = s[a]
		for b in range(a + 1, r + 1):
			if b in used:
				sb = s[b]
			else:
				sb = sum((int(i) for i in str(b)))
				s.update({b: sb})
				used.add(b)
			if a - sa <= b <= a + sa and b - sb <= a <= b + sb:
				count += 1
	return count


def comfortable_numbers(l, r):
	sm = 0
	dig = [sum((int(x) for x in str(i))) for i in range(l, r + 1)]
	for i in range(l, r):
		for j in range(i + 1, i + dig[i - l] + 1):
			if j > r:
				break
			if j - i <= dig[j - l]:
				sm += 1
	return sm


def comfortable_numbers(l, r):
	pairs = set()
	for a in range(l, r + 1):
		s = sum((int(d) for d in str(a)))
		pairs |= set(((a, x) for x in range(max(l, a - s), min(r, a + s) + 1) if x != a))
	return sum(((y, x) in pairs for (x, y) in pairs)) // 2


def comfortable_numbers(l, r):
	c = 0
	d = {}
	for a in range(l, r + 1):
		s = sum((int(d) for d in str(a)))
		d[a] = set(range(a - s, a + s + 1))
		for i in range(l, a):
			if a in d[i] and i in d[a]:
				c += 1
	return c


d = [sum((int(n) for n in str(i))) for i in xrange(1001)]
comfortable_numbers = lambda a, b: sum((i - d[i] <= j <= i + d[i] and j - d[j] <= i <= j + d[j] for i in xrange(a, b + 1) for j in xrange(i + 1, b + 1)))


def comfortable_numbers(l, r):
	sd = lambda x: sum((int(d) for d in str(x)))
	return sum((sd(b + 1) >= b - a + 1 for a in range(l, r) for b in range(a, min(a + sd(a), r))))