Created
June 27, 2012 19:07
-
-
Save mshock/3006099 to your computer and use it in GitHub Desktop.
golf - number range simplification
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# current solution - regex - 85 | |
#!perl -p | |
1while(s/((\d+)-)?(\d+)\.? (\d+)/$3==$4-1?"@{[$2||$3]}-$4.":"$1$3, $4."/e) | |
###################################################################################### | |
=pod | |
# scratch and previous solutions | |
while(s/(\d+)?-?(\d+),? (\d+) \?/$2==$3-1?"@{[$1||$2]}-$3, ":"$2, $3, "/e){} | |
sub r{pop=~s/(\d+) (\d+)//?} | |
$i=<>;$i=~s/(\d+) (\d+)/$1, $2/g | |
#!perl -p | |
while(s/[(\d+)-](\d+)[, ]+(\d+)/$2==$1+1?'$1'/e){} | |
for(split<>){$p eq$_-1?:$p=$_;} | |
#!perl -pa | |
for(@F){$t[-1]eq$_-1?$:push@t,$_} | |
for(split" ",<>){$s=~s/[(\d+), ]?-?(\d*)$/$2 eq$_-1?"$1-$_":", $_"/e} | |
s/(\d+)[ \d+]*(\d+)//e | |
=cut |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
"1 2 3 5 6 8" => "1-3, 5-6, 8."
This was a fun one, still plenty of room for improvement. I wonder if there is a way to build the loop into the regex.