Part 1
In this puzzle, we have a list of numbers. Starting with a base of zero, we should add all the numbers in the list and return the final result.
We can get the total sum by using sum(list).
Part 2
In part 2, we should add the numbers one at a time, repeating the original list multiple times if necessary. We should return the first repeated number.
To save the previoulsy seen numbers, we used a set. For repeating the list infinitely times, the itertools.cycle function was used. The rest was straight forward:
freqs = set()
freq = 0
for l in itertools.cycle(frequencies):
freq += l
if freq in freqs:
return freq
freqs.add(freq)
No comments:
Post a Comment