From 1f094c137df7daf022b1c421555cf9d9169c5c97 Mon Sep 17 00:00:00 2001 From: teridax Date: Fri, 21 Apr 2023 12:38:18 +0200 Subject: [PATCH] updated license information --- .idea/.gitignore | 8 +++ .idea/Rust-Programming.iml | 12 ++++ .idea/misc.xml | 6 ++ .idea/modules.xml | 8 +++ .idea/vcs.xml | 6 ++ duplicates/src/main.rs | 116 +++++++++++++++++++++++++++++++++-- fibonacci/src/main.rs | 21 ++++++- tuple_arithmetic/src/main.rs | 35 ++++++++--- 8 files changed, 195 insertions(+), 17 deletions(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/Rust-Programming.iml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/Rust-Programming.iml b/.idea/Rust-Programming.iml new file mode 100644 index 0000000..78c56dd --- /dev/null +++ b/.idea/Rust-Programming.iml @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..639900d --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..8d670ec --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/duplicates/src/main.rs b/duplicates/src/main.rs index 355e4cd..a55267a 100644 --- a/duplicates/src/main.rs +++ b/duplicates/src/main.rs @@ -1,19 +1,123 @@ +/** + * _ _ _ _ + * __ ___ __(_) |_| |_ ___ _ __ | |__ _ _ + * \ \ /\ / / '__| | __| __/ _ \ '_ \ | '_ \| | | | + * \ V V /| | | | |_| || __/ | | | | |_) | |_| | + * \_/\_/ |_| |_|\__|\__\___|_| |_| |_.__/ \__, | + * |___/ + * ____ __ __ _ + * / ___|_ _____ _ __ \ \ / /__ __ _ ___| | + * \___ \ \ / / _ \ '_ \ \ \ / / _ \ / _` |/ _ \ | + * ___) \ V / __/ | | | \ V / (_) | (_| | __/ | + * |____/ \_/ \___|_| |_| \_/ \___/ \__, |\___|_| + * |___/ + * Licensed under the GPLv2 License, Version 2.0 (the "License"); + * Copyright (c) Sven Vogel + */ + +use crate::FloatOrChar::{Char, Float}; +use std::collections::{HashMap, HashSet}; +use std::fmt::Debug; +use std::usize; + +/// pretty useless enum that either contains a char or +/// a f32 +#[derive(Debug)] +enum FloatOrChar { + Float(f64), + Char(char), +} + +impl PartialEq for FloatOrChar { + /// only returns true if + /// self and other of the same enum variant and their wrapped values match + fn eq(&self, other: &Self) -> bool { + return match self { + Float(x) => { + if let Float(y) = *other { + // compare floats with tolerance + (*x - y).abs() < f64::EPSILON + } else { + false // other is not a Float32 + } + } + Char(x) => { + if let Char(y) = *other { + *x == y + } else { + false // other is not a Char + } + } + }; + } + + /// opposite of equals + fn ne(&self, other: &Self) -> bool { + !self.eq(other) + } +} + +/// count all duplicate values in an array +/// equivalence is determined by the [`PartialEq`] trait +fn duplicates(arr: &[T]) -> usize +where + T: PartialEq + Debug, +{ + let mut total_dups = 0; -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() { + let mut count = 0; + + for y in (x + 1)..arr.len() { if &arr[y] == to_cmp { - dups += 1; + count += 1; } } + + total_dups += count; + + if count > 0 { + println!("Element {:?} was duplicated {} time(s)", to_cmp, count); + } } - dups * 2 + + total_dups +} + +/// read some data from stdin and stores it into a vector. +/// the reading of data stops if the input is neither a float nor a single character. +/// input line are trimmed +fn read_array() -> Vec { + // temporary string buffer for reading + let mut buf = String::new(); + // storage for our parsed array + let mut elems = Vec::::new(); + + println!("Enter some floats or characters, one per line:"); + + loop { + // read some characters + std::io::stdin().read_line(&mut buf).unwrap(); + + // parse the data + if let Ok(float) = buf.trim().parse::() { + elems.push(Float(float)); + } else if let Ok(char) = buf.trim().parse::() { + elems.push(Char(char)); + } else { + break; + } + + // empty string + buf.clear(); + } + + elems } fn main() { - let arr = ['😇', '🙈', '💀', '💦', '💣', '💀']; + let arr = &read_array()[..]; println!("Duplicates: {}", duplicates(&arr)); } diff --git a/fibonacci/src/main.rs b/fibonacci/src/main.rs index 3737ddb..9551b6e 100644 --- a/fibonacci/src/main.rs +++ b/fibonacci/src/main.rs @@ -1,4 +1,21 @@ +/** + * _ _ _ _ + * __ ___ __(_) |_| |_ ___ _ __ | |__ _ _ + * \ \ /\ / / '__| | __| __/ _ \ '_ \ | '_ \| | | | + * \ V V /| | | | |_| || __/ | | | | |_) | |_| | + * \_/\_/ |_| |_|\__|\__\___|_| |_| |_.__/ \__, | + * |___/ + * ____ __ __ _ + * / ___|_ _____ _ __ \ \ / /__ __ _ ___| | + * \___ \ \ / / _ \ '_ \ \ \ / / _ \ / _` |/ _ \ | + * ___) \ V / __/ | | | \ V / (_) | (_| | __/ | + * |____/ \_/ \___|_| |_| \_/ \___/ \__, |\___|_| + * |___/ + * Licensed under the GPLv2 License, Version 2.0 (the "License"); + * Copyright (c) Sven Vogel + */ +/// recursive variant of the fibonacci function fn fib_rec(x: u128) -> u128 { if x < 2 { return x; @@ -7,11 +24,12 @@ fn fib_rec(x: u128) -> u128 { return fib_rec(x - 1) + fib_rec(x - 2); } +/// iterative variant of the fibonacci function fn fib_loop(x: u128) -> u128 { let mut sum = 0; let mut sum2 = 1; - for x in 0..(x-1) { + for x in 0..(x - 1) { let t = sum; sum = sum2; sum2 = t + sum; @@ -20,6 +38,5 @@ fn fib_loop(x: u128) -> u128 { } fn main() { - println!("{}", fib_loop(5)); } diff --git a/tuple_arithmetic/src/main.rs b/tuple_arithmetic/src/main.rs index 6e42e8d..64c5249 100644 --- a/tuple_arithmetic/src/main.rs +++ b/tuple_arithmetic/src/main.rs @@ -1,4 +1,20 @@ -/* Compressed version: +/** + * _ _ _ _ + * __ ___ __(_) |_| |_ ___ _ __ | |__ _ _ + * \ \ /\ / / '__| | __| __/ _ \ '_ \ | '_ \| | | | + * \ V V /| | | | |_| || __/ | | | | |_) | |_| | + * \_/\_/ |_| |_|\__|\__\___|_| |_| |_.__/ \__, | + * |___/ + * ____ __ __ _ + * / ___|_ _____ _ __ \ \ / /__ __ _ ___| | + * \___ \ \ / / _ \ '_ \ \ \ / / _ \ / _` |/ _ \ | + * ___) \ V / __/ | | | \ V / (_) | (_| | __/ | + * |____/ \_/ \___|_| |_| \_/ \___/ \__, |\___|_| + * |___/ + * Licensed under the GPLv2 License, Version 2.0 (the "License"); + * Copyright (c) Sven Vogel + * + * 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 @@ -6,15 +22,10 @@ * 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 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) { @@ -34,5 +45,11 @@ fn div_tuple(a: (f64, f64), b: (f64, f64)) -> (f64, f64) { } 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)))); + println!( + "{:#?}", + sub_tuple( + div_tuple((2.42, 2.07), (0.3, 3.2)), + mul_tuple((1.7, 2.3), (0.73, 0.42)) + ) + ); }