From d9939e7b1ea45317c2d5c83cbc6ac9456a7065fd Mon Sep 17 00:00:00 2001 From: Zapwai Date: Wed, 3 Jan 2024 13:05:33 -0500 Subject: Rust for week 250 --- challenge-250/zapwai/rust/ch-1.rs | 18 ++++++++++++++++++ challenge-250/zapwai/rust/ch-2.rs | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 challenge-250/zapwai/rust/ch-1.rs create mode 100644 challenge-250/zapwai/rust/ch-2.rs diff --git a/challenge-250/zapwai/rust/ch-1.rs b/challenge-250/zapwai/rust/ch-1.rs new file mode 100644 index 0000000000..5aaa43811e --- /dev/null +++ b/challenge-250/zapwai/rust/ch-1.rs @@ -0,0 +1,18 @@ +fn main() { + let v = vec![0,1,2]; + let v2 = vec![4,3,2,1]; + proc(v); + proc(v2); +} + +fn proc(v :Vec) { + println!("Input: ints = {:?}", v); + let mut max :i32 = -1; + for i in 0 .. v.len() { + if i%10==v[i] { + max = i as i32; + break; + } + } + println!("Output: {max}"); +} diff --git a/challenge-250/zapwai/rust/ch-2.rs b/challenge-250/zapwai/rust/ch-2.rs new file mode 100644 index 0000000000..834662da3b --- /dev/null +++ b/challenge-250/zapwai/rust/ch-2.rs @@ -0,0 +1,36 @@ +fn main() { + let t = vec!["perl", "2", "000", "python", "r4ku"]; + let t2 = vec!["001", "1", "000", "0001"]; + proc(t); + proc(t2); +} + +fn proc(t :Vec<&str>) { + let mut max :i32 = 0; + for i in t.iter() { + let c = val(i); + if max < c { + max = c; + } + } + println!("Input: str = {:?}",t); + println!("Output: {max}"); +} + +fn val(i :&str) -> i32 { + return if is_num(i) { + i.parse::().unwrap() + } else { + i.len() as i32 + }; +} + +fn is_num (i :&str) -> bool { + let mut cnt = 0; + for c in i.chars() { + if c.is_digit(10) { + cnt += 1; + } + } + return if cnt == i.len() {true} else {false}; +} -- cgit