Click here to Skip to main content
15,895,256 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The problem is purely mathematic because they are the dominant combinatorics and probability. It is possible to solve the problem without knowing programming, but it is much easier to solve it by programming.

In the following 40 byte group, there are 3 pairs of the same bytes:

248, 195, 38, 169, 202, 202, 133, 240, 232, 204, 142, 40, 86, 89, 215, 138, 15, 217, 123, 33, 183, 105, 239, 239, 229, 192, 226, 191, 23, 158, 12, 94, 222, 253, 6, 113, 146, 146, 183, 209.

A pair of bytes 169 202 is a pair of different bytes.
A pair of bytes 202 202 is a pair of the same bytes.

Bytes must be shuffled so that the pairs of the same bytes do not appear. The resulting bytes must be composed only of pairs of different bytes!

Example of resultant bytes (swap 2*n and 2*n+1 byte, n=1..19) :

248, 38, 195, 202, 169, 133, 202, 232, 240, 142, 204, 86, 40, 215, 89, 15, 138, 123, 217, 183, 33, 239, 105, 229, 239, 226, 192, 23, 191, 12, 158, 222, 94, 6, 253, 146, 113, 183, 146, 209.

The procedure must be reversible. Instead of 40 bytes (in case of example), the procedure should be applied to a group of 1500 bytes. In a group of 1500 bytes, most pairs are different bytes. There are also pairs with the same bytes, but there are no more than 10 and they do not know where they are in the group. Multiple consecutive same bytes do not exist. In a group of 1500 bytes, all bytes appeared at least once. If the algorithm has to use additional numbers to shuffle a group of 1500 bytes, then these numbers must be the same for each group. The algorithm does not have to be successful in every call. It is enough that from 8 consecutive different groups of 1500 bytes it succeeds to shuffle any two.

Any group of bytes from any file type can be converted into a group as described in the problem. A group of such 1500 bytes is of interest for compression and encryption.

The problem is that there is recombination of bytes - the same pairs are eliminated at the places where they were, but appear elsewhere. This problem occurs with larger groups of bytes. A 40-byte group will solve almost every well-known algorithm for shuffling numbers, but a group of 1500 bytes has not solved any one!

It is desirable to provide a solution in delphi 7 code or pseudocode.

The following procedure sufficiently well model a group of 1500 bytes:

Delphi
var
  b : array [1..1500] of byte;

procedure MakePseudorandomNumbersWithEqualPairs (sum : longint; max : word);
var
  i, lastb : longint;
  range : word;
begin
  Randomize;                    // Prepare a random number generator
  lastb := sum;                 // How many total bytes
  range := max;                 // Bytes from the range 0..max-1
  for i := 1 to lastb do        // They can be the same neighbors!      
    b[i] := Random (range);
end;

After the procedure call:

Delphi
MakePseudorandomNumbersWithEqualPairs (1500, 256);


array b contains bytes that model the given group.

What I have tried:

Delphi
const
  lastb = 1500;
var
  b, c : array [1..lastb] of byte;
  
procedure PerfectShuffle;      // Perfect shuffle
var
  i : integer;

begin
  if (lastb mod 2) = 0     // only if is a even number of bytes!
   then
    begin
      for i := 1 to (lastb div 2) do
        c[i*2-1] := b[i];
      for i := (lastb div 2)+1 to lastb do
        c[i*2-lastb] := b[i];

      for i := 1 to lastb do
        b[i] := c[i];
    end;


Input: b
Output: b
Posted
Updated 8-Apr-19 22:30pm
v2
Comments
OriginalGriff 9-Apr-19 1:58am    
And?
What does it do that you didn't expect, or not do that you did?
Where are you stuck?
What help do you need?

1 solution

I would try this way: for each found pair (starting at index i) choose a random number r and swap the item at index i with the item at (i+r) (with wrap around) unless such item is itself equal to the pair members. In such an infortunate event try with (i+r+1), (i+r+2) ..., until you find a suitable item.
As per reversibility requirement, take note of the occurred swaps and apply them in the reverse order.
 
Share this answer
 
Comments
Bulog 9-Apr-19 6:37am    
The idea is quite good. I myself tried with random indexes. But the problem is that it's forbidden to remember different extra numbers for each group separately. It is permissible to remember only the same additional numbers that apply to all groups of bytes that are being processed, and a different group of bytes enters each call. Pairs with the same bytes are in each following group at completely different locations and their exact number is unknown. It is not allowed for each group, for example, to remember locations where pairs with the same bytes are located. This random number r is allowed, but it should apply to each group, at each call procedure. It simply has space for Nx1500 bytes + a few control bytes (like r) and that's it. The procedure must be universal, valid for each group of 1500 bytes. Maybe a problem looks naïve, but it's a very difficult problem.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900