Skip to content

Commit 943a219

Browse files
author
Colin Leach
committed
captains-log concept exercise
1 parent 7eded14 commit 943a219

File tree

17 files changed

+576
-294
lines changed

17 files changed

+576
-294
lines changed

config.json

+9-5
Original file line numberDiff line numberDiff line change
@@ -242,12 +242,16 @@
242242
"status": "beta"
243243
},
244244
{
245-
"slug": "old-annalyns-infiltration",
246-
"name": "Old Annalyn's Infiltration",
245+
"slug": "captains-log",
246+
"name": "captains-log",
247247
"uuid": "05cfc609-3a14-479a-b6fa-6d2a53ad111c",
248-
"concepts": [],
249-
"prerequisites": [],
250-
"status": "deprecated"
248+
"concepts": [
249+
"randomness"
250+
],
251+
"prerequisites": [
252+
"functions"
253+
],
254+
"status": "beta"
251255
},
252256
{
253257
"slug": "old-elyses-enchantments",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Hints
2+
3+
## 1. Generate a random planet
4+
5+
- How does [`rand()`][rand] behave when given a collection-type argument?
6+
7+
## 2. Generate a random starship registry number
8+
9+
- Think about [`rand()`][rand] with an integer range.
10+
- Remember string [interpolation][interpolation].
11+
12+
## 3. Generate a random stardate
13+
14+
- What does [`rand()`][rand] return when called with no argument?
15+
- Think about scaling and shifting the raw result.
16+
17+
## 4. Generate a rounded stardate
18+
19+
- Read the Introduction, and see what it says about floating-point ranges.
20+
21+
## 5. Pick some random starships from a list.
22+
23+
- For randomness, the input list needs to be [shuffled][shuffle].
24+
- For safety, it is better to create a shuffled copy, leaving the input unchanged (otherwise, the function would be mutating, and called `pick_starships!`)
25+
- Slice off what you need from the resulting vector.
26+
27+
## 5a. Optional: other argument types.
28+
29+
- How does [`Random.shuffle()`][shuffle] handle other iterable types? Check the documentation and/or experiment with it.
30+
- The REPL, [Pluto.jl][pluto], or [Jupyter][jupyter] are all useful for this.
31+
- If necessary, how do you convert an arbitrary iterable to a vector?
32+
33+
[rand]: https://docs.julialang.org/en/v1/stdlib/Random/#Random-generation-functions
34+
[interpolation]: https://docs.julialang.org/en/v1/manual/strings/#string-interpolation
35+
[shuffle]: https://docs.julialang.org/en/v1/stdlib/Random/#Random.shuffle
36+
[pluto]: https://plutojl.org/
37+
[jupyter]: https://github.com/JuliaLang/IJulia.jl
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# Instructions
2+
3+
Mary is a big fan of the TV series _Star Trek: The Next Generation_. She often plays pen-and-paper role playing games, where she and her friends pretend to be the crew of the _Starship Enterprise_. Mary's character is Captain Picard, which means she has to keep the captain's log. She loves the creative part of the game, but doesn't like to generate random data on the spot.
4+
5+
Help Mary by creating random generators for data commonly appearing in the captain's log.
6+
7+
## 1. Generate a random planet
8+
9+
The _Starship Enterprise_ encounters many planets in its travels. Planets in the Star Trek universe are split into categories based on their properties. For example, Earth is a class M planet. All possible planetary classes are: D, H, J, K, L, M, N, R, T, and Y.
10+
11+
Implement the `random_planet` function. It should return one of the planetary classes at random.
12+
13+
```julia-repl
14+
julia> random_planet_class()
15+
'K'
16+
```
17+
18+
## 2. Generate a random starship registry number
19+
20+
Enterprise (registry number NCC-1701) is not the only starship flying around! When it rendezvous with another starship, Mary needs to log the registry number of that starship.
21+
22+
Registry numbers start with the prefix "NCC-" and then use a number from 1000 to 9999 (inclusive).
23+
24+
Implement the `random_ship_registry_number` function that returns a random starship registry number.
25+
26+
```julia-repl
27+
julia> random_ship_registry_number()
28+
"NCC-1947"
29+
```
30+
31+
## 3. Generate a random stardate
32+
33+
What's the use of a log if it doesn't include dates?
34+
35+
A stardate is a floating point number. The adventures of the _Starship Enterprise_ from the first season of _The Next Generation_ take place between the stardates 41000.0 and 42000.0. The "4" stands for the 24th century, the "1" for the first season.
36+
37+
Implement the function `random_stardate` that returns a floating point number between 41000.0 and 42000.0 (inclusive).
38+
39+
```julia-repl
40+
julia> random_stardate()
41+
41458.15721310934
42+
```
43+
44+
## 4. Generate a rounded stardate
45+
46+
There have been complaints about having too many decimal points in stardates, so Mary decides to round logs to one decimal place.
47+
48+
Though this could be done by rounding values from `random_stardate()`, you realize that there is a much simpler approach.
49+
50+
Implement the function `random_stardate_v2` that returns a floating point number between 41000.0 and 42000.0 (inclusive), with a single decimal place.
51+
52+
```julia-repl
53+
julia> random_stardate_v2()
54+
41732.6
55+
```
56+
57+
## 5. Pick some random starships from a list.
58+
59+
One version of the game involves interacting with other starships, and Mary needs a selection.
60+
61+
There is a full list of possible starships, but only a few of these is needed in each game, and they should be chosen at random.
62+
63+
Implement the `pick_starships(starships, number_needed)` function, which takes a vector of unique starship registry numbers, and returns a vector of length `number_needed`.
64+
Result values should be taken from `starships`, with no duplicates.
65+
66+
```julia-repl
67+
julia> pick_starships(["NCC-5011", "NCC-1228", "NCC-7039", "NCC-3978", "NCC-1476"], 2)
68+
2-element Vector{String}:
69+
"NCC-1228"
70+
"NCC-5011"
71+
```
72+
73+
## _5a. Optionally, think about other argument types._
74+
75+
We constrained the `starships` argument to be a vector.
76+
How would you implement `pick_starships()` to work with other iterables, such as tuples or sets?
77+
78+
There are no tests for this part.

0 commit comments

Comments
 (0)