Skip to content

Commit 0036292

Browse files
Fix more exercises
1 parent 769ac9e commit 0036292

File tree

16 files changed

+512
-280
lines changed

16 files changed

+512
-280
lines changed

courses/comprehensive_rust_training/030_types_and_values/99_lab.rst

+3-14
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ When will this function panic?
2525
}
2626
}
2727
28+
.. container:: source_include 030_types_and_values/src/030_types_and_values.rs :start-after://ANCHOR-main :code:rust
29+
2830
fn main() {
2931
let n = 20;
3032
println!("fib({n}) = {}", fib(n));
@@ -34,17 +36,4 @@ When will this function panic?
3436
Fibonacci Solution
3537
---------------------
3638

37-
.. code:: rust
38-
39-
fn fib(n: u32) -> u32 {
40-
if n < 2 {
41-
return n;
42-
} else {
43-
return fib(n - 1) + fib(n - 2);
44-
}
45-
}
46-
47-
fn main() {
48-
let n = 20;
49-
println!("fib({n}) = {}", fib(n));
50-
}
39+
.. container:: source_include 030_types_and_values/src/030_types_and_values.rs :start-after://ANCHOR-fib :end-before://ANCHOR-main :code:rust
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright 2023 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
//ANCHOR-fib
16+
fn fib(n: u32) -> u32 {
17+
if n < 2 {
18+
return n;
19+
} else {
20+
return fib(n - 1) + fib(n - 2);
21+
}
22+
}
23+
24+
//ANCHOR-main
25+
fn main() {
26+
let n = 20;
27+
println!("fib({n}) = {}", fib(n));
28+
}

courses/comprehensive_rust_training/040_control_flow_basics/99_lab.rst

+2-21
Original file line numberDiff line numberDiff line change
@@ -36,29 +36,10 @@ given initial :rust:`n`.
3636
todo!("Implement this")
3737
}
3838
39-
fn main() {
40-
// should be 15
41-
println!("Length: {}", collatz_length(11));
42-
}
39+
.. container:: source_include 040_control_flow_basics/src/040_control_flow_basics.rs :start-after://ANCHOR-main :code:rust
4340

4441
----------------------------
4542
Collatz Sequence Solution
4643
----------------------------
4744

48-
.. code:: rust
49-
50-
/// Determine the length of the
51-
/// collatz sequence beginning at `n`.
52-
fn collatz_length(mut n: i32) -> u32 {
53-
let mut len = 1;
54-
while n > 1 {
55-
n = if n % 2 == 0 { n / 2 } else { 3 * n + 1 };
56-
len += 1;
57-
}
58-
len
59-
}
60-
61-
fn main() {
62-
// should be 15
63-
println!("Length: {}", collatz_length(11));
64-
}
45+
.. container:: source_include 040_control_flow_basics/src/040_control_flow_basics.rs :start-after://ANCHOR-solution :end-before://ANCHOR-tests :code:rust
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright 2023 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
//ANCHOR-solution
16+
/// Determine the length of the collatz sequence beginning at `n`.
17+
fn collatz_length(mut n: i32) -> u32 {
18+
let mut len = 1;
19+
while n > 1 {
20+
n = if n % 2 == 0 { n / 2 } else { 3 * n + 1 };
21+
len += 1;
22+
}
23+
len
24+
}
25+
26+
//ANCHOR-tests
27+
#[test]
28+
fn test_collatz_length() {
29+
assert_eq!(collatz_length(11), 15);
30+
}
31+
32+
//ANCHOR-main
33+
fn main() {
34+
println!("Length: {}", collatz_length(11));
35+
}

courses/comprehensive_rust_training/050_tuples_and_arrays/99_lab.rst

+7-35
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ Exercise: Nested Arrays
33
=========================
44

55
-------------------------
6-
Nested Arrays Problem
6+
Nested Arrays Setup
77
-------------------------
88

99
Arrays can contain other arrays:
@@ -37,6 +37,10 @@ into
3737
3 & 6 & 9
3838
\end{bmatrix}
3939
40+
-------------------------
41+
Nested Arrays Problem
42+
-------------------------
43+
4044
Copy the code below to https://play.rust-lang.org/ and implement the
4145
function. This function only operates on 3x3 matrices.
4246

@@ -46,42 +50,10 @@ function. This function only operates on 3x3 matrices.
4650
todo!()
4751
}
4852
49-
fn main() {
50-
let matrix = [
51-
[101, 102, 103], // comment makes rustfmt add newline
52-
[201, 202, 203],
53-
[301, 302, 303],
54-
];
55-
56-
dbg!(matrix);
57-
let transposed = transpose(matrix);
58-
dbg!(transposed);
59-
}
53+
.. container:: source_include 050_tuples_and_arrays/src/050_tuples_and_arrays.rs :start-after://ANCHOR-main :code:rust
6054

6155
-------------------------
6256
Nested Arrays Solution
6357
-------------------------
6458

