-
-
Notifications
You must be signed in to change notification settings - Fork 159
[Swift 6]: Update Exercises batch 22 #816
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
Open
meatball133
wants to merge
9
commits into
exercism:main
Choose a base branch
from
meatball133:update-exercise-batch-22
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
43d8ff9
Update Swift tools version to 6.0 and add new Position struct for sad…
meatball133 93a1df0
Format files
meatball133 74f1025
Merge branch 'exercism:main' into update-exercise-batch-22
meatball133 e2306ac
Re generate test file
meatball133 31c31b7
Fix template of poker
meatball133 9924cb8
Re generate saddle points test
meatball133 bda390a
Bump numerics version
meatball133 8a9724d
Update instructions.append.md
meatball133 c841958
Update instructions.append.md
meatball133 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
11 changes: 11 additions & 0 deletions
11
exercises/practice/complex-numbers/.docs/instructions.append.md
This file contains hidden or 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,11 @@ | ||
# Append | ||
|
||
You will have to implement your own equality operator for the `ComplexNumber` object. | ||
This will pose the challenge of comparing two floating point numbers. | ||
It might be useful to use the method `isApproximatelyEqual(to:absoluteTolerance:)` which can be found in the [Numerics][swift-numberics] library. | ||
A given tolerance of `0.00001` should be enough to pass the tests. | ||
The library is already imported in the project so it is just to import it in your file. | ||
|
||
You are also free to implement your own method to compare the two complex numbers. | ||
|
||
[swift-numberics]: https://github.com/apple/swift-numerics |
107 changes: 89 additions & 18 deletions
107
exercises/practice/complex-numbers/.docs/instructions.md
This file contains hidden or 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 |
---|---|---|
@@ -1,29 +1,100 @@ | ||
# Instructions | ||
|
||
A complex number is a number in the form `a + b * i` where `a` and `b` are real and `i` satisfies `i^2 = -1`. | ||
A **complex number** is expressed in the form `z = a + b * i`, where: | ||
|
||
`a` is called the real part and `b` is called the imaginary part of `z`. | ||
The conjugate of the number `a + b * i` is the number `a - b * i`. | ||
The absolute value of a complex number `z = a + b * i` is a real number `|z| = sqrt(a^2 + b^2)`. The square of the absolute value `|z|^2` is the result of multiplication of `z` by its complex conjugate. | ||
- `a` is the **real part** (a real number), | ||
|
||
The sum/difference of two complex numbers involves adding/subtracting their real and imaginary parts separately: | ||
`(a + i * b) + (c + i * d) = (a + c) + (b + d) * i`, | ||
`(a + i * b) - (c + i * d) = (a - c) + (b - d) * i`. | ||
- `b` is the **imaginary part** (also a real number), and | ||
|
||
Multiplication result is by definition | ||
`(a + i * b) * (c + i * d) = (a * c - b * d) + (b * c + a * d) * i`. | ||
- `i` is the **imaginary unit** satisfying `i^2 = -1`. | ||
|
||
The reciprocal of a non-zero complex number is | ||
`1 / (a + i * b) = a/(a^2 + b^2) - b/(a^2 + b^2) * i`. | ||
## Operations on Complex Numbers | ||
|
||
Dividing a complex number `a + i * b` by another `c + i * d` gives: | ||
`(a + i * b) / (c + i * d) = (a * c + b * d)/(c^2 + d^2) + (b * c - a * d)/(c^2 + d^2) * i`. | ||
### Conjugate | ||
|
||
Raising e to a complex exponent can be expressed as `e^(a + i * b) = e^a * e^(i * b)`, the last term of which is given by Euler's formula `e^(i * b) = cos(b) + i * sin(b)`. | ||
The conjugate of the complex number `z = a + b * i` is given by: | ||
|
||
Implement the following operations: | ||
```text | ||
zc = a - b * i | ||
``` | ||
|
||
- addition, subtraction, multiplication and division of two complex numbers, | ||
- conjugate, absolute value, exponent of a given complex number. | ||
### Absolute Value | ||
|
||
Assume the programming language you are using does not have an implementation of complex numbers. | ||
The absolute value (or modulus) of `z` is defined as: | ||
|
||
```text | ||
|z| = sqrt(a^2 + b^2) | ||
``` | ||
|
||
The square of the absolute value is computed as the product of `z` and its conjugate `zc`: | ||
|
||
```text | ||
|z|^2 = z * zc = a^2 + b^2 | ||
``` | ||
|
||
### Addition | ||
|
||
The sum of two complex numbers `z1 = a + b * i` and `z2 = c + d * i` is computed by adding their real and imaginary parts separately: | ||
|
||
```text | ||
z1 + z2 = (a + b * i) + (c + d * i) | ||
= (a + c) + (b + d) * i | ||
``` | ||
|
||
### Subtraction | ||
|
||
The difference of two complex numbers is obtained by subtracting their respective parts: | ||
|
||
```text | ||
z1 - z2 = (a + b * i) - (c + d * i) | ||
= (a - c) + (b - d) * i | ||
``` | ||
|
||
### Multiplication | ||
|
||
The product of two complex numbers is defined as: | ||
|
||
```text | ||
z1 * z2 = (a + b * i) * (c + d * i) | ||
= (a * c - b * d) + (b * c + a * d) * i | ||
``` | ||
|
||
### Reciprocal | ||
|
||
The reciprocal of a non-zero complex number is given by: | ||
|
||
```text | ||
1 / z = 1 / (a + b * i) | ||
= a / (a^2 + b^2) - b / (a^2 + b^2) * i | ||
``` | ||
|
||
### Division | ||
|
||
The division of one complex number by another is given by: | ||
|
||
```text | ||
z1 / z2 = z1 * (1 / z2) | ||
= (a + b * i) / (c + d * i) | ||
= (a * c + b * d) / (c^2 + d^2) + (b * c - a * d) / (c^2 + d^2) * i | ||
``` | ||
|
||
### Exponentiation | ||
|
||
Raising _e_ (the base of the natural logarithm) to a complex exponent can be expressed using Euler's formula: | ||
|
||
```text | ||
e^(a + b * i) = e^a * e^(b * i) | ||
= e^a * (cos(b) + i * sin(b)) | ||
``` | ||
|
||
## Implementation Requirements | ||
|
||
Given that you should not use built-in support for complex numbers, implement the following operations: | ||
|
||
- **addition** of two complex numbers | ||
- **subtraction** of two complex numbers | ||
- **multiplication** of two complex numbers | ||
- **division** of two complex numbers | ||
- **conjugate** of a complex number | ||
- **absolute value** of a complex number | ||
- **exponentiation** of _e_ (the base of the natural logarithm) to a complex number |
122 changes: 58 additions & 64 deletions
122
exercises/practice/complex-numbers/.meta/Sources/ComplexNumbers/ComplexNumbersExample.swift
This file contains hidden or 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 |
---|---|---|
@@ -1,66 +1,60 @@ | ||
import Foundation | ||
|
||
struct ComplexNumber { | ||
|
||
var realComponent: Double | ||
|
||
var imaginaryComponent: Double | ||
|
||
func getRealComponent() -> Double { | ||
|
||
return self.realComponent | ||
} | ||
|
||
func getImaginaryComponent() -> Double { | ||
|
||
return self.imaginaryComponent | ||
} | ||
|
||
func add(complexNumber: ComplexNumber) -> ComplexNumber { | ||
|
||
return ComplexNumber(realComponent: self.realComponent + complexNumber.realComponent, imaginaryComponent: self.imaginaryComponent + complexNumber.imaginaryComponent) | ||
|
||
} | ||
|
||
func subtract(complexNumber: ComplexNumber) -> ComplexNumber { | ||
|
||
return ComplexNumber(realComponent: self.realComponent - complexNumber.realComponent, imaginaryComponent: self.imaginaryComponent - complexNumber.imaginaryComponent) | ||
} | ||
|
||
func multiply(complexNumber: ComplexNumber) -> ComplexNumber { | ||
|
||
return ComplexNumber(realComponent: self.realComponent * complexNumber.realComponent - self.imaginaryComponent * complexNumber.imaginaryComponent, imaginaryComponent: self.imaginaryComponent * complexNumber.realComponent + self.realComponent * complexNumber.imaginaryComponent) | ||
} | ||
|
||
func divide(complexNumber: ComplexNumber) -> ComplexNumber { | ||
|
||
let amplitudeOfComplexNumber = (complexNumber.realComponent * complexNumber.realComponent) + (complexNumber.imaginaryComponent * complexNumber.imaginaryComponent) | ||
|
||
let realPartOfQuotient = (self.realComponent * complexNumber.realComponent + self.imaginaryComponent * complexNumber.imaginaryComponent) / amplitudeOfComplexNumber | ||
|
||
let imaginaryPartOfQuotient = (self.imaginaryComponent * complexNumber.realComponent - self.realComponent * self.realComponent * complexNumber.imaginaryComponent) / amplitudeOfComplexNumber | ||
|
||
return ComplexNumber(realComponent: realPartOfQuotient, imaginaryComponent: imaginaryPartOfQuotient) | ||
} | ||
|
||
func conjugate() -> ComplexNumber { | ||
|
||
return ComplexNumber(realComponent: self.realComponent, imaginaryComponent: (-1 * self.imaginaryComponent)) | ||
} | ||
|
||
func absolute() -> Double { | ||
|
||
return sqrt(pow(self.realComponent, 2.0) + pow(self.imaginaryComponent, 2.0)) | ||
} | ||
|
||
func exponent() -> ComplexNumber { | ||
|
||
let realPartOfResult = cos(self.imaginaryComponent) | ||
let imaginaryPartOfResult = sin(self.imaginaryComponent) | ||
let factor = exp(self.realComponent) | ||
|
||
return ComplexNumber(realComponent: realPartOfResult * factor, imaginaryComponent: imaginaryPartOfResult * factor) | ||
|
||
} | ||
|
||
import Numerics | ||
|
||
struct ComplexNumbers: Equatable { | ||
|
||
var real: Double | ||
var imaginary: Double | ||
|
||
init(realComponent: Double, imaginaryComponent: Double? = 0) { | ||
real = realComponent | ||
imaginary = imaginaryComponent ?? 0 | ||
} | ||
|
||
func add(complexNumber: ComplexNumbers) -> ComplexNumbers { | ||
ComplexNumbers( | ||
realComponent: real + complexNumber.real, | ||
imaginaryComponent: imaginary + complexNumber.imaginary) | ||
} | ||
|
||
func sub(complexNumber: ComplexNumbers) -> ComplexNumbers { | ||
ComplexNumbers( | ||
realComponent: real - complexNumber.real, | ||
imaginaryComponent: imaginary - complexNumber.imaginary) | ||
} | ||
|
||
func mul(complexNumber: ComplexNumbers) -> ComplexNumbers { | ||
ComplexNumbers( | ||
realComponent: real * complexNumber.real - imaginary * complexNumber.imaginary, | ||
imaginaryComponent: real * complexNumber.imaginary + imaginary * complexNumber.real) | ||
} | ||
|
||
func div(complexNumber: ComplexNumbers) -> ComplexNumbers { | ||
let denominator = | ||
complexNumber.real * complexNumber.real + complexNumber.imaginary * complexNumber.imaginary | ||
let realComponent = | ||
(real * complexNumber.real + imaginary * complexNumber.imaginary) / denominator | ||
let imaginaryComponent = | ||
(imaginary * complexNumber.real - real * complexNumber.imaginary) / denominator | ||
return ComplexNumbers(realComponent: realComponent, imaginaryComponent: imaginaryComponent) | ||
} | ||
|
||
func absolute() -> Double { | ||
sqrt(Double(real * real + imaginary * imaginary)) | ||
} | ||
|
||
func conjugate() -> ComplexNumbers { | ||
ComplexNumbers(realComponent: real, imaginaryComponent: -imaginary) | ||
} | ||
|
||
func exponent() -> ComplexNumbers { | ||
let expReal = exp(Double(real)) * cos(Double(imaginary)) | ||
let expImaginary = exp(Double(real)) * sin(Double(imaginary)) | ||
return ComplexNumbers(realComponent: expReal, imaginaryComponent: expImaginary) | ||
} | ||
|
||
static func == (lhs: ComplexNumbers, rhs: ComplexNumbers) -> Bool { | ||
lhs.real.isApproximatelyEqual(to: rhs.real, absoluteTolerance: 0.0001) | ||
&& lhs.imaginary.isApproximatelyEqual(to: rhs.imaginary, absoluteTolerance: 0.0001) | ||
} | ||
} |
This file contains hidden or 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 |
---|---|---|
@@ -1,6 +1,7 @@ | ||
{ | ||
"authors": [ | ||
"AlwynC" | ||
"AlwynC", | ||
"meatball133" | ||
], | ||
"contributors": [ | ||
"bhargavg", | ||
|
This file contains hidden or 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,72 @@ | ||
import Testing | ||
import Foundation | ||
|
||
@testable import {{exercise|camelCase}} | ||
|
||
let RUNALL = Bool(ProcessInfo.processInfo.environment["RUNALL", default: "false"]) ?? false | ||
|
||
@Suite struct {{exercise|camelCase}}Tests { | ||
{% outer: for case in cases %} | ||
{%- if case.cases %} | ||
{%- for subCases in case.cases %} | ||
{%- if subCases.cases %} | ||
{%- for subSubCases in subCases.cases %} | ||
@Test("{{subSubCases.description}}", .enabled(if: RUNALL)) | ||
func test{{subSubCases.description |camelCase }}() { | ||
let complexNumberOne = {{exercise|camelCase}}(realComponent: {{subSubCases.input.z1[0]}}, imaginaryComponent: {{subSubCases.input.z1[1]}}) | ||
let complexNumberTwo = {{exercise|camelCase}}(realComponent: {{subSubCases.input.z2[0]}}, imaginaryComponent: {{subSubCases.input.z2[1]}}) | ||
let result = complexNumberOne.{{subSubCases.property}}(complexNumber: complexNumberTwo) | ||
let expected = {{exercise|camelCase}}(realComponent: {{subSubCases.expected[0]}}, imaginaryComponent: {{subSubCases.expected[1]}}) | ||
#expect(expected == result) | ||
} | ||
{%- endfor %} | ||
{%- else %} | ||
{%- if forloop.outer.first and forloop.first %} | ||
@Test("{{subCases.description}}") | ||
{%- else %} | ||
@Test("{{subCases.description}}", .enabled(if: RUNALL)) | ||
{%- endif %} | ||
func test{{subCases.description |camelCase }}() { | ||
{%- if subCases.property == "real" or subCases.property == "imaginary" or subCases.property == "abs" or subCases.property == "conjugate" or subCases.property == "exp" %} | ||
let complexNumber = {{exercise|camelCase}}(realComponent: {{subCases.input.z[0] | complexNumber}}, imaginaryComponent: {{subCases.input.z[1] | complexNumber}}) | ||
{%- if subCases.property == "real" %} | ||
#expect(complexNumber.real == {{subCases.expected}}) | ||
{%- elif subCases.property == "imaginary" %} | ||
#expect(complexNumber.imaginary == {{subCases.expected}}) | ||
{%- elif subCases.property == "abs" %} | ||
#expect(complexNumber.absolute() == {{subCases.expected}}) | ||
{%- elif subCases.property == "conjugate" %} | ||
let expected = {{exercise|camelCase}}(realComponent: {{subCases.expected[0]}}, imaginaryComponent: {{subCases.expected[1]}}) | ||
#expect(complexNumber.conjugate() == expected) | ||
{%- elif subCases.property == "exp" %} | ||
let expected = {{exercise|camelCase}}(realComponent: {{subCases.expected[0] | complexNumber}}, imaginaryComponent: {{subCases.expected[1]}}) | ||
#expect(complexNumber.exponent() == expected) | ||
{%- elif subCases.property == "add" or subCases.property == "sub" or subCases.property == "mul" or subCases.property == "div" %} | ||
{%- endif %} | ||
{%- else %} | ||
{%- if subCases.input.z1[0] %} | ||
let complexNumberOne = {{exercise|camelCase}}(realComponent: {{subCases.input.z1[0]}}, imaginaryComponent: {{subCases.input.z1[1]}}) | ||
let complexNumberTwo = {{exercise|camelCase}}(realComponent: {{subCases.input.z2}}, imaginaryComponent: nil) | ||
{%- else %} | ||
let complexNumberOne = {{exercise|camelCase}}(realComponent: {{subCases.input.z1}}, imaginaryComponent: nil) | ||
let complexNumberTwo = {{exercise|camelCase}}(realComponent: {{subCases.input.z2[0]}}, imaginaryComponent: {{subCases.input.z2[1]}}) | ||
{%- endif %} | ||
let result = complexNumberOne.{{subCases.property}}(complexNumber: complexNumberTwo) | ||
let expected = {{exercise|camelCase}}(realComponent: {{subCases.expected[0]}}, imaginaryComponent: {{subCases.expected[1]}}) | ||
#expect(expected == result) | ||
{%- endif %} | ||
} | ||
{%- endif %} | ||
{% endfor -%} | ||
{%- else %} | ||
@Test("{{case.description}}", .enabled(if: RUNALL)) | ||
func test{{case.description |camelCase }}() { | ||
let complexNumberOne = {{exercise|camelCase}}(realComponent: {{case.input.z1[0]}}, imaginaryComponent: {{case.input.z1[1]}}) | ||
let complexNumberTwo = {{exercise|camelCase}}(realComponent: {{case.input.z2[0]}}, imaginaryComponent: {{case.input.z2[1]}}) | ||
let result = complexNumberOne.{{case.property}}(complexNumber: complexNumberTwo) | ||
let expected = {{exercise|camelCase}}(realComponent: {{case.expected[0]}}, imaginaryComponent: {{case.expected[1]}}) | ||
#expect(expected == result) | ||
} | ||
{%- endif %} | ||
{% endfor -%} | ||
} |
This file contains hidden or 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
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.
This line is confusing. Does it mean the library is imported but the student needs to reference it in their code?
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.
The library is included in the project config file. But you also have to import it the file you are working in.
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.