# 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.
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
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