## Thursday, February 24, 2011

### Hyperpublic programming challenge

A start-up put up a small programming challenge.

There was some doubt whether the automatic answer key was correct, so I decided to check the answers using Mathematica. (note, it might be considered cheating to submit solutions obtained using Mathematica)

The first problem asks to find "influence" which comes down to the problem of counting descendants of each node in a graph, and can be done using recursion with memoization. Using Daniel Lichtblau's suggestion on how to parse the adjacency matrix, here's the whole solution.

words = ReadList["challenge2input.txt", Word];mat = Map[Characters, words] /. {"O" -> 0, "X" -> 1};inf[i_] := (inf[i] =     1 + If[Total[mat[[i]]] == 0, 0,       Total[inf /@ Flatten@Position[mat[[i]], 1]]]);influences = inf /@ Range@Length@mat - 1;top3 = (Reverse@Sort@influences)[[;; 3]];answer1 = StringJoin[ToString /@ top3]

Checking the answer to the second "hard" problem is actually easier than the first because you can use Mathematica's MIP solver. The problem basically asks for the smallest number of integers of several different type needed so that their sum adds up to a given total. Here's how you could do it in Mathematica

mintasks[total_] := (   types = {2, 3, 17, 23, 42, 98};   vars = x /@ Range@Length@types;   poscons = And @@ (# >= 0 & /@ vars);   First@Minimize[{Total[vars], vars.types == total && poscons}, vars,      Integers]   );counts = mintasks /@ {2349, 2102, 2001, 1747};answer2 = Times @@ counts

#### 1 comment:

1. its very good post about Dentist .
Mathematica