Initial commit
This commit is contained in:
commit
70fb45c9b9
|
@ -0,0 +1 @@
|
||||||
|
/target
|
|
@ -0,0 +1,7 @@
|
||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
version = 3
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "collatz"
|
||||||
|
version = "0.1.0"
|
|
@ -0,0 +1,6 @@
|
||||||
|
[package]
|
||||||
|
name = "collatz"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
|
@ -0,0 +1,24 @@
|
||||||
|
/// Determine the length of the collatz sequence beginning at `n`.
|
||||||
|
fn collatz_length(mut n: i32) -> u32 {
|
||||||
|
let mut i: u32 = 0;
|
||||||
|
loop {
|
||||||
|
i = i+1;
|
||||||
|
if n == 1 {
|
||||||
|
break
|
||||||
|
} else if n % 2 == 0 {
|
||||||
|
n = n / 2;
|
||||||
|
} else {
|
||||||
|
n = 3 * n + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_collatz_length() {
|
||||||
|
assert_eq!(collatz_length(11), 15);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
println!("Length: {}", collatz_length(11));
|
||||||
|
}
|
|
@ -0,0 +1 @@
|
||||||
|
/target
|
|
@ -0,0 +1,7 @@
|
||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
version = 3
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "geometry"
|
||||||
|
version = "0.1.0"
|
|
@ -0,0 +1,6 @@
|
||||||
|
[package]
|
||||||
|
name = "geometry"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
|
@ -0,0 +1,36 @@
|
||||||
|
// Calculate the magnitude of a vector by summing the squares of its coordinates
|
||||||
|
// and taking the square root. Use the `sqrt()` method to calculate the square
|
||||||
|
// root, like `v.sqrt()`.
|
||||||
|
|
||||||
|
fn magnitude(v: &[f64; 3]) -> f64 {
|
||||||
|
let mut r: f64 = 0.0;
|
||||||
|
for x in v {
|
||||||
|
r += x.powi(2);
|
||||||
|
}
|
||||||
|
r.sqrt()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Normalize a vector by calculating its magnitude and dividing all of its
|
||||||
|
// coordinates by that magnitude.
|
||||||
|
|
||||||
|
fn normalize(v: &mut [f64; 3]) -> &[f64; 3] {
|
||||||
|
let mag = magnitude(v);
|
||||||
|
for elm in 0..v.len() {
|
||||||
|
v[elm] /= mag;
|
||||||
|
}
|
||||||
|
v
|
||||||
|
}
|
||||||
|
|
||||||
|
// Use the following `main` to test your work.
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
println!(
|
||||||
|
"Magnitude of a unit vector: {}",
|
||||||
|
magnitude(&[0.0, 1.0, 0.0])
|
||||||
|
);
|
||||||
|
|
||||||
|
let mut v = [1.0, 2.0, 9.0];
|
||||||
|
println!("Magnitude of {v:?}: {}", magnitude(&v));
|
||||||
|
normalize(&mut v);
|
||||||
|
println!("Magnitude of {v:?} after normalization: {}", magnitude(&v));
|
||||||
|
}
|
|
@ -0,0 +1 @@
|
||||||
|
/target
|
|
@ -0,0 +1,7 @@
|
||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
version = 3
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "nest_array"
|
||||||
|
version = "0.1.0"
|
|
@ -0,0 +1,6 @@
|
||||||
|
[package]
|
||||||
|
name = "nest_array"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
|
@ -0,0 +1,42 @@
|
||||||
|
// TODO: remove this when you're done with your implementation.
|
||||||
|
#![allow(unused_variables, dead_code)]
|
||||||
|
|
||||||
|
fn transpose(matrix: [[i32; 3]; 3]) -> [[i32; 3]; 3] {
|
||||||
|
let mut t = [[0; 3]; 3];
|
||||||
|
for row in 0..3 {
|
||||||
|
for column in 0..3 {
|
||||||
|
t[column][row] = matrix[row][column];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return t;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_transpose() {
|
||||||
|
let matrix = [
|
||||||
|
[101, 102, 103], //
|
||||||
|
[201, 202, 203],
|
||||||
|
[301, 302, 303],
|
||||||
|
];
|
||||||
|
let transposed = transpose(matrix);
|
||||||
|
assert_eq!(
|
||||||
|
transposed,
|
||||||
|
[
|
||||||
|
[101, 201, 301], //
|
||||||
|
[102, 202, 302],
|
||||||
|
[103, 203, 303],
|
||||||
|
]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let matrix = [
|
||||||
|
[101, 102, 103], // <-- the comment makes rustfmt add a newline
|
||||||
|
[201, 202, 203],
|
||||||
|
[301, 302, 303],
|
||||||
|
];
|
||||||
|
|
||||||
|
println!("matrix: {:#?}", matrix);
|
||||||
|
let transposed = transpose(matrix);
|
||||||
|
println!("transposed: {:#?}", transposed);
|
||||||
|
}
|
Loading…
Reference in New Issue