Skip to content

Instantly share code, notes, and snippets.

@maczniak
Last active August 16, 2022 10:38
Show Gist options
  • Save maczniak/fc906f9260774394d13743141716aa4b to your computer and use it in GitHub Desktop.
Save maczniak/fc906f9260774394d13743141716aa4b to your computer and use it in GitHub Desktop.
use std::iter::zip;
use std::str::FromStr;
fn main() {
println!("Input racing car names with comma separation:");
let input_names = "pobi,crong,honux\n";
println!("{}", input_names);
let names : Vec<&str> = input_names.split(',').map(|w| w.trim()).collect();
if names.iter().any(|w| w.len() > 5) {
panic!("A racing car name must be less than 6 characters.");
}
println!("How many times do you try?");
let input_try = "5\n";
println!("{}", input_try);
let times = u32::from_str(input_try.trim()).expect("You should input a positive integer");
println!("Result:");
let distances = vec![0; names.len()];
let mut scores : Vec<(&str, u32)> = zip(names, distances).collect();
for _ in 0..times {
// scores.iter_mut().for_each(|s| if rand::random::<u32>() % 10 > 3 { s.1 += 1 });
for score in &mut scores {
if rand::random::<u32>() % 10 > 3 {
score.1 += 1
}
}
// scores.iter().for_each(|s| println!("{} : {}", s.0, "-".repeat(s.1 as usize)));
for score in &scores {
println!("{} : {}", score.0, "-".repeat(score.1 as usize));
}
println!("");
}
let max_distance = scores.iter().max_by(|x, y| x.1.cmp(&y.1)).unwrap().1;
let winners : Vec<&str> = scores.iter().filter(|s| s.1 == max_distance).map(|s| s.0).collect();
println!("Winner : {}", winners.join(", "));
}
use std::f32::consts::PI;
pub struct Degrees(pub f32);
pub struct Radians(pub f32);
impl Degrees {
pub fn new(angle: f32) -> Self {
Self(angle)
}
}
impl From<Degrees> for Radians {
fn from(d: Degrees) -> Self {
Self(d.0 * PI / 180.0)
}
}
fn main() {
let one_eighty_degrees = Degrees::new(180.0);
let one_eighty_radians : Radians = one_eighty_degrees.into();
println!("180 degrees in radians = {}", one_eighty_radians.0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment