formatting improvements, more rustic

master
nkoch001 6 years ago
parent a61f0f7d29
commit 02ff991bb0
  1. 45
      rust/rabbits/src/main.rs
  2. 10
      rust/tally/src/main.rs

@ -1,20 +1,18 @@
// 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) {
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();
@ -22,15 +20,20 @@ fn reproduce(m_pop: [i128; 96], f_pop: [i128; 96]) -> (i128, i128) {
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];
@ -49,11 +52,10 @@ fn age(m_pop: [i128; 96], f_pop: [i128; 96], new_males: i128, new_females: i128)
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<i128> = vec!(10, 10, 10000);
let trims = vec![trim_m, trim_f, trim_n];
let mut mf: Vec<i128> = vec![10, 10, 10000];
for (i, trim) in trims.iter().enumerate() {
let g = &trim.parse::<i128>().unwrap_or(20);
mf[i] = *g;
@ -95,7 +103,10 @@ 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;
}

@ -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());
@ -23,7 +23,7 @@ fn init_player_list(input: String) -> String{
result.push(lch);
}
}
return result
result
}
// tally each players scores
@ -32,11 +32,11 @@ fn init_player_list(input: String) -> String{
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
}

Loading…
Cancel
Save