Close

Results 1 to 5 of 5
  1. #1
    DF Wh0re ancojo's Avatar
    Join Date
    May 2014
    Location
    UK
    Posts
    142
    Thanks
    86
    Thanked:        30
    Karma Level
    84

    Default Sons homework, how many can you get?

    See how many ways you can make 1000 using the numbers below i.e 754+246
    Easy eh? NOPE!

    59, 123, 156, 246, 274
    329, 338, 465, 487, 512
    573, 619, 692, 754, 781
    853, 871, 945, 924, 382

    I got one!

  2. #2
    DF VIP Member Undertaker's Avatar
    Join Date
    Nov 2000
    Location
    Earth
    Posts
    2,518
    Thanks
    39
    Thanked:        189
    Karma Level
    448

    Default Re: Sons homework, how many can you get?

    only two ways

    59,274,329,338

    and of course 754,246

    simple if using visual studio c# and a console app
    oh and I was lazy and I googled the code below, given time I would have done it from scratch, buy hey ho, 2 mins and job done,



    Code:
         private static void Main(string[] args)        {
    
    
    
    
                int[] set =
                {
                    59, 123, 156, 246, 274, 329, 338, 465, 487, 512, 573, 619, 692, 754, 781, 853, 871, 945, 924,
                    382
                };
    
    
                foreach (string s in GetCombinations(set, 1000, ""))
                {
                    Console.WriteLine(s);
    
    
                }
                Console.ReadKey();
            }
    
    
            public static IEnumerable<string> GetCombinations(int[] set, int sum, string values)
            {
                for (int i = 0; i < set.Length; i++)
                {
                    int left = sum - set[i];
                    string vals = set[i] + "," + values;
                    if (left == 0)
                    {
                        yield return vals;
                    }
                    else
                    {
                        int[] possible = set.Take(i).Where(n => n <= sum).ToArray();
                        if (possible.Length > 0)
                        {
                            foreach (string s in GetCombinations(possible, left, vals))
                            {
                                yield return s;
                            }
                        }
                    }
                }
            }

    Thanks to Undertaker

    ancojo (30th April 2015) 


  3. #3
    DF VIP Member Undertaker's Avatar
    Join Date
    Nov 2000
    Location
    Earth
    Posts
    2,518
    Thanks
    39
    Thanked:        189
    Karma Level
    448

    Default Re: Sons homework, how many can you get?

    but then again question is not very clear, is that just addition, or can we use division, multiplcation, substraction etc

    if so then code gets complicated

    anyway, its late, night night

    Thanks to Undertaker

    ancojo (30th April 2015) 


  4. #4
    DF Wh0re ancojo's Avatar
    Join Date
    May 2014
    Location
    UK
    Posts
    142
    Thanks
    86
    Thanked:        30
    Karma Level
    84

    Default Re: Sons homework, how many can you get?

    As its an 8 year olds homework I would assume its just adding and subtracting?

    Thanks for your replies, much appreciated.

  5. #5
    DF Jedi blacksheep's Avatar
    Join Date
    Jun 2006
    Location
    Manchester
    Posts
    3,877
    Thanks
    87
    Thanked:        265
    Karma Level
    514

    Default Re: Sons homework, how many can you get?

    This is a stupid exercise and as a parent I'd give feedback on that, it's not teaching the kid anything other than trial and error and unless I'm wrong the solution is what's called np complete and there's a million dollar prize if you can prove it can be solved otherwise (other than trying every permutation - which with this I guess you don't)

    actually this is quite a good interview question in some ways for coders. Might steal it. If you sort the list you obviously can't have two above the mid point so reduces choices - you can then work through the list like some of the sorting algorithms (which if you're at all interested there's visual video/graphics of how they work on wikipedia).

    anyway - Id say that's a little beyond an 8 year old and something I'd pitch to an experienced coder.
    Last edited by blacksheep; 30th April 2015 at 02:50 AM.

    2 Thanks given to blacksheep

    ancojo (30th April 2015), dpSparhawk (30th April 2015) 


Similar Threads

  1. Homework and a bit of fun..
    By [wingnut] in forum Your Photos & Artwork
    Replies: 8
    Last Post: 1st May 2006, 12:33 PM
  2. Beckham baffled by 6 year old sons homework
    By Deadly in forum News & Current Affairs
    Replies: 4
    Last Post: 28th February 2006, 11:54 AM
  3. Homework Assignment
    By ibobsy in forum The Comedy Club
    Replies: 0
    Last Post: 6th October 2003, 02:34 PM

Social Networking Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •