Last active
September 12, 2017 17:24
-
-
Save kmorin/7fa0e22c867e376222f5201473b1b797 to your computer and use it in GitHub Desktop.
retrive int[] of all grays code combinations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private IEnumerable<int[]> getGrays(int n) { | |
//if n is > 24 memory fails | |
if (n <= 0 || n > 24) { return null; } | |
var ar = new List<string> { "0", "1" }; | |
for (var i = 2; i < 1 << n; i = i << 1) { | |
for (var j = i - 1; j >= 0; j--) { ar.Add(ar[j]); } | |
for (var j = 0; j < i; j++) { ar[j] = $"0{ar[j]}"; } | |
for (var j = i; j < 2 * i; j++) { ar[j] = $"1{ar[j]}"; } | |
} | |
var returnAr = new List<int[]>(); | |
foreach (var s in ar) { | |
var chars = s.ToCharArray(); | |
var tempIntArray = new int[chars.Length]; | |
for (var i = 0; i < chars.Length; i++) { | |
var tempInt = int.Parse(chars[i].ToString()); | |
tempIntArray[i] = tempInt; | |
} | |
returnAr.Add(tempIntArray); | |
} | |
return returnAr; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment