Skip to content

Instantly share code, notes, and snippets.

@mshock
Created June 27, 2012 19:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mshock/3006099 to your computer and use it in GitHub Desktop.
Save mshock/3006099 to your computer and use it in GitHub Desktop.
golf - number range simplification
# 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
@mshock
Copy link
Author

mshock commented Jun 27, 2012

"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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment