Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def product_of_number_of_differences(s):
- l = sorted(map(int, s.split()))
- counter = collections.Counter(np.diff([0] + l + [l[-1] + 3]))
- return counter[1] * counter[3]
- # Version 1; exotic.
- def count_combinations(s):
- diff = np.diff([0] + sorted(map(int, s.split())))
- lengths_of_ones = map(len, re.findall('1+', ''.join(map(str, diff))))
- def f(n):
- """Number of combinations from a sequence of n consecutive one-diffs."""
- return 0 if n < 0 else 1 if n < 2 else f(n - 1) + f(n - 2) + f(n - 3)
- return np.prod(list(map(f, lengths_of_ones)))
- # Version 2; inspired by Ray's solution.
- def count_combinations(s):
- l = [0] + sorted(map(int, s.split()))
- npaths = [1] + [0] * (len(l) - 1)
- for i in range(len(l)):
- for j in range(i + 1, min(i + 4, len(l))):
- if l[j] - l[i] <= 3:
- npaths[j] += npaths[i]
- return npaths[-1]
- # Version 3; "gather/pull".
- def count_combinations(s):
- l = [0] + sorted(map(int, s.split()))
- npaths = [1]
- for i in range(1, len(l)):
- npaths.append(
- sum(npaths[j]
- for j in range(-4, 0)
- if i + j >= 0 and l[i] - l[i + j] < 4))
- return npaths[-1]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement