Problem 55280. Count estrangements
Recently I made a puzzle for my wife that included a cryptogram, which involves an arrangement of the letters A through Z. I used MATLAB (of course) to permute the letters, but in the first arrangement, H was coded as H. I tried again until MATLAB gave me a derangement, a permutation such that none of the letters was in its original position. I later noticed that the letter I was coded as S and S was coded as I. Though I used that permutation, I really wanted one in which no two elements are simply swapped. I called such a permutation an estrangement, and although I later learned of a more technical and mathematical description, I will keep my name.
Write a function to count estrangements—i.e., the permutations of elements in a 1xn vector such that (1) no element is in its original position and (2) no two elements are simply swapped. For example, if the vector is [1 2 3 4], then [3 2 4 1] and [4 1 3 2] would not be allowed under condition 1 and [4 3 2 1] and [2 1 4 3] would not be allowed under condition 2. Return the count as a string.
Solution Stats
Problem Comments
-
4 Comments
Show
1 older comment
William
on 20 Apr 2023
Okay, I have now submitted a correct (I think!) solution, but I think test #14 has an error.
Dyuman Joshi
on 22 Apr 2023
William, I have updated the test suit to include the assert() command now and rescored the solutions.
And yes, the test case #14 was incorrect and I have commented it out for now. I'm leaving it to Chris to update that particular test case.
ChrisR
on 24 Apr 2023
I don't know how I missed the asserts, but in my test file on my computer, they are missing. Thanks, Dyuman, for fixing that problem.
I also fixed Test 14.
Solution Comments
Show commentsProblem Recent Solvers7
Suggested Problems
-
Swap the first and last columns
21011 Solvers
-
1965 Solvers
-
Set some matrix elements to zero
557 Solvers
-
Find out missing number from a vector of 9 elements
302 Solvers
-
330 Solvers
More from this Author291
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!