Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Add complex numbers exercise - Part I #378

Merged
merged 13 commits into from
Aug 30, 2017
11 changes: 11 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -995,6 +995,17 @@
"Algorithms"
]
},
{
"uuid": "0ed2de0f-70da-4f04-834e-01bfb0aa48d3",
"slug": "complex-numbers",
"core": false,
"unlocked_by": "leap",
"difficulty": 6,
"topics": [
"Tuples",
"Mathematics"
]
},
{
"uuid": "d03a9508-336a-4425-8f47-f04317d1ba15",
"slug": "poker",
Expand Down
51 changes: 51 additions & 0 deletions exercises/complex-numbers/ComplexNumbers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System;

public struct ComplexNumber
{
public int Real { get; set; }

public int Imaginary { get; set; }
}

public static class ComplexNumbers
{
public static ComplexNumber Mul(ComplexNumber z1, ComplexNumber z2)
{
throw new NotImplementedException("You need to implement this function.");
}

public static ComplexNumber Add(ComplexNumber z1, ComplexNumber z2)
{
throw new NotImplementedException("You need to implement this function.");
}

public static ComplexNumber Sub(ComplexNumber z1, ComplexNumber z2)
{
throw new NotImplementedException("You need to implement this function.");
}

public static ComplexNumber Div(ComplexNumber z1, ComplexNumber z2)
{
throw new NotImplementedException("You need to implement this function.");
}

public static int Abs(ComplexNumber input)
{
throw new NotImplementedException("You need to implement this function.");
}

public static ComplexNumber Conjugate(ComplexNumber input)
{
throw new NotImplementedException("You need to implement this function.");
}

public static int Real(ComplexNumber input)
{
throw new NotImplementedException("You need to implement this function.");
}

public static int Imaginary(ComplexNumber input)
{
throw new NotImplementedException("You need to implement this function.");
}
}
18 changes: 18 additions & 0 deletions exercises/complex-numbers/ComplexNumbers.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp1.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Remove="Example.cs" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0" />
<PackageReference Include="xunit" Version="2.2.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />
</ItemGroup>

</Project>
224 changes: 224 additions & 0 deletions exercises/complex-numbers/ComplexNumbersTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
// This file was auto-generated based on version 1.0.0 of the canonical data.

using Xunit;

public class ComplexNumbersTest
{
[Fact]
public void Imaginary_unit()
{
var z1 = new ComplexNumber { Real = 0, Imaginary = 1 };
var z2 = new ComplexNumber { Real = 0, Imaginary = 1 };
var expected = new ComplexNumber { Real = -1, Imaginary = 0 };
Assert.Equal(expected, ComplexNumbers.Mul(z1, z2));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was wondering of the static class approach makes sense. Shouldn't we be using instance methods?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ErikSchierboom I'm fine with that approach which will end up with using having pretty much the same solution as they do in the Java track:
https://github.com/exercism/java/blob/master/exercises/complex-numbers/src/example/java/ComplexNumber.java

The getReal and getImaginary methods are trivially to implement, but that's definitely not the main complexity of the question.

Another thing is once I implement part II, I'll have to convert the integers to be double to represent non-whole numbers with pi and e.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good points! Good luck with pt. II

}

[Fact(Skip = "Remove to run test")]
public void Add_purely_real_numbers()
{
var z1 = new ComplexNumber { Real = 1, Imaginary = 0 };
var z2 = new ComplexNumber { Real = 2, Imaginary = 0 };
var expected = new ComplexNumber { Real = 3, Imaginary = 0 };
Assert.Equal(expected, ComplexNumbers.Add(z1, z2));
}

[Fact(Skip = "Remove to run test")]
public void Add_purely_imaginary_numbers()
{
var z1 = new ComplexNumber { Real = 0, Imaginary = 1 };
var z2 = new ComplexNumber { Real = 0, Imaginary = 2 };
var expected = new ComplexNumber { Real = 0, Imaginary = 3 };
Assert.Equal(expected, ComplexNumbers.Add(z1, z2));
}

[Fact(Skip = "Remove to run test")]
public void Add_numbers_with_real_and_imaginary_part()
{
var z1 = new ComplexNumber { Real = 1, Imaginary = 2 };
var z2 = new ComplexNumber { Real = 3, Imaginary = 4 };
var expected = new ComplexNumber { Real = 4, Imaginary = 6 };
Assert.Equal(expected, ComplexNumbers.Add(z1, z2));
}

[Fact(Skip = "Remove to run test")]
public void Subtract_purely_real_numbers()
{
var z1 = new ComplexNumber { Real = 1, Imaginary = 0 };
var z2 = new ComplexNumber { Real = 2, Imaginary = 0 };
var expected = new ComplexNumber { Real = -1, Imaginary = 0 };
Assert.Equal(expected, ComplexNumbers.Sub(z1, z2));
}

[Fact(Skip = "Remove to run test")]
public void Subtract_purely_imaginary_numbers()
{
var z1 = new ComplexNumber { Real = 0, Imaginary = 1 };
var z2 = new ComplexNumber { Real = 0, Imaginary = 2 };
var expected = new ComplexNumber { Real = 0, Imaginary = -1 };
Assert.Equal(expected, ComplexNumbers.Sub(z1, z2));
}

[Fact(Skip = "Remove to run test")]
public void Subtract_numbers_with_real_and_imaginary_part()
{
var z1 = new ComplexNumber { Real = 1, Imaginary = 2 };
var z2 = new ComplexNumber { Real = 3, Imaginary = 4 };
var expected = new ComplexNumber { Real = -2, Imaginary = -2 };
Assert.Equal(expected, ComplexNumbers.Sub(z1, z2));
}

[Fact(Skip = "Remove to run test")]
public void Multiply_purely_real_numbers()
{
var z1 = new ComplexNumber { Real = 1, Imaginary = 0 };
var z2 = new ComplexNumber { Real = 2, Imaginary = 0 };
var expected = new ComplexNumber { Real = 2, Imaginary = 0 };
Assert.Equal(expected, ComplexNumbers.Mul(z1, z2));
}

[Fact(Skip = "Remove to run test")]
public void Multiply_purely_imaginary_numbers()
{
var z1 = new ComplexNumber { Real = 0, Imaginary = 1 };
var z2 = new ComplexNumber { Real = 0, Imaginary = 2 };
var expected = new ComplexNumber { Real = -2, Imaginary = 0 };
Assert.Equal(expected, ComplexNumbers.Mul(z1, z2));
}

[Fact(Skip = "Remove to run test")]
public void Multiply_numbers_with_real_and_imaginary_part()
{
var z1 = new ComplexNumber { Real = 1, Imaginary = 2 };
var z2 = new ComplexNumber { Real = 3, Imaginary = 4 };
var expected = new ComplexNumber { Real = -5, Imaginary = 10 };
Assert.Equal(expected, ComplexNumbers.Mul(z1, z2));
}

[Fact(Skip = "Remove to run test")]
public void Divide_purely_real_numbers()
{
var z1 = new ComplexNumber { Real = 1, Imaginary = 0 };
var z2 = new ComplexNumber { Real = 2, Imaginary = 0 };
var expected = new ComplexNumber { Real = 0, Imaginary = 0 };
Assert.Equal(expected, ComplexNumbers.Div(z1, z2));
}

[Fact(Skip = "Remove to run test")]
public void Divide_purely_imaginary_numbers()
{
var z1 = new ComplexNumber { Real = 0, Imaginary = 1 };
var z2 = new ComplexNumber { Real = 0, Imaginary = 2 };
var expected = new ComplexNumber { Real = 0, Imaginary = 0 };
Assert.Equal(expected, ComplexNumbers.Div(z1, z2));
}

[Fact(Skip = "Remove to run test")]
public void Divide_numbers_with_real_and_imaginary_part()
{
var z1 = new ComplexNumber { Real = 1, Imaginary = 2 };
var z2 = new ComplexNumber { Real = 3, Imaginary = 4 };
var expected = new ComplexNumber { Real = 0, Imaginary = 0 };
Assert.Equal(expected, ComplexNumbers.Div(z1, z2));
}

[Fact(Skip = "Remove to run test")]
public void Absolute_value_of_a_positive_purely_real_number()
{
var input = new ComplexNumber { Real = 5, Imaginary = 0 };
Assert.Equal(5, ComplexNumbers.Abs(input));
}

[Fact(Skip = "Remove to run test")]
public void Absolute_value_of_a_negative_purely_real_number()
{
var input = new ComplexNumber { Real = -5, Imaginary = 0 };
Assert.Equal(5, ComplexNumbers.Abs(input));
}

[Fact(Skip = "Remove to run test")]
public void Absolute_value_of_a_purely_imaginary_number_with_positive_imaginary_part()
{
var input = new ComplexNumber { Real = 0, Imaginary = 5 };
Assert.Equal(5, ComplexNumbers.Abs(input));
}

[Fact(Skip = "Remove to run test")]
public void Absolute_value_of_a_purely_imaginary_number_with_negative_imaginary_part()
{
var input = new ComplexNumber { Real = 0, Imaginary = -5 };
Assert.Equal(5, ComplexNumbers.Abs(input));
}

[Fact(Skip = "Remove to run test")]
public void Absolute_value_of_a_number_with_real_and_imaginary_part()
{
var input = new ComplexNumber { Real = 3, Imaginary = 4 };
Assert.Equal(5, ComplexNumbers.Abs(input));
}

[Fact(Skip = "Remove to run test")]
public void Conjugate_a_purely_real_number()
{
var input = new ComplexNumber { Real = 5, Imaginary = 0 };
var expected = new ComplexNumber { Real = 5, Imaginary = 0 };
Assert.Equal(expected, ComplexNumbers.Conjugate(input));
}

[Fact(Skip = "Remove to run test")]
public void Conjugate_a_purely_imaginary_number()
{
var input = new ComplexNumber { Real = 0, Imaginary = 5 };
var expected = new ComplexNumber { Real = 0, Imaginary = -5 };
Assert.Equal(expected, ComplexNumbers.Conjugate(input));
}

[Fact(Skip = "Remove to run test")]
public void Conjugate_a_number_with_real_and_imaginary_part()
{
var input = new ComplexNumber { Real = 1, Imaginary = 1 };
var expected = new ComplexNumber { Real = 1, Imaginary = -1 };
Assert.Equal(expected, ComplexNumbers.Conjugate(input));
}

[Fact(Skip = "Remove to run test")]
public void Real_part_of_a_purely_real_number()
{
var input = new ComplexNumber { Real = 1, Imaginary = 0 };
Assert.Equal(1, ComplexNumbers.Real(input));
}

[Fact(Skip = "Remove to run test")]
public void Real_part_of_a_purely_imaginary_number()
{
var input = new ComplexNumber { Real = 0, Imaginary = 1 };
Assert.Equal(0, ComplexNumbers.Real(input));
}

[Fact(Skip = "Remove to run test")]
public void Real_part_of_a_number_with_real_and_imaginary_part()
{
var input = new ComplexNumber { Real = 1, Imaginary = 2 };
Assert.Equal(1, ComplexNumbers.Real(input));
}

[Fact(Skip = "Remove to run test")]
public void Imaginary_part_of_a_purely_real_number()
{
var input = new ComplexNumber { Real = 1, Imaginary = 0 };
Assert.Equal(0, ComplexNumbers.Imaginary(input));
}

[Fact(Skip = "Remove to run test")]
public void Imaginary_part_of_a_purely_imaginary_number()
{
var input = new ComplexNumber { Real = 0, Imaginary = 1 };
Assert.Equal(1, ComplexNumbers.Imaginary(input));
}

[Fact(Skip = "Remove to run test")]
public void Imaginary_part_of_a_number_with_real_and_imaginary_part()
{
var input = new ComplexNumber { Real = 1, Imaginary = 2 };
Assert.Equal(2, ComplexNumbers.Imaginary(input));
}
}
75 changes: 75 additions & 0 deletions exercises/complex-numbers/Example.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using System;

public struct ComplexNumber
{
public int Real { get; set; }

public int Imaginary { get; set; }
}

public static class ComplexNumbers
{
public static ComplexNumber Mul(ComplexNumber z1, ComplexNumber z2)
{
return new ComplexNumber
{
Real = z1.Real * z2.Real - z1.Imaginary * z2.Imaginary,
Imaginary = z1.Imaginary * z2.Real + z1.Real * z2.Imaginary
};
}

public static ComplexNumber Add(ComplexNumber z1, ComplexNumber z2)
{
return new ComplexNumber
{
Real = z1.Real + z2.Real,
Imaginary = z1.Imaginary + z2.Imaginary
};
}

public static ComplexNumber Sub(ComplexNumber z1, ComplexNumber z2)
{
return new ComplexNumber
{
Real = z1.Real - z2.Real,
Imaginary = z1.Imaginary - z2.Imaginary
};
}

public static ComplexNumber Div(ComplexNumber z1, ComplexNumber z2)
{
var denominator = z2.Real * z2.Real + z2.Imaginary * z2.Imaginary;
var real = (z1.Real * z2.Real + z1.Imaginary * z2.Imaginary) / denominator;
var imaginary = (z1.Imaginary * z2.Real - z1.Real * z1.Real * z2.Imaginary) / denominator;

return new ComplexNumber
{
Real = real,
Imaginary = imaginary
};
}

public static int Abs(ComplexNumber input)
{
return (int)Math.Sqrt(input.Real * input.Real + input.Imaginary * input.Imaginary);
}

public static ComplexNumber Conjugate(ComplexNumber input)
{
return new ComplexNumber
{
Real = input.Real,
Imaginary = -1 * input.Imaginary
};
}

public static int Real(ComplexNumber input)
{
return input.Real;
}

public static int Imaginary(ComplexNumber input)
{
return input.Imaginary;
}
}
Loading