115 Answers
New AnswerWrite a program that takes a list of integers in increasing order and returns a correctly formatted string in the range format. Example: Input: [-6, -3, -2, -1, 0, 1, 3, 4, 5, 7, 8, 9, 10, 11, 14, 15, 17, 18, 19, 20] Output "-6,-3-1,3-5,7-11,14,15,17-20" Example 2: Input: [-5,-4,0,5,6,7,9,10] output: "-5,-4,0,5-7,9,10" Example 3: Input: [1,2,3,5,6,8,9,10,11] Output: "1-3,5,6,8-11" Note: a range spans at least 3 numbers. e.g. 1,2,3: 1-3 and 5,6: 5,6 and 8,9,10,11: 8-11 Have fun! Here, the reference: https://www.rosettacode.org/wiki/Range_extraction and here the kata: https://www.codewars.com/kata/51ba717bb08c1cd60f00002f
9/2/2017 3:33:42 PM
ysraelcon115 Answers
New AnswerCan you explain your challenge again? I read it 7 times and couldn't understand it at all
For those Who don't understand the challenge basicly if you have a range of numbers like 0,1,2,3,5,6,7 the output Will be 0-3,5-7 indicating the number in range beetween 0 and 3 in increasing order and then from 5-7 beacuse they have both in beetween numbers that are consequential. ( Sorry for the bad english )
for those who might not understand: take a list of integers which increase in order and find 3+ numbers which follow each other directly, example: 1,2,3 or 6,7,8 or 17,18,19, and make them into the format of 1-3, 6-8, 17-19, and include the other numbers. OK so to clarify, a list - [-6,-5,-2,0,1,2,3,6,7,9,10,11] when you change it to the required format: "-6,-5,-2,0-3,6,7,9-11" So we are just grouping 3 or more consecutive numbers in a list. If I've made a mistake feel free to correct me in the comments
Here is a short version, just to use elegantly python's possibilities https://code.sololearn.com/ciH9qbyCE6bA
Extremely badly done but I don't care, there you go: https://code.sololearn.com/WmJhk7XEf30K/?ref=app (link's not working anymore cuz code removed)
It isn't the shortest possible, I believe, but at least readable https://code.sololearn.com/c8MF0pQ721l6/?ref=app
Sololearn Inc.
535 Mission Street, Suite 1591Send us a message