From 885f3cdf0dae82921d91ca4fcbe16e2b285ff9cb Mon Sep 17 00:00:00 2001 From: teridax Date: Wed, 19 Apr 2023 12:58:30 +0200 Subject: [PATCH] restructured repository from gitlab --- duplicates/Cargo.toml | 8 ++++++++ duplicates/src/main.rs | 19 ++++++++++++++++++ fibonacci/Cargo.toml | 8 ++++++++ fibonacci/src/main.rs | 25 ++++++++++++++++++++++++ tuple_arithmetic/Cargo.toml | 8 ++++++++ tuple_arithmetic/src/main.rs | 38 ++++++++++++++++++++++++++++++++++++ 6 files changed, 106 insertions(+) create mode 100644 duplicates/Cargo.toml create mode 100644 duplicates/src/main.rs create mode 100644 fibonacci/Cargo.toml create mode 100644 fibonacci/src/main.rs create mode 100644 tuple_arithmetic/Cargo.toml create mode 100644 tuple_arithmetic/src/main.rs diff --git a/duplicates/Cargo.toml b/duplicates/Cargo.toml new file mode 100644 index 0000000..40f182e --- /dev/null +++ b/duplicates/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "duplicates" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/duplicates/src/main.rs b/duplicates/src/main.rs new file mode 100644 index 0000000..355e4cd --- /dev/null +++ b/duplicates/src/main.rs @@ -0,0 +1,19 @@ + +fn duplicates(arr: &[T]) -> usize where T: PartialEq { + let mut dups = 0; + for x in 0..arr.len() { + let to_cmp = &arr[x]; + for y in (x+1)..arr.len() { + if &arr[y] == to_cmp { + dups += 1; + } + } + } + dups * 2 +} + +fn main() { + let arr = ['😇', '🙈', '💀', '💦', '💣', '💀']; + + println!("Duplicates: {}", duplicates(&arr)); +} diff --git a/fibonacci/Cargo.toml b/fibonacci/Cargo.toml new file mode 100644 index 0000000..9cd048e --- /dev/null +++ b/fibonacci/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "fibonacci" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/fibonacci/src/main.rs b/fibonacci/src/main.rs new file mode 100644 index 0000000..3737ddb --- /dev/null +++ b/fibonacci/src/main.rs @@ -0,0 +1,25 @@ + +fn fib_rec(x: u128) -> u128 { + if x < 2 { + return x; + } + + return fib_rec(x - 1) + fib_rec(x - 2); +} + +fn fib_loop(x: u128) -> u128 { + let mut sum = 0; + let mut sum2 = 1; + + for x in 0..(x-1) { + let t = sum; + sum = sum2; + sum2 = t + sum; + } + sum2 +} + +fn main() { + + println!("{}", fib_loop(5)); +} diff --git a/tuple_arithmetic/Cargo.toml b/tuple_arithmetic/Cargo.toml new file mode 100644 index 0000000..df179e8 --- /dev/null +++ b/tuple_arithmetic/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "rust_playground" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/tuple_arithmetic/src/main.rs b/tuple_arithmetic/src/main.rs new file mode 100644 index 0000000..6e42e8d --- /dev/null +++ b/tuple_arithmetic/src/main.rs @@ -0,0 +1,38 @@ +/* Compressed version: + * + * type V=(f64,f64);fn t(a:V,b:V,f:fn( + * a:f64,b:f64)->f64)->V{(f(a.0,b.0),f + * (a.1,b.1))}fn main(){print!("{:?}", + * t(t((2.42,2.07),(0.3,3.2),|a,b|{a/b + * }),t((1.7,2.3),(0.73,0.42),|a,b|{a* + * b}),|a,b|{a-b}))} + * + */ + + +fn merge_tuple(a: (f64, f64), b: (f64, f64), f: fn (a: f64, b: f64) -> f64) -> (f64, f64) { + ( + f(a.0, b.0), + f(a.1, b.1), + ) +} + +fn add_tuple(a: (f64, f64), b: (f64, f64)) -> (f64, f64) { + merge_tuple(a, b, |a, b| a + b) +} + +fn sub_tuple(a: (f64, f64), b: (f64, f64)) -> (f64, f64) { + merge_tuple(a, b, |a, b| a - b) +} + +fn mul_tuple(a: (f64, f64), b: (f64, f64)) -> (f64, f64) { + merge_tuple(a, b, |a, b| a * b) +} + +fn div_tuple(a: (f64, f64), b: (f64, f64)) -> (f64, f64) { + merge_tuple(a, b, |a, b| a / b) +} + +fn main() { + println!("{:#?}", sub_tuple(div_tuple((2.42, 2.07), (0.3, 3.2)), mul_tuple((1.7, 2.3), (0.73, 0.42)))); +}