Interview Question - Validate Page Range
I applied for a job position in a software company some time ago. They emailed me the following question and asked me to solve it in one day, but I failed to do so. Today I looked back at this question (to prepare for other interviews) and managed to write a solution, though it is lengthy and not optimized. I would like to see whether there's a better solution. _______________________________________________________________________ Here's the question: Given the 4-line testbench as below: isPageRange(1, '1,2,5-7,9-') # true isPageRange(6, '1,2,5-7,9-') # true isPageRange(12, '1,2,5-7,9-') # true isPageRange(3, '1,2,5-7,9-') # false Create the function isPageRange(). The first parameter is the page number. The second parameter is a string representing a range. The comma in the range represents 'and', the dash in the range represents 'to'. ________________________________________________________________________ Here's my solution: https://code.sololearn.com/cA23A9a4A18a/#py