Create a function ```sel_number()```, that will select numbers that fulfill the following constraints:

1) The numbers should have 2 digits at least.

2) They should have their respective digits in increasing order from left to right. 
Examples: 789, 479, 12678, have these feature. But 617, 89927 are not of this type.
In general, if ```d1, d2, d3....``` are the digits of a certain number ```i``` 
Example:
```( i = d1d2d3d4d5) so, d1 < d2 < d3 < d4 < d5```

3) They cannot have digits that occurs twice or more. Example: 8991 should be discarded.

4) The difference between neighbouring pairs of digits cannot exceed certain value. 
Example: If the difference between contiguous digits cannot excced 2, so 1345, 23568 and 234578 pass this test. Other numbers like 1456, 389, 157 don't belong to that group because in the first number(1456), the difference between second and first digit 4 - 1 > 2; in the next one(389), we have 8 - 3 > 2; and see by yourself why 157 should be discarded.
In general, taking the example above of ```i = d1d2d3d4d5```:
```
d2 - d1 <= d;

d3 - d2 <= d;

d4 - d3 <= d;

d5 - d4 <= d;
```
The function should accept two arguments n and d; n is the upper limit of the range to work with(all the numbers should be less or equal than n), and d is maximum difference  between every pair of its contiguous digits. It's clear that 1 <= d <= 8.

Here we have some cases:
```
sel_number(0,1) = 0 # n = 0, empty range
sel_number(3, 1) = 0 # n = 3, numbers should be higher or equal than 12
sel_number(13, 1) = 1 # only 12 fulfill the requirements
sel_number(20, 2) = 2 # 12 and 13 are the numbers
sel_number(30, 2) = 4 # 12, 13, 23 and 24 are the selected ones
sel_number(44, 2) = 6 # 12, 13, 23, 24, 34 and 35 are valid ones
sel_number(50, 3) = 12 # 12, 13, 14, 23, 24, 25, 34, 35, 36, 45, 46 and 47 are valid
```
Compare the last example with this one:
```
sel_number(47, 3) = 12 # 12, 13, 14, 23, 24, 25, 34, 35, 36, 45, 46 and 47 are valid 
```
(because the instructions says the value of may be included if it fulfills the above constraints of course)

Happy coding!!

Source: codewars | Difficulty: EASY
Tags: Fundamentals

Data structures

Mathematics

Starter code: def sel_number(n, d):
	

Solutions:
def sel_number(n, d):
	cnt = 0
	for a in range(12, n + 1):
		nums = list(map(int, str(a)))
		if nums == sorted(set(nums)) and all((c - b <= d for (b, c) in zip(nums[:-1], nums[1:]))):
			cnt += 1
	return cnt


from itertools import tee, islice

def sel_number(n, d):

	def okay(x):
		(it1, it2) = tee(map(int, str(x)))
		return all((0 < y - x <= d for (x, y) in zip(it1, islice(it2, 1, None))))
	return sum(map(okay, range(10, n + 1)))


sel_number = lambda n, d: len([x for x in range(10, n + 1) if sorted(list(set(list(str(x))))) == list(str(x)) and all((int(str(x)[i]) - int(str(x)[i - 1]) <= d for i in range(1, len(str(x)))))])


from itertools import izip as zip, islice

def sel_number(n, d):

	def ok(x):
		s = map(int, str(x))
		return all((0 < b - a <= d for (a, b) in zip(s, islice(s, 1, None))))
	return sum((ok(i) for i in range(12, n + 1)))


def good_digits(m, d):
	s = str(m)
	res = []
	if len(s) < 2:
		return False
	i = 0
	while i < len(s) - 1:
		if s[i] >= s[i + 1]:
			return False
		if int(s[i + 1]) - int(s[i]) > d:
			return False
		i += 1
	for c in s:
		if c in res:
			return False
		res.append(c)
	return True

def sel_number(n, d):
	cnt = 0
	i = 0
	while i <= n:
		if good_digits(i, d):
			cnt += 1
		i += 1
	return cnt


def sel_number(n, d):
	iterator_count = lambda it: sum((1 for _ in it))
	check_constraints_1_and_3 = lambda a: len(set(a)) == len(a) > 1
	check_constraints_2_and_4 = lambda a: all((int(y) - int(x) <= d and y > x for (x, y) in zip(a[:-1], a[1:])))
	numbers_iterator = map(str, xrange(n + 1))
	numbers_iterator = filter(check_constraints_1_and_3, numbers_iterator)
	numbers_iterator = filter(check_constraints_2_and_4, numbers_iterator)
	return iterator_count(numbers_iterator)


sel_number = lambda n, d: sum((1 for a in map(str, xrange(n + 1)) if len(set(a)) == len(a) > 1 and all((int(b) - int(a) <= d and b > a for (a, b) in zip(a[:-1], a[1:])))))


def sel_number(n, d):
	count = 0

	def any_invalid(digits):
		for (a, b) in zip(digits, digits[1:]):
			if a >= b or b - a > d:
				return True
		return False
	for i in xrange(10, n + 1):
		if any_invalid([int(c) for c in str(i)]):
			continue
		count += 1
	return count


def check(n, d):
	s = str(n)
	if ''.join(sorted(s)) != s:
		return False
	z = s[0]
	for i in xrange(1, len(s)):
		x = s[i]
		if x == z or int(x) - int(z) > d:
			return False
		z = x
	return True

def sel_number(n, d):
	total = 0
	for i in xrange(12, n + 1):
		if check(i, d):
			total += 1
	return total