← Back to challenges

Repeating Cycle

PythonHardarraysvalidation

Instructions

Below is an example of a repeating cycle.

is_repeating_cycle([1, 2, 3, 1, 2], 3) ➞ True
# Since the first two elements of [1, 2, 3] equals [1, 2]

Below is an example of a non-repeating cycle.

is_repeating_cycle([1, 2, 3, 1, 3], 3) ➞ False
# Since [1, 2, 3] != [1, 3]

You are tasked with writing a function that takes in two inputs:

  1. A list of integers.
  2. The length of each cycle.

Return the boolean value True if the list is a repeating cycle, and False if the list is a non-repeating cycle.

Examples

is_repeating_cycle([1, 2, 3, 1, 2, 3, 1], 3) ➞ True

is_repeating_cycle([1, 2, 3, 4, 2, 3, 1], 4) ➞ False

is_repeating_cycle([1, 2, 1, 2, 2], 2) ➞ False

is_repeating_cycle([1, 1, 1, 1], 3) ➞ True

Notes

  • All cycles begin with the first element of the list.
  • Return True if the cycle length is greater than the list length.
python3
Loading editor…
to run
Walks through the solution with reasoning and edge cases.