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
Solution Comments
Show comments
Loading...
Problem Recent Solvers9
Suggested Problems
-
3429 Solvers
-
Project Euler: Problem 10, Sum of Primes
2088 Solvers
-
365 Solvers
-
16352 Solvers
-
Polar Form Complex Number Entry
56 Solvers
More from this Author321
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
Sorry, but I submitted a phoney solution that passes all of the tests. Since there are no assert() functions, just about anything will pass!
Okay, I have now submitted a correct (I think!) solution, but I think test #14 has an error.
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.
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.