-
-
Notifications
You must be signed in to change notification settings - Fork 358
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
Changes from 2 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
e45f460
Add complex numbers exercise - Part I
b1870b1
Update exercise to use ComplexNumber class
e500935
Change to class with instance methods
d11c0ad
Merge remote-tracking branch 'origin/master' into complex-numbers
a9e7e70
Fix build error after merge
fd4f499
Add exercise to solution
8f6c29e
Convert to double
e571c9a
Fix processing doubles
e04edc8
Add generation for Exp function
2a52e92
Add implementation for exponents
5c3b925
Keep math symbols in exercises
1449d1b
Add custom assert for comparing the complex number class
d1a1757
Re-order methods to reflect implementation change
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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."); | ||
} | ||
} |
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
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> |
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
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)); | ||
} | ||
|
||
[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)); | ||
} | ||
} |
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
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; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
andgetImaginary
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
ande
.There was a problem hiding this comment.
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