Click here to Skip to main content
15,898,134 members
Home / Discussions / Algorithms
   

Algorithms

 
GeneralRe: Records and Clusters Pin
Member 41945936-Nov-08 7:57
Member 41945936-Nov-08 7:57 
GeneralRe: Records and Clusters Pin
Member 419459325-Apr-09 4:28
Member 419459325-Apr-09 4:28 
GeneralRe: Records and Clusters Pin
DQNOK27-Apr-09 3:13
professionalDQNOK27-Apr-09 3:13 
GeneralRe: Records and Clusters Pin
DQNOK27-Apr-09 5:57
professionalDQNOK27-Apr-09 5:57 
GeneralRe: Records and Clusters Pin
Member 419459327-Apr-09 11:22
Member 419459327-Apr-09 11:22 
QuestionRePosted from C# Forums: A Job Exam Question Pin
Bulky Fellow4-Nov-08 20:31
Bulky Fellow4-Nov-08 20:31 
AnswerRe: RePosted from C# Forums: A Job Exam Question Pin
Arash Partow4-Nov-08 21:59
Arash Partow4-Nov-08 21:59 
AnswerRe: RePosted from C# Forums: A Job Exam Question Pin
Mark Churchill5-Nov-08 1:46
Mark Churchill5-Nov-08 1:46 
My 10 mins. I really wanted to do something cool with yield, but the difficulty of nesting these really stuffed me. I wanted to recursively traverse the mask like a b-tree and yield out results along the way. Frown | :(

    class SubsetSummer<br />
    {<br />
        static IEnumerable< long> SubsetSumCore(IList< int> values, int target)<br />
        {<br />
            long mask = (1 << values.Count);<br />
<br />
            while(mask-- > 0)<br />
            {<br />
                int total = 0;<br />
                for(int bit = 0; bit< values.Count; bit++)<br />
                {<br />
                    if ((mask & 1 << bit) > 0) total += values[bit];<br />
                }<br />
                   <br />
                if (total == target) yield return mask;<br />
            }<br />
        }<br />
<br />
        static IEnumerable< List< int>> SubsetSum(IList< int> values, int target)<br />
        {<br />
            foreach (var resultMask in SubsetSumCore(values,target))<br />
            {<br />
                var results = new List< int>();<br />
                for (int bit = 0; bit < values.Count; bit++)<br />
                {<br />
                    if ((resultMask & 1 << bit) > 0) results.Add(values[bit]);<br />
                }<br />
                yield return results;<br />
            }<br />
        }<br />
<br />
        static void Main(string[] args)<br />
        {<br />
            var values = new []{7, 8, 12, 3, 4};<br />
<br />
            foreach (var result in SubsetSum(values, 15))<br />
            {<br />
                Console.WriteLine(string.Join(" + ", result.ConvertAll(i => i.ToString()).ToArray()));<br />
            }<br />
        }<br />
    }


(wow that really shagged my generics)


GeneralRe: RePosted from C# Forums: A Job Exam Question Pin
riced9-Nov-08 4:03
riced9-Nov-08 4:03 
GeneralRe: RePosted from C# Forums: A Job Exam Question Pin
Bulky Fellow9-Nov-08 7:45
Bulky Fellow9-Nov-08 7:45 
GeneralRe: RePosted from C# Forums: A Job Exam Question Pin
Mark Churchill9-Nov-08 11:33
Mark Churchill9-Nov-08 11:33 
QuestionTrammel method for constructing an ellipse Pin
hxhl9525-Oct-08 19:38
hxhl9525-Oct-08 19:38 
AnswerRe: Trammel method for constructing an ellipse Pin
73Zeppelin25-Oct-08 23:52
73Zeppelin25-Oct-08 23:52 
GeneralRe: Trammel method for constructing an ellipse [modified] Pin
Luc Pattyn26-Oct-08 9:02
sitebuilderLuc Pattyn26-Oct-08 9:02 
GeneralRe: Trammel method for constructing an ellipse Pin
hxhl9526-Oct-08 16:43
hxhl9526-Oct-08 16:43 
QuestionPRECISION and ACCURACY Pin
εїзεїзεїз22-Oct-08 1:27
εїзεїзεїз22-Oct-08 1:27 
JokeRe: PRECISION and ACCURACY Pin
CPallini22-Oct-08 2:21
mveCPallini22-Oct-08 2:21 
AnswerRe: PRECISION and ACCURACY Pin
73Zeppelin22-Oct-08 2:58
73Zeppelin22-Oct-08 2:58 
GeneralRe: PRECISION and ACCURACY Pin
εїзεїзεїз22-Oct-08 3:10
εїзεїзεїз22-Oct-08 3:10 
GeneralRe: PRECISION and ACCURACY Pin
Andrew Rissing22-Oct-08 6:53
Andrew Rissing22-Oct-08 6:53 
GeneralRe: PRECISION and ACCURACY Pin
73Zeppelin22-Oct-08 7:02
73Zeppelin22-Oct-08 7:02 
GeneralRe: PRECISION and ACCURACY Pin
Paul Conrad22-Oct-08 8:05
professionalPaul Conrad22-Oct-08 8:05 
GeneralRe: PRECISION and ACCURACY Pin
PIEBALDconsult13-Nov-08 16:14
mvePIEBALDconsult13-Nov-08 16:14 
AnswerRe: PRECISION and ACCURACY Pin
Roger Wright23-Oct-08 11:28
professionalRoger Wright23-Oct-08 11:28 
GeneralRe: PRECISION and ACCURACY Pin
Luc Pattyn26-Oct-08 9:07
sitebuilderLuc Pattyn26-Oct-08 9:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.