Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
reduce(gcd, trees) solution in Speedy category for Evenly Spaced Trees by PythonWithPI
from typing import List
from math import gcd
from functools import reduce
def evenly_spaced_trees(trees: List[int]) -> int:
min = trees[0]
max = trees[-1]
for i in range(len(trees)):
trees[i] -= min
separation = reduce(gcd, trees)
renge = range(min, max + separation, separation)
return (len(renge) - len(trees))
if __name__ == '__main__':
print("Example:")
print(evenly_spaced_trees([0, 2, 6]))
assert evenly_spaced_trees([0, 2, 6]) == 1, 'add 1'
assert evenly_spaced_trees([1, 3, 6]) == 3, 'add 3'
assert evenly_spaced_trees([0, 2, 4]) == 0, 'no add'
print("Coding complete? Click 'Check' to earn cool rewards!")
Jan. 28, 2020