見習村25 - Range Extraction
25 - Range Extraction
Don’t say so much, just coding…
Instruction
A format for expressing an ordered list of integers is to use a comma separated list of either
- individual integers
- or a range of integers denoted by the starting integer separated from the end integer in the range by a dash, ‘-‘. The range includes all integers in the interval including both endpoints. It is not considered a range unless it spans at least 3 numbers. For example (“12, 13, 15-17”)
Complete the solution so that it takes a list of integers in increasing order and returns a correctly formatted string in the range format.
Example:
1 |
|
Sample Testing
1 |
|
Javascript
Init
1 |
|
Sample Testing
1 |
|
Thinking
想法(1): 如果現在的數 +1 跟下一位數相等,就會被算在同個區間
想法(2): 如果只有一位數的話回傳該數,一位到兩位數之間回傳該兩位數,超過則回傳區間並且加上符號 -
Hint & Reference
- Ruby
- JavaScript
Solution
Ruby
1 |
|
Javascript
1 |
|