-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Sync recent problem-spec updates (#369)
* sync docs * sync metadata * sync tests bob * sync tests pig-latin
- Loading branch information
Showing
25 changed files
with
270 additions
and
118 deletions.
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
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
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
107 changes: 89 additions & 18 deletions
107
exercises/practice/complex-numbers/.docs/instructions.md
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 |
---|---|---|
@@ -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 |
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 |
---|---|---|
@@ -1,21 +1,21 @@ | ||
# Instructions | ||
|
||
Given students' names along with the grade that they are in, create a roster for the school. | ||
Given students' names along with the grade they are in, create a roster for the school. | ||
|
||
In the end, you should be able to: | ||
|
||
- Add a student's name to the roster for a grade | ||
- Add a student's name to the roster for a grade: | ||
- "Add Jim to grade 2." | ||
- "OK." | ||
- Get a list of all students enrolled in a grade | ||
- Get a list of all students enrolled in a grade: | ||
- "Which students are in grade 2?" | ||
- "We've only got Jim just now." | ||
- "We've only got Jim right now." | ||
- Get a sorted list of all students in all grades. | ||
Grades should sort as 1, 2, 3, etc., and students within a grade should be sorted alphabetically by name. | ||
- "Who all is enrolled in school right now?" | ||
Grades should be sorted as 1, 2, 3, etc., and students within a grade should be sorted alphabetically by name. | ||
- "Who is enrolled in school right now?" | ||
- "Let me think. | ||
We have Anna, Barb, and Charlie in grade 1, Alex, Peter, and Zoe in grade 2 and Jim in grade 5. | ||
So the answer is: Anna, Barb, Charlie, Alex, Peter, Zoe and Jim" | ||
We have Anna, Barb, and Charlie in grade 1, Alex, Peter, and Zoe in grade 2, and Jim in grade 5. | ||
So the answer is: Anna, Barb, Charlie, Alex, Peter, Zoe, and Jim." | ||
|
||
Note that all our students only have one name (It's a small town, what do you want?) and each student cannot be added more than once to a grade or the roster. | ||
In fact, when a test attempts to add the same student more than once, your implementation should indicate that this is incorrect. | ||
Note that all our students only have one name (it's a small town, what do you want?), and each student cannot be added more than once to a grade or the roster. | ||
If a test attempts to add the same student more than once, your implementation should indicate that this is incorrect. |
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,12 @@ | ||
# Introduction | ||
|
||
Your body is made up of cells that contain DNA. | ||
Those cells regularly wear out and need replacing, which they achieve by dividing into daughter cells. | ||
In fact, the average human body experiences about 10 quadrillion cell divisions in a lifetime! | ||
|
||
When cells divide, their DNA replicates too. | ||
Sometimes during this process mistakes happen and single pieces of DNA get encoded with the incorrect information. | ||
If we compare two strands of DNA and count the differences between them, we can see how many mistakes occurred. | ||
This is known as the "Hamming distance". | ||
|
||
The Hamming distance is useful in many areas of science, not just biology, so it's a nice phrase to be familiar with :) |
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
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 |
---|---|---|
@@ -1,8 +1,10 @@ | ||
# Introduction | ||
|
||
Bob is a thief. | ||
After months of careful planning, he finally manages to crack the security systems of a fancy store. | ||
Lhakpa is a [Sherpa][sherpa] mountain guide and porter. | ||
After months of careful planning, the expedition Lhakpa works for is about to leave. | ||
She will be paid the value she carried to the base camp. | ||
|
||
In front of him are many items, each with a value and weight. | ||
Bob would gladly take all of the items, but his knapsack can only hold so much weight. | ||
Bob has to carefully consider which items to take so that the total value of his selection is maximized. | ||
In front of her are many items, each with a value and weight. | ||
Lhakpa would gladly take all of the items, but her knapsack can only hold so much weight. | ||
|
||
[sherpa]: https://en.wikipedia.org/wiki/Sherpa_people#Mountaineering |
Oops, something went wrong.