[Tutor] fake defrag revisited
Dave Angel
d at davea.name
Sat Oct 1 20:03:44 CEST 2011
On 10/01/2011 11:53 AM, R. Alan Monroe wrote:
>>> achieve the cosmetic randomness, until I realized the real problem is
>>> magically determining the correct sequence in which to perform the
>>> moves without ruining a future move inadverently.
>>>
>>> If I move 0-to-1 first, I've now ruined the future 1-to-22 which ought
>>> to have taken place in advance.
>> For a deterministic algorithm, simply sort that list of tuples, based on
>> the second item.
> Thanks, although I'm not seeing how that helps:
> [(63, 63), (62, 62), (61, 61), (60, 60), (59, 59), (58, 58), (57, 57),
> (52, 56), (49, 55), (47, 54), (46, 53), (44, 52), (43, 51), ( 42, 50),
> (41, 49), (40, 48), (39, 47), (38, 46), (36, 45), (34, 44), (33, 43),
> (32, 42), (31, 41), (30, 40), (28, 39), (27, 38), (26 , 37), (25, 36),
> (23, 35), (22, 34), (18, 33), (16, 32), (15, 31), (14, 30), (13, 29),
> (11, 28), (10, 27), (9, 26), (7, 25), (6, 24) , (3, 23), (1, 22), (48,
> 21), (37, 20), (4, 19), (12, 18), (17, 17), (53, 16), (20, 15), (2,
> 14), (54, 13), (21, 12), (29, 11), (24, 10), (55, 9), (51, 8), (50,
> 7), (8, 6), (35, 5), (5, 4), (19, 3), (56, 2), (0, 1), (45, 0)]
> This still suffers the same ruin-a-downstream-move problem when you
> see 35-to-5 ruining the future move 5-to-4.
>
> Alan
You missed the rest of the paragraph. Don't wait for the sort to
finish, you do the swapping in the compare function.
+++
For a deterministic algorithm, simply sort that list of tuples, based on
the second item. The sort will swap two tuples each time, and they will
gradually become more ordered. Whether this will appear visually random
depends partly on what your initial order is, and on which sort
algorithm you implement. If you use the builtin sort (by supplying your
own compare function callback), you can add the visual swap each time
your comparator will return true.
+++
Are you running Python 3.* ? If so, they dropped the compare function
parameter, so you'd have to roll your own. What version are you using?
In Python 2.x, you can call it as
dummy = sorted(mylist, cmp=comp_function)
Where comp_function is a function that takes two arguments (in your case
each is a two-tuple). Normally, you'd just compare the second member of
each tuple, and if less, do your swap of the corresponding items in your
display.
.....
Continuing on with the same paragraph:
+++
Or you could use a bubble sort, which won't generally look as random.
+++
In this case, you write your own sort function, calling a common
function for doing the comparison, and another to do the actual swap.
And when you're swapping the tuples, you can also swap the display.
--
DaveA
More information about the Tutor
mailing list