Hi, Hallvard,

To ensure "unsorted", first sort, then interleave second and first
halves.  (Assuming you have enough distinct values that moving
halfway through the sorted order gives you a different value.)

Hallvard Ystad wrote:
> 
> I have a block with, say 10 blocks inside it. Each block has many
 > different data types within it (but only one element matters).
 > I want to unsort them so that _part of_  the fourth element (a
 > string) is not identical in adjacent blocks...
> 
> example: [[4 1 7 "nicsrg" 5 6 3 8 2 9] [3 1 2 "sicrtg" 9 8 4 7 6 5]
 > [9 3 7 "iscgtn" 6 2 8 5 4 1] [1 3 8 "inctrg" 5 2 9 4 6 7]
 > [9 6 5 "ngsirt" 8 2 1 3 7 4] [3 7 8 "igtnrs" 4 1 2 9 5 6]
 > [6 4 8 "gnrsit" 2 3 9 1 5 7] [9 5 4 "sntgir" 6 7 8 2 1 3]
 > [6 7 4 "gstinr" 1 5 2 3 9 8] [7 9 2 "sgcnit" 5 1 8 3 6 4]]
> 
> In the first four blocks, the third character in the fourth element
 > is #"c". I want these four blocks split apart. Getting at the
 > character in question is easy (foreach b example [print pick fourth
 > b 3]), but unsorting the blocks from there?
> 

Let's define (and demonstrate) the interleaving function first:

     riffle: func [b [block!] /local n j result] [
         j: to-integer (n: length? b) / 2
         result: make block! n
         for i 1 j 1 [
             append/only result pick b i + j
             append/only result pick b i
         ]
         if odd? n [append/only result last b]
         result
     ]

Which behaves as:

     >> riffle [0 1 2 3 4 5 6 7 8 9]
     == [5 0 6 1 7 2 8 3 9 4]
     >> riffle [0 1 2 3 4 5 6 7 8]
     == [4 0 5 1 6 2 7 3 8]

So that (for a trivial case, which I know is already sorted)

     >> riffle sort [0 0 0 1 1 1 2 2 2 3 3 3]
     == [2 0 2 0 2 0 3 1 3 1 3 1]

adjacent elements are now different.  Finally, for your example:

   example: [[4 1 7 "nicsrg" 5 6 3 8 2 9] [3 1 2 "sicrtg" 9 8 4 7 6 5]
             [9 3 7 "iscgtn" 6 2 8 5 4 1] [1 3 8 "inctrg" 5 2 9 4 6 7]
             [9 6 5 "ngsirt" 8 2 1 3 7 4] [3 7 8 "igtnrs" 4 1 2 9 5 6]
             [6 4 8 "gnrsit" 2 3 9 1 5 7] [9 5 4 "sntgir" 6 7 8 2 1 3]
             [6 7 4 "gstinr" 1 5 2 3 9 8] [7 9 2 "sgcnit" 5 1 8 3 6 4]]
   example: riffle sort/compare example func [a b][a/4/3 < b/4/3]
   foreach item example [print mold item]

which gives:

   [6 4 8 "gnrsit" 2 3 9 1 5 7]
   [1 3 8 "inctrg" 5 2 9 4 6 7]
   [9 6 5 "ngsirt" 8 2 1 3 7 4]
   [3 1 2 "sicrtg" 9 8 4 7 6 5]
   [3 7 8 "igtnrs" 4 1 2 9 5 6]
   [4 1 7 "nicsrg" 5 6 3 8 2 9]
   [6 7 4 "gstinr" 1 5 2 3 9 8]
   [7 9 2 "sgcnit" 5 1 8 3 6 4]
   [9 5 4 "sntgir" 6 7 8 2 1 3]
   [9 3 7 "iscgtn" 6 2 8 5 4 1]

where adjacent (sub-)blocks differ in the third character of the
fourth element.

-jn-

-- 
To unsubscribe from this list, just send an email to
[EMAIL PROTECTED] with unsubscribe as the subject.

Reply via email to