FunctionRacerProblemsRace

Ruler Pattern

BeginnerRecursionPython, JavaScript, TypeScript, Java, C++

Return the ruler pattern between the integers lo and hi, built by this rule: when hi < lo the pattern is the empty list; otherwise it is the pattern between lo and hi - 1, then the single value hi, then the pattern between lo and hi - 1 once more.

A loop can build it, but the rule is recursive and a recursive function follows it word for word.

For example, lo = 1, hi = 3 gives [1, 2, 1, 3, 1, 2, 1]: the pattern between 1 and 2 is [1, 2, 1], and it appears on both sides of the 3.

Example 1

lo 1
hi 3
returns [1,2,1,3,1,2,1]

The pattern between 1 and 2 is [1, 2, 1]; write it, then 3, then write it again.

Example 2

lo 5
hi 5
returns [5]

With lo = hi the inner pattern is empty, so only the value 5 remains.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Recursion problems