Corion has asked for the wisdom of the Perl Monks concerning the following question:
I just stumbled across "Fuzzy finder in 10 lines of Python" and thought to myself that this would make a nice golf exercise, or at least in my implementation, a weird obfuscation. The idea is to sort a set of strings by order of the shortest leftmost occurance of a sequence of characters, excluding any string that doesn't contain the characters in sequence. You can think of this like some autocompletion. For example, for the string mig in the search set main_generator migrations django_migrations django_admin_log , the ranking of choices would be
migrationsMy solution, as also taken in the Python post, is to build a (somewhat saner/safer) regex from the input string and then search the input list against that.
The sample harness gets the data in @ARGV. The first argument is the substring to search, the rest is the list to sort. The sorted list is to be printed to STDOUT, one item per line.
When I remove the whitespace, my solution gets to 123 bytes.
#!perl -wl @ARGV=qw(mig main_generator migrations django_migrations django_admin_ +log myfoo\bar.txt ); # 1 2 3 4 5 6 7 + 8 #234567890123456789012345678901234567890123456789012345678901234567890 +12345678901234567890 #-- count from here ($i=shift)=~s#(.)(?!\z)#[$1].*?#g; print substr $_,16 for sort(map{ /($i)/ ? sprintf'%08x%08x%s',$+[0]-$-[0],$-[0],$_ : () }@ARGV)
The oneliner is:
#!perl -wl @ARGV=qw(mig main_generator migrations django_migrations django_admin_ +log myfoo\bar.txt ); # 1 2 3 4 5 6 7 + 8 #234567890123456789012345678901234567890123456789012345678901234567890 +12345678901234567890 #-- count from here ($i=shift)=~s#(.)(?!\z)#[$1].*?#g;print substr $_,16 for sort(map{/($i +)/?sprintf'%08x%08x%s',$+[0]-$-[0],$-[0],$_:()}@ARGV)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Golf: Fuzzy finder / autocomplete
by Anonymous Monk on Jun 22, 2015 at 19:52 UTC | |
|
Re: noGolf: Fuzzy finder / autocomplete
by Discipulus (Canon) on Jun 23, 2015 at 08:50 UTC | |
|
Re: Golf: Fuzzy finder / autocomplete
by choroba (Cardinal) on Jun 23, 2015 at 12:08 UTC |