65-
.. code:: rust
66-
67-
fn transpose(matrix: [[i32; 3]; 3]) -> [[i32; 3]; 3] {
68-
let mut result = [[0; 3]; 3];
69-
for i in 0..3 {
70-
for j in 0..3 {
71-
result[j][i] = matrix[i][j];
72-
}
73-
}
74-
result
75-
}
76-
77-
fn main() {
78-
let matrix = [
79-
[101, 102, 103], // comment makes rustfmt add newline
80-
[201, 202, 203],
81-
[301, 302, 303],
82-
];
83-
84-
dbg!(matrix);
85-
let transposed = transpose(matrix);
86-
dbg!(transposed);
87-
}
59+
.. container:: source_include 050_tuples_and_arrays/src/050_tuples_and_arrays.rs :start-after://ANCHOR-solution :code:rust :end-before://ANCHOR-tests
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright 2022 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
//ANCHOR-solution
16+
fn transpose(matrix: [[i32; 3]; 3]) -> [[i32; 3]; 3] {
17+
let mut result = [[0; 3]; 3];
18+
for i in 0..3 {
19+
for j in 0..3 {
20+
result[j][i] = matrix[i][j];
21+
}
22+
}
23+
result
24+
}
25+
26+
//ANCHOR-tests
27+
#[test]
28+
fn test_transpose() {
29+
let matrix = [
30+
[101, 102, 103], //
31+
[201, 202, 203],
32+
[301, 302, 303],
33+
];
34+
let transposed = transpose(matrix);
35+
assert_eq!(
36+
transposed,
37+
[
38+
[101, 201, 301], //
39+
[102, 202, 302],
40+
[103, 203, 303],
41+
]
42+
);
43+
}
44+
45+
//ANCHOR-main
46+
fn main() {
47+
let matrix = [
48+
[101, 102, 103], // <-- the comment makes rustfmt add a newline
49+
[201, 202, 203],
50+
[301, 302, 303],
51+
];
52+
53+
println!("matrix: {:#?}", matrix);
54+
let transposed = transpose(matrix);
55+
println!("transposed: {:#?}", transposed);
56+
}

courses/comprehensive_rust_training/060_references/99_lab.rst

+4-37
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ We will create a few utility functions for 3-dimensional geometry,
1010
representing a point as :rust:`[f64;3]`. It is up to you to determine the
1111
function signatures.
1212

13-
::
13+
.. code:: rust
1414
1515
// Calculate the magnitude of a vector by summing the squares of its coordinates
1616
// and taking the square root. Use the `sqrt()` method to calculate the square
@@ -27,48 +27,15 @@ function signatures.
2727
todo!()
2828
}
2929
30-
// Use the following `main` to test your work.
31-
32-
fn main() {
33-
println!("Magnitude of a unit vector: {}", magnitude(&[0.0, 1.0, 0.0]));
30+
Use the following :rust:`main` to test your work.
3431

35-
let mut v = [1.0, 2.0, 9.0];
36-
println!("Magnitude of {v:?}: {}", magnitude(&v));
37-
normalize(&mut v);
38-
println!("Magnitude of {v:?} after normalization: {}", magnitude(&v));
39-
}
32+
.. container:: source_include 060_references/src/060_references.rs :start-after://ANCHOR-main :code:rust
4033

4134
--------------------
4235
Geometry Solution
4336
--------------------
4437

45-
.. code:: rust
46-
47-
/// Calculate the magnitude of the given vector.
48-
fn magnitude(vector: &[f64; 3]) -> f64 {
49-
let mut mag_squared = 0.0;
50-
for coord in vector {
51-
mag_squared += coord * coord;
52-
}
53-
mag_squared.sqrt()
54-
}
55-
56-
/// Change the magnitude of the vector to 1.0 without changing its direction.
57-
fn normalize(vector: &mut [f64; 3]) {
58-
let mag = magnitude(vector);
59-
for item in vector {
60-
*item /= mag;
61-
}
62-
}
63-
64-
fn main() {
65-
println!("Magnitude of a unit vector: {}", magnitude(&[0.0, 1.0, 0.0]));
66-
67-
let mut v = [1.0, 2.0, 9.0];
68-
println!("Magnitude of {v:?}: {}", magnitude(&v));
69-
normalize(&mut v);
70-
println!("Magnitude of {v:?} after normalization: {}", magnitude(&v));
71-
}
38+
.. container:: source_include 060_references/src/060_references.rs :start-after://ANCHOR-solution :end-before://ANCHOR-main :code:rust
7239

7340
------------------------
7441
Additional Information
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright 2022 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
//ANCHOR-solution
16+
/// Calculate the magnitude of the given vector.
17+
fn magnitude(vector: &[f64; 3]) -> f64 {
18+
let mut mag_squared = 0.0;
19+
for coord in vector {
20+
mag_squared += coord * coord;
21+
}
22+
mag_squared.sqrt()
23+
}
24+
25+
/// Change the magnitude of the vector to 1.0 without changing its direction.
26+
fn normalize(vector: &mut [f64; 3]) {
27+
let mag = magnitude(vector);
28+
for item in vector {
29+
*item /= mag;
30+
}
31+
}
32+
33+
//ANCHOR-main
34+
fn main() {
35+
println!("Magnitude of a unit vector: {}", magnitude(&[0.0, 1.0, 0.0]));
36+
37+
let mut v = [1.0, 2.0, 9.0];
38+
println!("Magnitude of {v:?}: {}", magnitude(&v));
39+
normalize(&mut v);
40+
println!("Magnitude of {v:?} after normalization: {}", magnitude(&v));
41+
}

0 commit comments

Comments
 (0)