You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: exercises/concept/annalyns-infiltration/.docs/instructions.md
+33-16Lines changed: 33 additions & 16 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,26 +1,35 @@
1
1
# Instructions
2
2
3
-
In this exercise, you'll be implementing the quest logic for a new RPG game a friend is developing. The game's main character is Annalyn, a brave girl with a fierce and loyal pet dog. Unfortunately, disaster strikes, as her best friend was kidnapped while searching for berries in the forest. Annalyn will try to find and free her best friend, optionally taking her dog with her on this quest.
3
+
In this exercise, you'll implement the quest logic for a new RPG game that a friend is developing.
4
+
The game's main character is Annalyn, a brave girl with a fierce and loyal pet dog.
5
+
Unfortunately, disaster strikes: her best friend was kidnapped while searching for berries in the forest.
6
+
Annalyn will try to find and rescue her friend, optionally taking her dog along on the quest.
4
7
5
-
After some time spent following her best friend's trail, she finds the camp in which her best friend is imprisoned. It turns out there are two kidnappers: a mighty knight and a cunning archer.
8
+
After some time spent following the trail, Annalyn discovers the camp where her friend is imprisoned.
9
+
It turns out there are two kidnappers: a mighty knight and a cunning archer.
6
10
7
11
Having found the kidnappers, Annalyn considers which of the following actions she can engage in:
8
12
9
-
- Fast attack: a fast attack can be made if the knight is sleeping, as it takes time for him to get his armor on, so he will be vulnerable.
10
-
- Spy: the group can be spied upon if at least one of them is awake. Otherwise, spying is a waste of time.
11
-
- Signal prisoner: the prisoner can be signalled using bird sounds if the prisoner is awake and the archer is sleeping, as archers are trained in bird signaling, so they could intercept the message.
12
-
-_Free prisoner_: Annalyn can try sneaking into the camp to free the prisoner.
13
-
This is a risky thing to do and can only succeed in one of two ways:
14
-
- If Annalyn has her pet dog with her she can rescue the prisoner if the archer is asleep.
13
+
- Fast attack: a fast attack can be made if the knight is sleeping, as it takes time for him to put on his armor, leaving him vulnerable.
14
+
- Spy: the group can be spied upon if at least one of them is awake.
15
+
Otherwise, spying is a waste of time.
16
+
- Signal prisoner: the prisoner can be signaled using bird sounds if the prisoner is awake and the archer is sleeping.
17
+
Archers are trained in bird signaling and could intercept the message if they are awake.
18
+
-_Free prisoner_: Annalyn can attempt to sneak into the camp to free the prisoner.
19
+
This is risky and can only succeed in one of two ways:
20
+
- If Annalyn has her pet dog, she can rescue the prisoner if the archer is asleep.
15
21
The knight is scared of the dog and the archer will not have time to get ready before Annalyn and the prisoner can escape.
16
-
- If Annalyn does not have her dog then she and the prisoner must be very sneaky!
17
-
Annalyn can free the prisoner if the prisoner is awake and the knight and archer are both sleeping, but if the prisoner is sleeping they can't be rescued: the prisoner would be startled by Annalyn's sudden appearance and wake up the knight and archer.
22
+
- If Annalyn does not have her pet dog, then she and the prisoner must be very sneaky!
23
+
Annalyn can free the prisoner if the prisoner is awake and both the knight and archer are sleeping.
24
+
However, if the prisoner is sleeping, they can't be rescued, as the prisoner would be startled by Annalyn's sudden appearance and wake up the knight and archer.
18
25
19
-
You have four tasks: to implement the logic for determining if the above actions are available based on the state of the three characters found in the forest and whether Annalyn's pet dog is present or not.
26
+
You have four tasks: to implement the logic for determining if the above actions are available based on the state of the three characters in the forest and whether Annalyn's pet dog is present or not.
20
27
21
28
## 1. Check if a fast attack can be made
22
29
23
-
Implement the (_static_) `AnnalynsInfiltration.canFastAttack()` method that takes a boolean value that indicates if the knight is awake. This method returns `true` if a fast attack can be made based on the state of the knight. Otherwise, returns `false`:
30
+
Implement the (_static_) `AnnalynsInfiltration.canFastAttack()` method, which takes a boolean value indicating whether the knight is awake.
31
+
This method returns `true` if a fast attack can be made based on the state of the knight.
Implement the (_static_) `AnnalynsInfiltration.canSpy()` method that takes three boolean values, indicating if the knight, archer and the prisoner, respectively, are awake. The method returns `true` if the group can be spied upon, based on the state of the three characters. Otherwise, returns `false`:
42
+
Implement the (_static_) `AnnalynsInfiltration.canSpy()` method, which takes three boolean values indicating whether the knight, archer, and prisoner, respectively, are awake.
43
+
The method returns `true` if the group can be spied upon based on the state of the three characters.
Implement the (_static_) `AnnalynsInfiltration.canSignalPrisoner()` method that takes two boolean values, indicating if the archer and the prisoner, respectively, are awake. The method returns `true` if the prisoner can be signalled, based on the state of the two characters. Otherwise, returns `false`:
56
+
Implement the (_static_) `AnnalynsInfiltration.canSignalPrisoner()` method, which takes two boolean values indicating whether the archer and the prisoner, respectively, are awake.
57
+
The method returns `true` if the prisoner can be signaled based on the state of the two characters.
Implement the (_static_) `AnnalynsInfiltration.canFreePrisoner()` method that takes four boolean values. The first three parameters indicate if the knight, archer and the prisoner, respectively, are awake. The last parameter indicates if Annalyn's pet dog is present. The method returns `true` if the prisoner can be freed based on the state of the three characters and Annalyn's pet dog's presence. Otherwise, it returns `false`:
69
+
Implement the (_static_) `AnnalynsInfiltration.canFreePrisoner()` method, which takes four boolean values.
70
+
The first three parameters indicate whether the knight, archer, and prisoner, respectively, are awake.
71
+
The last parameter indicates whether Annalyn's pet dog is present.
72
+
The method returns `true` if the prisoner can be freed based on the state of the three characters and the presence of Annalyn's pet dog.
A complex number is a number in the form `a + b * i` where`a` and `b` are real and `i` satisfies `i^2 = -1`.
3
+
A **complex number** is expressed in the form `z = a + b * i`, where:
4
4
5
-
`a` is called the real part and `b` is called the imaginary part of `z`.
6
-
The conjugate of the number `a + b * i` is the number `a - b * i`.
7
-
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.
5
+
-`a` is the **real part** (a real number),
8
6
9
-
The sum/difference of two complex numbers involves adding/subtracting their real and imaginary parts separately:
10
-
`(a + i * b) + (c + i * d) = (a + c) + (b + d) * i`,
11
-
`(a + i * b) - (c + i * d) = (a - c) + (b - d) * i`.
7
+
-`b` is the **imaginary part** (also a real number), and
12
8
13
-
Multiplication result is by definition
14
-
`(a + i * b) * (c + i * d) = (a * c - b * d) + (b * c + a * d) * i`.
9
+
-`i` is the **imaginary unit** satisfying `i^2 = -1`.
15
10
16
-
The reciprocal of a non-zero complex number is
17
-
`1 / (a + i * b) = a/(a^2 + b^2) - b/(a^2 + b^2) * i`.
11
+
## Operations on Complex Numbers
18
12
19
-
Dividing a complex number `a + i * b` by another `c + i * d` gives:
20
-
`(a + i * b) / (c + i * d) = (a * c + b * d)/(c^2 + d^2) + (b * c - a * d)/(c^2 + d^2) * i`.
13
+
### Conjugate
21
14
22
-
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)`.
15
+
The conjugate of the complex number `z = a + b * i`is given by:
23
16
24
-
Implement the following operations:
17
+
```text
18
+
zc = a - b * i
19
+
```
25
20
26
-
- addition, subtraction, multiplication and division of two complex numbers,
27
-
- conjugate, absolute value, exponent of a given complex number.
21
+
### Absolute Value
28
22
29
-
Assume the programming language you are using does not have an implementation of complex numbers.
23
+
The absolute value (or modulus) of `z` is defined as:
24
+
25
+
```text
26
+
|z| = sqrt(a^2 + b^2)
27
+
```
28
+
29
+
The square of the absolute value is computed as the product of `z` and its conjugate `zc`:
30
+
31
+
```text
32
+
|z|^2 = z * zc = a^2 + b^2
33
+
```
34
+
35
+
### Addition
36
+
37
+
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:
38
+
39
+
```text
40
+
z1 + z2 = (a + b * i) + (c + d * i)
41
+
= (a + c) + (b + d) * i
42
+
```
43
+
44
+
### Subtraction
45
+
46
+
The difference of two complex numbers is obtained by subtracting their respective parts:
47
+
48
+
```text
49
+
z1 - z2 = (a + b * i) - (c + d * i)
50
+
= (a - c) + (b - d) * i
51
+
```
52
+
53
+
### Multiplication
54
+
55
+
The product of two complex numbers is defined as:
56
+
57
+
```text
58
+
z1 * z2 = (a + b * i) * (c + d * i)
59
+
= (a * c - b * d) + (b * c + a * d) * i
60
+
```
61
+
62
+
### Reciprocal
63
+
64
+
The reciprocal of a non-zero complex number is given by:
65
+
66
+
```text
67
+
1 / z = 1 / (a + b * i)
68
+
= a / (a^2 + b^2) - b / (a^2 + b^2) * i
69
+
```
70
+
71
+
### Division
72
+
73
+
The division of one complex number by another is given by:
74
+
75
+
```text
76
+
z1 / z2 = z1 * (1 / z2)
77
+
= (a + b * i) / (c + d * i)
78
+
= (a * c + b * d) / (c^2 + d^2) + (b * c - a * d) / (c^2 + d^2) * i
79
+
```
80
+
81
+
### Exponentiation
82
+
83
+
Raising _e_ (the base of the natural logarithm) to a complex exponent can be expressed using Euler's formula:
84
+
85
+
```text
86
+
e^(a + b * i) = e^a * e^(b * i)
87
+
= e^a * (cos(b) + i * sin(b))
88
+
```
89
+
90
+
## Implementation Requirements
91
+
92
+
Given that you should not use built-in support for complex numbers, implement the following operations:
93
+
94
+
-**addition** of two complex numbers
95
+
-**subtraction** of two complex numbers
96
+
-**multiplication** of two complex numbers
97
+
-**division** of two complex numbers
98
+
-**conjugate** of a complex number
99
+
-**absolute value** of a complex number
100
+
-**exponentiation** of _e_ (the base of the natural logarithm) to a complex number
0 commit comments