Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Puzzle: Given an array of integers, find the best sequence of pop / shift...

by McDarren (Abbot)
on Mar 20, 2006 at 03:20 UTC ( [id://537863]=note: print w/replies, xml ) Need Help??


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

Unless I'm missing something, I reckon your solution is a lot more complex than it needs to be.

Given that "the other player also picks his best move", then all you need to do is determine which produces the higher score - a pop-first, or a shift-first.

This is my go at it:

#!/usr/bin/perl -wl use strict; my @numbers = qw(16 2 10 2 9 17 5 8 15 14 20 19 19 11 10 11 9 13 7 13) +; my $shiftscore = checkscore("shift"); @numbers = qw(16 2 10 2 9 17 5 8 15 14 20 19 19 11 10 11 9 13 7 13); my $popscore = checkscore("pop"); my $bestscore = $popscore > $shiftscore ? $popscore : $shiftscore; print "POP:$popscore:SHIFT:$shiftscore"; print "Best Score:$bestscore"; sub checkscore { my $first_turn = shift; my ($player, $opponent); $player = $first_turn eq "pop" ? pop(@numbers) : shift(@numbers); while (@numbers) { $opponent += $numbers[0] > $numbers[-1] ? shift(@numbers) : pop(@numbers); last if !@numbers; $player += $numbers[0] > $numbers[-1] ? shift(@numbers) : pop(@numbers); } return ($player - $opponent); }
Which gives:
POP:4:SHIFT:10 Best Score:10

Update: To test how long it takes with 1000 numbers I added the following lines:.

use List::Util qw(shuffle); my @numbers = shuffle(1 .. 1000); print join(" ", @numbers); my @copy = @numbers; my $shiftscore = checkscore("shift"); @numbers = @copy; my $popscore = checkscore("pop");
And I get something like this:
POP:4354:SHIFT:7452 Best Score:7452 real 0m0.049s user 0m0.030s sys 0m0.000s
PS: Like the OP, I'm not 100% sure that I'm getting correct results - and I'm not sure how to verify it for very large ranges, I guess I'll just wait for some more responses :)

Update 2: - as tilly has pointed out, my solution is wrong. I kindof suspected that it would be - because it seemed too simple. And although I acknowledged further down in the thread that it was wrong, I forgot to add an update here.

Cheers,
Darren :)

Replies are listed 'Best First'.
Re^2: Puzzle: Given an array of integers, find the best sequence of pop / shift...
by tilly (Archbishop) on Mar 21, 2006 at 00:35 UTC
    Because most people are not following this discussion they may not have noticed that Darren's solution is wrong.

    What he's written is to make the best choice at every turn based on the assumption that both players will thereafter play by a greedy strategy. However the greedy solution is suboptimal play, and the smart opponent will not do that.

    As an example for the game 38 67 63 43 83 66, Darren's solution says that if you pop, you'll get a final score of 4, versus -26 if you shift, so you should pop. In fact by shifting you can get a score of 8, while with pop you can be forced to a score of -8.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://537863]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2024-04-24 19:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found