From 70fb45c9b9457cb495a3d99b529d619f76ee0c5c Mon Sep 17 00:00:00 2001 From: Alex Denes Date: Mon, 28 Oct 2024 14:11:46 +0000 Subject: [PATCH] Initial commit --- collatz/.gitignore | 1 + collatz/Cargo.lock | 7 +++++++ collatz/Cargo.toml | 6 ++++++ collatz/src/main.rs | 24 ++++++++++++++++++++++++ geometry/.gitignore | 1 + geometry/Cargo.lock | 7 +++++++ geometry/Cargo.toml | 6 ++++++ geometry/src/main.rs | 36 ++++++++++++++++++++++++++++++++++++ nest_array/.gitignore | 1 + nest_array/Cargo.lock | 7 +++++++ nest_array/Cargo.toml | 6 ++++++ nest_array/src/main.rs | 42 ++++++++++++++++++++++++++++++++++++++++++ 12 files changed, 144 insertions(+) create mode 100644 collatz/.gitignore create mode 100644 collatz/Cargo.lock create mode 100644 collatz/Cargo.toml create mode 100644 collatz/src/main.rs create mode 100644 geometry/.gitignore create mode 100644 geometry/Cargo.lock create mode 100644 geometry/Cargo.toml create mode 100644 geometry/src/main.rs create mode 100644 nest_array/.gitignore create mode 100644 nest_array/Cargo.lock create mode 100644 nest_array/Cargo.toml create mode 100644 nest_array/src/main.rs diff --git a/collatz/.gitignore b/collatz/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/collatz/.gitignore @@ -0,0 +1 @@ +/target diff --git a/collatz/Cargo.lock b/collatz/Cargo.lock new file mode 100644 index 0000000..fa34d02 --- /dev/null +++ b/collatz/Cargo.lock @@ -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" diff --git a/collatz/Cargo.toml b/collatz/Cargo.toml new file mode 100644 index 0000000..ebf3907 --- /dev/null +++ b/collatz/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "collatz" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/collatz/src/main.rs b/collatz/src/main.rs new file mode 100644 index 0000000..6b8bb7a --- /dev/null +++ b/collatz/src/main.rs @@ -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)); +} diff --git a/geometry/.gitignore b/geometry/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/geometry/.gitignore @@ -0,0 +1 @@ +/target diff --git a/geometry/Cargo.lock b/geometry/Cargo.lock new file mode 100644 index 0000000..af89272 --- /dev/null +++ b/geometry/Cargo.lock @@ -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" diff --git a/geometry/Cargo.toml b/geometry/Cargo.toml new file mode 100644 index 0000000..5defe26 --- /dev/null +++ b/geometry/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "geometry" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/geometry/src/main.rs b/geometry/src/main.rs new file mode 100644 index 0000000..f3f8e80 --- /dev/null +++ b/geometry/src/main.rs @@ -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)); +} diff --git a/nest_array/.gitignore b/nest_array/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/nest_array/.gitignore @@ -0,0 +1 @@ +/target diff --git a/nest_array/Cargo.lock b/nest_array/Cargo.lock new file mode 100644 index 0000000..936560b --- /dev/null +++ b/nest_array/Cargo.lock @@ -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" diff --git a/nest_array/Cargo.toml b/nest_array/Cargo.toml new file mode 100644 index 0000000..74983a7 --- /dev/null +++ b/nest_array/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "nest_array" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/nest_array/src/main.rs b/nest_array/src/main.rs new file mode 100644 index 0000000..1a13328 --- /dev/null +++ b/nest_array/src/main.rs @@ -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); +}