var loves = “loves”.toList map( x => “roger federer maria sharapova”.toList count(x == _ ))

while (loves.length > 1)

loves = loves zip(loves tail) map {case (a, b) => a+b}

println(“3 line compatibility = ” + loves.head * 2 + ” %”)

I’ve posted it in the blog with code highlighting for more clarity.

]]>var loves = “loves”.Select(x => “roger federer maria sharapova”.Count(c => x == c)).ToList();

while (loves.Count > 1)

loves = Enumerable.Range(0, loves.Count – 1).Select(i => loves[i] + loves[i + 1]).ToList();

Console.WriteLine(loves.Single() * 2);

code is here – http://pastebin.com/f57392163

]]>compare

“adding up pairs of values in the list, creating a new list, then repeating until there’s one value left.”

with

http://en.wikipedia.org/wiki/Pascals_triangle

to get

String names = “roger federer maria sharapova”,

compWord = “loves”;

int result = 0;

int n = compWord.length()-1;

for(int i=0; i<=n; i++)

result += c(i,n)*count(compWord.charAt(i), names);

System.out.println(“\nCalculated compatibility = ” + result*2 + ” %”);

// return the number of occurrences of c in s

int count(char c, String s) {

return s.replaceAll(“[^”+c+”]”,””).length();

}

// calculates the binomial coefficient

int c(int n, int k) {

if (n==0 || n==k) return 1;

return c(n-1,k)*(k-n+1)/n;

}

nice article tony, I’ve downloaded scala – time to try it 🙂

]]>