Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
GCD solution in Clear category for Evenly Spaced Trees by swagg010164
from typing import List
from math import gcd
def evenly_spaced_trees(a: List[int]) -> int:
diff = a[1] - a[0]
for i in range(1, len(a) - 1):
diff = min(diff, gcd(a[i+1] - a[i], diff))
return sum([(a[i+1] - a[i])//diff - 1 for i in range(len(a)-1)])
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!")
April 17, 2019
Comments: