From 02ff991bb0d5617a15974bda6ca78393c5d7cd07 Mon Sep 17 00:00:00 2001 From: nkoch001 Date: Sun, 20 Jan 2019 18:35:49 +0100 Subject: [PATCH] formatting improvements, more rustic --- rust/rabbits/src/main.rs | 61 ++++++++++++++++++++++++---------------- rust/tally/src/main.rs | 18 ++++++------ 2 files changed, 45 insertions(+), 34 deletions(-) diff --git a/rust/rabbits/src/main.rs b/rust/rabbits/src/main.rs index 2dc51f4..6745c07 100644 --- a/rust/rabbits/src/main.rs +++ b/rust/rabbits/src/main.rs @@ -1,36 +1,39 @@ // https://www.reddit.com/r/dailyprogrammer/comments/7s888w/20180122_challenge_348_easy_the_rabbit_problem/ -use std::{thread, time, io}; +use std::{io, thread, time}; -fn init(male: i128, female: i128, needed_alive: i128) -> ([i128; 96], [i128; 96], i128){ - +fn init(male: i128, female: i128, needed_alive: i128) -> ([i128; 96], [i128; 96], i128) { let mut m: [i128; 96] = [0; 96]; let mut f: [i128; 96] = [0; 96]; m[2] = male; f[2] = female; - return (m, f, needed_alive); + (m, f, needed_alive) } - // reproduce, by letting all females 4 months and over have 9 female and 5 male descendants // fn reproduce(m_pop: [i128; 96], f_pop: [i128; 96]) -> ([i128; 96], [i128;96], i128, i128) { -fn reproduce(m_pop: [i128; 96], f_pop: [i128; 96]) -> (i128, i128) { +fn reproduce(_m_pop: [i128; 96], f_pop: [i128; 96]) -> (i128, i128) { let mut res_m = 0; let mut res_f = 0; let f_iter = f_pop.iter().enumerate(); for (i, count) in f_iter { - if i < 95 && i > 3{ + if i < 95 && i > 3 { // res_f[0] = res_f[0] + count * 9; // res_m[0] = res_m[0] + count * 5; - res_m = res_m + count * 5; - res_f = res_f + count * 9; + res_m += count * 5; + res_f += count * 9; } } - return (res_m, res_f); + (res_m, res_f) } // age the bunny population by shifting the array one to the right -fn age(m_pop: [i128; 96], f_pop: [i128; 96], new_males: i128, new_females: i128) -> ([i128; 96], [i128; 96]){ +fn age( + m_pop: [i128; 96], + f_pop: [i128; 96], + new_males: i128, + new_females: i128, +) -> ([i128; 96], [i128; 96]) { let mut res_m: [i128; 96] = [0; 96]; let mut res_f: [i128; 96] = [0; 96]; @@ -40,20 +43,19 @@ fn age(m_pop: [i128; 96], f_pop: [i128; 96], new_males: i128, new_females: i128) let f_iter = f_pop.iter().enumerate(); let m_iter = m_pop.iter().enumerate(); for (i, count) in f_iter { - if i < 95{ - res_f[i+1] = *count; + if i < 95 { + res_f[i + 1] = *count; } } for (i, count) in m_iter { - if i < 95{ - res_m[i+1] = *count; + if i < 95 { + res_m[i + 1] = *count; } } - return (res_m, res_f); + (res_m, res_f) } fn main() { - // this whole part is just for reading from stdin... let mut input_m = String::new(); @@ -61,18 +63,24 @@ fn main() { let mut input_n = String::new(); println!("Enter the number of male rabbits to start with:"); - io::stdin().read_line(&mut input_m).expect("failed to read input"); + io::stdin() + .read_line(&mut input_m) + .expect("failed to read input"); println!("Enter the number of female rabbits to start with:"); - io::stdin().read_line(&mut input_f).expect("failed to read input"); + io::stdin() + .read_line(&mut input_f) + .expect("failed to read input"); println!("Enter the number of rabbits you want to reach:"); - io::stdin().read_line(&mut input_n).expect("failed to read input"); + io::stdin() + .read_line(&mut input_n) + .expect("failed to read input"); let trim_m = input_m.trim(); let trim_f = input_f.trim(); let trim_n = input_n.trim(); - let trims = vec!(trim_m, trim_f, trim_n); - let mut mf: Vec = vec!(10, 10, 10000); + let trims = vec![trim_m, trim_f, trim_n]; + let mut mf: Vec = vec![10, 10, 10000]; for (i, trim) in trims.iter().enumerate() { let g = &trim.parse::().unwrap_or(20); mf[i] = *g; @@ -86,7 +94,7 @@ fn main() { let mut sum = sum_male + sum_female; // println!("There are now {} rabbits alive!", sum); let mut i = 0; - while sum < n_a { + while sum < n_a { // this handling of tuples sucks, there's gotta be a better way... let (r_m, r_f) = reproduce(m, f); let (r_m, r_f) = age(m, f, r_m, r_f); @@ -95,9 +103,12 @@ fn main() { sum_male = m.iter().sum(); sum_female = f.iter().sum(); sum = sum_male + sum_female; - println!("There are now {} rabbits alive! {} males and {} females", sum, sum_male, sum_female); + println!( + "There are now {} rabbits alive! {} males and {} females", + sum, sum_male, sum_female + ); thread::sleep(time::Duration::from_millis(500)); i += 1; } - println!("It took {} months!", i); + println!("It took {} months!", i); } diff --git a/rust/tally/src/main.rs b/rust/tally/src/main.rs index 20d838c..6559890 100644 --- a/rust/tally/src/main.rs +++ b/rust/tally/src/main.rs @@ -4,7 +4,7 @@ fn main() { let input = "EbAAdbBEaBaaBBdAccbeebaec"; println!("Tallying {}", input); - let player_list = String::from(init_player_list(input.to_string())); + let player_list = init_player_list(input.to_string()); println!("These people are playing: {}", player_list); let tally = tally(player_list, input.to_string()); @@ -13,30 +13,30 @@ fn main() { } // get all distinct characters -fn init_player_list(input: String) -> String{ +fn init_player_list(input: String) -> String { let mut result = String::new(); let ch = input.chars(); - for cha in ch{ + for cha in ch { let lch = cha.to_lowercase().next().unwrap(); - if !result.contains(lch){ + if !result.contains(lch) { result.push(lch); } } - return result + result } // tally each players scores // lowercase => +1 point // uppercase => -1 point -fn tally(player_list: String, tally: String) -> Vec<(char, i32)>{ +fn tally(player_list: String, tally: String) -> Vec<(char, i32)> { let mut v: Vec<(char, i32)> = Vec::new(); for (_i, player) in player_list.chars().enumerate() { - let count = (tally.matches(player).count() as i32) - (tally.matches(player - .to_uppercase().next().unwrap()).count() as i32); + let count = (tally.matches(player).count() as i32) + - (tally.matches(player.to_uppercase().next().unwrap()).count() as i32); v.push((player, count)); } v.sort_by_key(|v| v.1); v.reverse(); - return v + v }