Crate derangements

Crate derangements 

Source
Expand description

§Derangements

derangements allows you to derange iterables: permutations where no element equals its index

§Examples

use std::collections::HashMap;
use itertools::{assert_equal, Itertools};
use derangements::{derangements, restricted_permutations_by_map_value};
assert_equal(derangements(vec![0usize, 1, 2].into_iter(), 3).sorted(), [[1, 2, 0], [2, 0, 1]]);

// The module also contains other variants of restricted permutations
// Exclude from value 0 from indices 0 and 1, and value 1 from indices 1 and 2.
let restrict = HashMap::from([(0, vec![0, 1]), (1, vec![1, 2])]);
assert_equal(restricted_permutations_by_map_value(vec![0, 1, 2, 3].into_iter(), 3, restrict).sorted(),
   [[1, 2, 0], [1, 2, 3], [1, 3, 0], [1, 3, 2], [2, 3, 0], [3, 2, 0]]);

Functions§

derangements
Derange k or all elements of an iterable.
derangements_range
Derange all elements of a range of 0 to n (non-inclusive).
derangements_range_fast
Derange all elements of a range of 0 to n (non-inclusive) with caching.
distinct_derangements
Derange k or all elements of an iterable without repetitions.
distinct_permutations
Permute k or all elements of an iterable without repetitions.
fast_permutations
Permute k or all elements of an iterable.
restricted_permutations
Permute k or all elements of an iterable while excluding based on an input restriction
restricted_permutations_by_map_index
Permute k or all elements of an iterable while excluding based on an input restriction
restricted_permutations_by_map_value
Permute k or all elements of an iterable while excluding based on an input restriction
restricted_permutations_by_self
Permute k or all elements of an iterable while excluding any results where one of the elements doesn’t change.