Last active
August 29, 2015 14:05
-
-
Save Denommus/0212d2f4e1a6c75463f8 to your computer and use it in GitHub Desktop.
Solution for another dojo at FPF
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::collections::HashMap; | |
use std::num::abs; | |
pub struct Preference { | |
name: String, | |
flavors: HashMap<String, int> | |
} | |
impl Preference { | |
pub fn compare_preference<'a>(&'a self, preferences: &'a[Preference]) -> Option<&'a str> { | |
let mut preference = None; | |
let mut menor_total = 10000; // such woowww | |
for pref in preferences.iter() { | |
let mut total = 0; | |
for (key, value) in self.flavors.iter(){ | |
total += abs(pref.flavors.get(key) - *value); | |
} | |
if menor_total > total { | |
preference = Some(pref); | |
menor_total = total; | |
} | |
} | |
preference.map(|result| result.name.as_slice()) | |
} | |
} | |
mod test { | |
use std::collections::HashMap; | |
use super::Preference; | |
#[test] | |
fn test_preference() { | |
let mut flavors = HashMap::new(); | |
flavors.insert(String::from_str("Mussarela"),3 ); | |
flavors.insert(String::from_str("Toscana"),5 ); | |
flavors.insert(String::from_str("Sertaneja"),1 ); | |
let fulano = Preference { | |
name: String::from_str("Fulano"), | |
flavors: flavors | |
}; | |
let mut flavors = HashMap::new(); | |
flavors.insert(String::from_str("Mussarela"),3 ); | |
flavors.insert(String::from_str("Toscana"),5); | |
flavors.insert(String::from_str("Sertaneja"),1); | |
let ciclano = Preference{ | |
name: String::from_str("ciclano"), | |
flavors: flavors | |
}; | |
let mut flavors = HashMap::new(); | |
flavors.insert(String::from_str("Mussarela"),1); | |
flavors.insert(String::from_str("Toscana"),1 ); | |
flavors.insert(String::from_str("Sertaneja"),1 ); | |
let beltrano = Preference{ | |
name: String::from_str("Beltrano"), | |
flavors: flavors | |
}; | |
let pessoas = vec![ciclano,beltrano]; | |
assert_eq!(fulano.compare_preference(pessoas.as_slice()), Some("ciclano")); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::collections::HashMap; | |
pub struct Preference { | |
name: String, | |
flavors: HashMap<String, int> | |
} | |
impl Preference { | |
pub fn compare_preference<'a>(&'a self, preferences: &'a[Preference]) | |
-> Option<&'a str> { | |
preferences.iter().min_by(|pref| self.flavors.iter() | |
.fold(0, |x, (key,value)| | |
x+(pref.flavors.get(key) - *value).abs())) | |
.map(|pref| pref.name.as_slice()) | |
} | |
} | |
mod test { | |
use std::collections::HashMap; | |
use super::Preference; | |
#[test] | |
fn test_preference() { | |
let mut flavors = HashMap::new(); | |
flavors.insert(String::from_str("Mussarela"), 3); | |
flavors.insert(String::from_str("Toscana"), 5); | |
flavors.insert(String::from_str("Sertaneja"), 1); | |
let fulano = Preference { | |
name: String::from_str("Fulano"), | |
flavors: flavors | |
}; | |
let mut flavors = HashMap::new(); | |
flavors.insert(String::from_str("Mussarela"), 4); | |
flavors.insert(String::from_str("Toscana"), 5); | |
flavors.insert(String::from_str("Sertaneja"), 1); | |
let ciclano = Preference{ | |
name: String::from_str("ciclano"), | |
flavors: flavors | |
}; | |
let mut flavors = HashMap::new(); | |
flavors.insert(String::from_str("Mussarela"), 1); | |
flavors.insert(String::from_str("Toscana"), 1); | |
flavors.insert(String::from_str("Sertaneja"), 1); | |
let beltrano = Preference{ | |
name: String::from_str("Beltrano"), | |
flavors: flavors | |
}; | |
let pessoas = vec![ciclano,beltrano]; | |
assert_eq!(fulano.compare_preference(pessoas.as_slice()), Some("ciclano")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment