PY-96

Build and Check a Seeded Permutation

  • Medium
  • Seeded Permutations
  • Python

Task

Write build_seeded_permutation(ids, values, generator). The ids input is a non-empty list of unique, non-empty strings. values is a two-dimensional, finite numeric NumPy array. Its first dimension must have the same length as ids, and it must have at least one column. Row i in values belongs to ids[i].

generator is one passed np.random.Generator. Call generator.permutation(len(ids)) exactly once to create a permutation of the source positions. Do not create a generator, set a global seed, make another random draw, or use a different random source. The returned position array must contain every source position exactly once.

Apply that one position array to both aligned inputs. Do not sort either input and do not change either input in place. Return a dictionary with exactly these keys:

permutation, shuffled_ids, shuffled_values, audit

permutation is an independent one-dimensional integer NumPy array. The ith item in shuffled_ids must be ids[permutation[i]], and row i in shuffled_values must be values[permutation[i]]. Return shuffled_ids as a tuple and shuffled_values as an independent array with the same shape and dtype as values.

audit must be a new dictionary with exactly these keys:

size, in_range, unique, covers_all, aligned

size is the number of source records. The remaining values are booleans. They must report, respectively, that every position is in range, no position is repeated, every source position is covered, and both shuffled outputs still agree with the same position plan.

Example

The exact order is determined by the supplied generator. Repeating the call with a fresh generator made with the same seed produces the same permutation, IDs, and rows. The source list and array remain unchanged.

Your implementation

You may import NumPy as np. Do not print or ask for input.