ocehb: (Default)
[personal profile] ocehb
#Create a script which takes a list of numbers from command line and 
#print the same in the compact form. For example, 
#if you pass “1,2,3,4,9,10,14,15,16” then it should print the compact form like “1-4,9,10,14-16”.



# perl -le '$n = $ARGV[0]; foreach(@ARGV) { if ($n++ == $_) { push @{$a[$k]},$_; } else { push @{$a[++$k]},$_; $n=$_+1} } END{ print join",", map { "$_->[0]-$_->[-1]" } @a}' 1 2 3 4 7 8 10 11 12 13     
1-4,7-8,10-13
#


Аргументы должны быть уже сортированными.

Date: 2019-05-09 12:56 pm (UTC)
From: [identity profile] ico.livejournal.com
но там написано же "Create a script", а не "Create a one command line" :)
На олимпиадах я такое заворачивал сразу :)

Profile

ocehb: (Default)
ocehb

January 2021

S M T W T F S
     12
345 6789
10111213141516
17181920212223
24252627282930
31      

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Jun. 1st, 2025 02:49 am
Powered by Dreamwidth Studios