in reply to Re^2: Puzzle: Given an array of integers, find the best sequence of pop / shift...
in thread Puzzle: Given an array of integers, find the best sequence of pop / shift...

Nope, he's spot on. The decision player one gets to make is at the beginning.

imagine a 6 element list...

The beginning, bottom row is the number, top row is odds/evens flag. 1 0 1 0 1 0 5 1 3 4 5 3 player 1 totals the odds and the evens, decides to go for odds, and takes from the left 1 0 1 0 1 0 1 3 4 5 3 player 2 has to take an even element, lets say from the right 1 0 1 0 1 0 1 3 4 5 player 1 is after odds, so takes from the right 1 0 1 0 1 0 1 3 4 hmmm, player two is left with the evens again. 1 0 1 0 1 0 1 3 player 1 sticks with odds 1 0 1 0 1 0 1 player two takes the last even element, and loses

If you try this strategy on the example numbers the OP gave, you win by ten, which is as good as strategy in the OP's example code.

---
my name's not Keith, and I'm not reasonable.
  • Comment on Re^3: Puzzle: Given an array of integers, find the best sequence of pop / shift...
  • Download Code

Replies are listed 'Best First'.
Re^4: Puzzle: Given an array of integers, find the best sequence of pop / shift...
by McDarren (Abbot) on Mar 20, 2006 at 12:22 UTC
    Hang about.....

    If you are shifting and popping, aren't the indices going to change as you go?

    Well, actually - only as you shift. That is, each time you shift an element off the list, the index of each remaining element is reduced by one - no?

    Therfore, this whole odds/evens thing won't work at all - or have I been smoking too much happy weed? ;)

    (just kidding - I never touch the stuff ;)

      Yes, shifting and popping will change the index. However, the point the OP is trying to make is that the player who starts first can force the second player to take all evens or all odds from the original layout. With this ability, the first player can total the two possibilities and decide the outcome before the game even starts. Quite clever.

      Dave
        Okay, I understand what you are saying - but I'm still not sure if I buy it. Isn't this mistakenly assuming (like I did in my original reply) that each player (or at least player 2) will take the highest of the two available numbers at each turn?