For a given list of different numbers from 1 to N, you need to build an abbreviated version of the list: if there are several numbers in a row, they are replaced with an interval through the dash. Example:

1,3,4,5,6,7,8,10,12,16,17,20,21,22,23,24

turns into

1,3-8,10,12,16-17,20-24

How to implement it in the most elegant way?

The specific language is not important, but better than C ++ or similar.

Closed due to the fact that off-topic party PashaPash Jun 3 '17 at 7:51 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • "The message contains only the text of the task, in which there is no description of the problem, or the question is purely formal (" how do I do this task ") . To reopen the question, add a description of the specific problem, explain what does not work, what you see the problem. " - PashaPash
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • one
    And what is the question? - Vladimir Martyanov
  • You probably confused the site, this is not freelancing. - alias
  • 2
    This is not a question, but a request :) For this site it goes very far beyond ... - Harry
  • one
    I think in the framework of golf this question is valid. - val

1 answer 1

Works in Visual Studio 2015. Long arithmetic is supported.

#include <iostream> #include <string> #include <regex> using namespace std; int main() { string s; cin >> s; cout << regex_replace ( regex_replace ( regex_replace ( s, regex("\\d+\\b"), "$&#$&-$&" ) + "0123456789", regex("(\\b(?=9.*(1))|0(?=.*(1))|1(?=.*(2))|2(?=.*(3))|3(?=.*(4))|4(?=.*(5))|5(?=.*(6))|6(?=.*(7))|7(?=.*(8))|8(?=.*(9))|9(?=.*(0)))(?=9*-)|\\d{10}$"), "$2$3$4$5$6$7$8$9$10$11$12" ), regex("(#(\\d+)-\\d+,\\2\\b)+#\\d+|#\\d+-\\d+"), "" ) << '\n'; return 0; } 
  • And why is regex used here? Here on the cycles to do better. - Alex78191
  • @ Alex78191, and the question rating in -12 does not tell you that nobody wants to answer it normally? But it was interesting to play with the regulars - I hardly know them in the pluses. - Qwertiy
  • Unfortunately the question is closed and does not answer it - Alex78191
  • @ Alex78191, do you want to rediscover? - Qwertiy
  • I believe that the reason for the closure is incorrect - Alex78191