Well, I thought it was a valid move because it is exactly what happens in the original post.
my $before = [qw(1 2 3 4 5 6 7 8 9 10)];
my $after = [qw(2 3 4 5 1 6 7 8 9 10)];
Element 0, the 1, has been spliced out then spliced back in as element 4. rafl also says in his first reply "the items can move forwards and backwards in the list." Also, nowhere in the posts is any constraint placed on where an element can be moved from or to.
There is another wee bugette in that the sort to find $smallest should be a numerical one:-
my $smallest = (sort {$a <=> $b} @array)[0];
otherwise @array = (4 .. 13); would come up with 10 as it's "smallest" item because lexically 10 comes before 4.
If we accept that we can move a number to the beginning of the list I still think there is a problem with your logic. If we start with
my @array = (7,1,2,3,4,5,6,8,9);
we get a $smallest of 1 and a $stored of 7 and $array[0] is also 1. Applying your logic, 7 != 1 && (7 + 1) != 1 is true and you exit with the error. Cheers JohnGG |