Need help
public static string CaesarTower(int n)
{
if (n < 1)
{
throw new ArgumentException();
}
int to;
string res = $"Moves for {n} disks\n";
int total = (int)Basix.MyPow(2, n) - 1;
for (int i = 1; i <= total ; i++)
{
if (n%2==1)
{
res += (i & i - 1) % 3 + 1 + " -> " + (((i | i - 1) + 1) % 3 + 1);
}
else
{
...
}
if (i != total)
res += "\n";
}
return res;
public static string CaesarTower(int n)
{
if (n < 1)
{
throw new ArgumentException();
}
int to;
string res = $"Moves for {n} disks\n";
int total = (int)Basix.MyPow(2, n) - 1;
for (int i = 1; i <= total ; i++)
{
if (n%2==1)
{
res += (i & i - 1) % 3 + 1 + " -> " + (((i | i - 1) + 1) % 3 + 1);
}
else
{
...
}
if (i != total)
res += "\n";
}
return res;
1 Reply
it's tower of hanoi in iterative
it's my tests :
[Theory]
[InlineData(1,"Moves for 1 disks\n1 -> 3")]
[InlineData(2, "Moves for 2 disks\n1 -> 2\n1 -> 3\n2 -> 3")]
[InlineData(3,"Moves for 3 disks\n1 -> 3\n1 -> 2\n3 -> 2\n1 -> 3\n2 -> 1\n2 -> 3\n1 -> 3")]
[InlineData(4, "Moves for 4 disks\n1 -> 2\n1 -> 3\n2 -> 3\n1 -> 2\n3 -> 1\n3 -> 2\n1 -> 2\n1 -> 3\n2 -> 3\n2 -> 1\n3 -> 1\n2 -> 3\n1 -> 2\n1 -> 3\n2 -> 3")]
[InlineData(5, "Moves for 5 disks\n1 -> 3\n1 -> 2\n3 -> 2\n1 -> 3\n2 -> 1\n2 -> 3\n1 -> 3\n1 -> 2\n3 -> 2\n3 -> 1\n2 -> 1\n3 -> 2\n1 -> 3\n1 -> 2\n3 -> 2\n1 -> 3\n2 -> 1\n2 -> 3\n1 -> 3\n2 -> 1\n3 -> 2\n3 -> 1\n2 -> 1\n2 -> 3\n1 -> 3\n1 -> 2\n3 -> 2\n1 -> 3\n2 -> 1\n2 -> 3\n1 -> 3")]
public void CaesarTowerTest(int n,string expected)
{
Assert.Equal(expected, Circus.CaesarTower(n));
}
[Theory]
[InlineData(1,"Moves for 1 disks\n1 -> 3")]
[InlineData(2, "Moves for 2 disks\n1 -> 2\n1 -> 3\n2 -> 3")]
[InlineData(3,"Moves for 3 disks\n1 -> 3\n1 -> 2\n3 -> 2\n1 -> 3\n2 -> 1\n2 -> 3\n1 -> 3")]
[InlineData(4, "Moves for 4 disks\n1 -> 2\n1 -> 3\n2 -> 3\n1 -> 2\n3 -> 1\n3 -> 2\n1 -> 2\n1 -> 3\n2 -> 3\n2 -> 1\n3 -> 1\n2 -> 3\n1 -> 2\n1 -> 3\n2 -> 3")]
[InlineData(5, "Moves for 5 disks\n1 -> 3\n1 -> 2\n3 -> 2\n1 -> 3\n2 -> 1\n2 -> 3\n1 -> 3\n1 -> 2\n3 -> 2\n3 -> 1\n2 -> 1\n3 -> 2\n1 -> 3\n1 -> 2\n3 -> 2\n1 -> 3\n2 -> 1\n2 -> 3\n1 -> 3\n2 -> 1\n3 -> 2\n3 -> 1\n2 -> 1\n2 -> 3\n1 -> 3\n1 -> 2\n3 -> 2\n1 -> 3\n2 -> 1\n2 -> 3\n1 -> 3")]
public void CaesarTowerTest(int n,string expected)
{
Assert.Equal(expected, Circus.CaesarTower(n));
}