Challenge #1 week 6
May. 9th, 2019 01:06 pm![[personal profile]](https://www.dreamwidth.org/img/silk/identity/user.png)
#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
#
Аргументы должны быть уже сортированными.
no subject
Date: 2019-05-09 12:56 pm (UTC)На олимпиадах я такое заворачивал сразу :)
no subject
Date: 2019-05-10 03:06 am (UTC)И потом, я ведь оставляю пробелы!