Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int[][] next(string haystack, string needle){
- if(!needle.length)
- return [[]];
- int[][] matches = [];
- foreach(hi, h; haystack){
- if(needle[0].toLower == h.toLower){
- foreach(m; haystack.next(needle[1..$])){
- if(m.all!(a => a > hi))
- matches ~= ([hi.to!int] ~ m);
- }
- }
- }
- return matches;
- }
- int[] compareFuzzy(string haystack, string needle){
- writeln(haystack);
- auto matches = haystack.next(needle);
- foreach(m; matches){
- foreach(i; 0..haystack.length){
- if(m.canFind(i))
- write("*");
- else
- write(" ");
- }
- writeln;
- }
- int best = -1;
- int bestScore;
- foreach(i, m; matches){
- if(m.length < needle.length)
- continue;
- auto s = m.score;
- if(s > bestScore){
- bestScore = s;
- best = i.to!int;
- }
- }
- if(best < 0)
- return [0];
- return [bestScore] ~ matches[best];
- }
- int score(int[] match){
- int s = 0;
- int previous = -1;
- foreach(m; match){
- if(previous == -1){
- previous = m;
- continue;
- }
- if(m == previous+1){
- s += 1000;
- }
- previous = m;
- }
- return s;
- }
- unittest {
- assert("122".compareFuzzy("123")[0] <= 0);
- assert("3345".compareFuzzy("345")[0] > 0);
- assert(
- "3345".compareFuzzy("345")[0] ==
- "2345".compareFuzzy("345")[0]
- );
- assert(
- "3245".compareFuzzy("345")[0] <
- "2345".compareFuzzy("345")[0]
- );
- assert("ls".compareFuzzy("ls")[0] > 0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement