Permutation Square Roots
Description:
Overview
Given a permutation (a list representing an arrangement of for some ), find a square root of that permutation (a new arrangement that, when applied to itself, gives the original permutation.)
Explanation
In this kata, a permutation of length is a list containing each non-negative integer less than exactly once, in some order. For instance, is a permutation of length . 1
Each permutation can be thought of as a rearrangement of the list . It may also be thought of as a function, where the the inputs are the indices and the outputs are the values at those indices:
k 0 1 2 3 4 5 6 7
f(k) 1 7 6 3 2 4 5 0
Like any function, may be composed with another function, or with itself. The function will also represent a permutation of the same length:
k 0 1 2 3 4 5 6 7
f(k) 1 7 6 3 2 4 5 0
f(f(k)) 7 0 5 3 6 2 4 1
We may call the square of . It makes sense, then, to call a square root of .
Like real numbers, permutations may have multiple square roots, or no square roots. The only other square root of is , though some permutations may have many more square roots. On the other hand, the permutation has none. (To see this, note that the only permutations of length are and , both of which square to .)
Objective
Write a function get_square_root
that accepts a permutation as a list of integers for its input. If that permutation has one or more square roots as defined above, return any one of those permutations also in list form. Otherwise, return None
.
Example 1
has_square_root([7, 0, 5, 3, 6, 2, 4, 1])
# should return [1, 7, 6, 3, 2, 4, 5, 0] or [1, 7, 4, 3, 5, 6, 2, 0]
Example 2
has_square_root([1, 0])
# should return None
All inputs will be lists of length , where . Inputs will always be valid permutations, containing exclusively non-negative integers less than , and never containing duplicates.
- It is more standard in mathematics for permutations be rearrangements of , but zero-indexed permutations are easier to work with here. The actual objects being arranged do not fundamentally matter. There is a more useful notation for permutations called cycle notation, but this is not used in this kata.
Similar Kata:
Stats:
Created | Jan 25, 2025 |
Published | Jan 27, 2025 |
Warriors Trained | 75 |
Total Skips | 8 |
Total Code Submissions | 75 |
Total Times Completed | 18 |
Python Completions | 18 |
Total Stars | 8 |
% of votes with a positive feedback rating | 95% of 11 |
Total "Very Satisfied" Votes | 10 |
Total "Somewhat Satisfied" Votes | 1 |
Total "Not Satisfied" Votes | 0 |
Total Rank Assessments | 8 |
Average Assessed Rank | 4 kyu |
Highest Assessed Rank | 3 kyu |
Lowest Assessed Rank | 5 kyu |