Skip to main content
Commonmark migration
Source Link

Brachylog, 9 bytes

⟦kpiᶠ≠ᵐhᵐ 

Try it online!

This is a generator that outputs one derangement of [0, …, n-1] given n.

If we wrap it in a ᶠ - findall metapredicate, we get all possible generations of derangements by the generator.

###Explanation

Explanation

⟦ The range [0, …, Input] k Remove the last element p Take a permutation of the range [0, …, Input - 1] iᶠ Take all pair of Element-index: [[Elem0, 0],…,[ElemN-1, N-1]] ≠ᵐ Each pair must contain different values hᵐ The output is the head of each pair 

Brachylog, 9 bytes

⟦kpiᶠ≠ᵐhᵐ 

Try it online!

This is a generator that outputs one derangement of [0, …, n-1] given n.

If we wrap it in a ᶠ - findall metapredicate, we get all possible generations of derangements by the generator.

###Explanation

⟦ The range [0, …, Input] k Remove the last element p Take a permutation of the range [0, …, Input - 1] iᶠ Take all pair of Element-index: [[Elem0, 0],…,[ElemN-1, N-1]] ≠ᵐ Each pair must contain different values hᵐ The output is the head of each pair 

Brachylog, 9 bytes

⟦kpiᶠ≠ᵐhᵐ 

Try it online!

This is a generator that outputs one derangement of [0, …, n-1] given n.

If we wrap it in a ᶠ - findall metapredicate, we get all possible generations of derangements by the generator.

Explanation

⟦ The range [0, …, Input] k Remove the last element p Take a permutation of the range [0, …, Input - 1] iᶠ Take all pair of Element-index: [[Elem0, 0],…,[ElemN-1, N-1]] ≠ᵐ Each pair must contain different values hᵐ The output is the head of each pair 
Source Link
Fatalize
  • 39.6k
  • 5
  • 73
  • 165

Brachylog, 9 bytes

⟦kpiᶠ≠ᵐhᵐ 

Try it online!

This is a generator that outputs one derangement of [0, …, n-1] given n.

If we wrap it in a ᶠ - findall metapredicate, we get all possible generations of derangements by the generator.

###Explanation

⟦ The range [0, …, Input] k Remove the last element p Take a permutation of the range [0, …, Input - 1] iᶠ Take all pair of Element-index: [[Elem0, 0],…,[ElemN-1, N-1]] ≠ᵐ Each pair must contain different values hᵐ The output is the head of each pair