ocehb: (Default)
ocehb ([personal profile] ocehb) wrote2019-05-09 01:06 pm
Entry tags:

Challenge #1 week 6

#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
#


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

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