Ruler Pattern
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
The pattern between 1 and 2 is [1, 2, 1]; write it, then 3, then write it again.
Example 2
With lo = hi the inner pattern is empty, so only the value 5 remains.
Constraints
-20 <= lo <= 20lo - 1 <= hi <= lo + 5, so the pattern has at most 63 values
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More Recursion problems
- Same Both WaysBeginner
- Suffix ChainBeginner
- Wheels in the RowBeginner
- Deep ReverseEasy
- Depth-Weighted SumEasy
- Digit Product StepsEasy
- Downhill SpillEasy
- Every ArrangementEasy