There was an error in my comp function in the code I have just posted, here is 
it after modification:

import std.stdio;
import std.string;
import std.array;
import std.conv;
import std.algorithm;
/*
   This program works as follows:
   there is a string[] of users with genders and their answers to a multichoice 
survey
   the program takes a user name as input, the user wants to find other users 
who answered at least a number (called similarity factor)
   of question as he/she did.
   the program then outputs a list of users who are ordered by how similar 
their answers to the user, if two users have the same
   similarity factor, they are then ordered by name.
*/
void main()
{
        string[] members=
        [
                "BETTY F M A A C C",
                "TOM3 M F A D C A",
                "TOM M F A D C A",
                "SUE F M D D D D",
                "ELLEN F M A A C A",
                "TOM6 M F A D C A",
                "JOE M F A A C A",
                "TOM12 M F A D C A",
                "ED M F A D D A",
                "SALLY F M C D A B",
                "MARGE F M A A C C",
                "TOM4 M F A D C A",
        ];
//      the form is: "NAME G RG X X X .." where G is gender, RG is requested 
gender, X is the answer to a multichoice question
        foreach (string s; bestmatch(members,"BETTY",2))
                writefln(s);
}
string[] bestmatch(string[] ms, string user, int sf)
{
        string[] u,tmp;
        foreach (string m; ms)
                if (((u=m.split())[0])==user) break;
        auto re = appender!(string[]);
        auto sa = appender!(int[]);
        int s;
        foreach (string m; ms)
        {
                s = 0;
                if ((tmp=m.split())[1]==u[2])
                {
                        for (int i = 3; i<u.length; i++)
                                if (tmp[i]==u[i]) s++;
                }
                if (s>=sf)
                        re.put(join([to!(string)(s),tmp[0]]," "));
        }
        sort!(comp)(re.data);
        for (int i = 0; i<re.data.length; i++)
                re.data[i] = re.data[i].split()[1];
        return re.data;
}
bool comp(string x, string y)
{
        writefln("now: %s %s", x, y);
        string[] a = x.split(); string[] b = y.split();
        int cmp = (to!(int)(a[0])>to!(int)(b[0]));
        if (cmp>0)
                return 1;
        else if (cmp==0 && a[1]<b[1])
                return 1;
        return 0;
}

Reply via email to