Open the Lockers
Description:
Problem
There is a line of lockers numbered 1
to N
, initially all closed. A man walks down the line, opens 1
, and then alternately skips and opens each closed locker (i.e. he opens 1
, skips 2
, opens 3
, skips 4
, and so on).
At the end of the line, he turns around, and walks: opening the first closed locker he reaches, and alternately skips and opens each closed locker. He does this again (walking forward, then backward, then forward, ...), until all the lockers are opened.
Return a function LastLocker(N)
that will return the number of the locker opened last.
Input / Output
Input: Any positive integer N
(no need to check it)
Output: Integer (the last locker)
Source
Shoutout to our professors in maths class for introducing to us this problem. I don't know who actually made this problem (probably from Putnam / local Maths Olympiad), but this actually is fascinating -- for me at least.
Similar Kata:
Stats:
Created | Jun 21, 2016 |
Warriors Trained | 80 |
Total Skips | 18 |
Total Code Submissions | 145 |
Total Times Completed | 22 |
Python Completions | 22 |
Total Stars | 4 |
% of votes with a positive feedback rating | 81% of 13 |
Total "Very Satisfied" Votes | 10 |
Total "Somewhat Satisfied" Votes | 1 |
Total "Not Satisfied" Votes | 2 |
Total Rank Assessments | 12 |
Average Assessed Rank | 6 kyu |
Highest Assessed Rank | 5 kyu |
Lowest Assessed Rank | 7 kyu |