restructured repository from gitlab

This commit is contained in:
Sven Vogel 2023-04-19 12:58:30 +02:00
parent 88a21d13b6
commit 885f3cdf0d
6 changed files with 106 additions and 0 deletions

8
duplicates/Cargo.toml Normal file
View File

@ -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]

19
duplicates/src/main.rs Normal file
View File

@ -0,0 +1,19 @@
fn duplicates<T>(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));
}

8
fibonacci/Cargo.toml Normal file
View File

@ -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]

25
fibonacci/src/main.rs Normal file
View File

@ -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));
}

View File

@ -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]

View File

@ -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))));
}