Skip to content

Instantly share code, notes, and snippets.

@nixiz
Last active August 24, 2022 14:05
Show Gist options
  • Save nixiz/c82e01f72652932f9239ec27722bd5ab to your computer and use it in GitHub Desktop.
Save nixiz/c82e01f72652932f9239ec27722bd5ab to your computer and use it in GitHub Desktop.
Aşk uyumu hesaplayıcı
use std::collections::HashSet;
use itertools::Itertools;
use itertools::EitherOrBoth::{Both, Left, Right};
type Score = usize;
fn main() {
let score = calculate("Selçuk", "Evin");
println!("score: {}", score);
}
fn calculate_score(scores: &Vec<usize>) -> Score {
match scores.len() {
0 => 0,
1 => scores[0],
2 => (scores[0] * 10) + scores[1],
_ => {
let mid = if scores.len() % 2 == 0 {scores.len() / 2} else {(scores.len() / 2) + 1};
let (first, last) = scores.split_at(mid);
println!("first: {:?} , last: {:?}", first, last);
let mut transformed_scores = Vec::new();
for it in first.iter().zip_longest(last.iter().rev()) {
match it {
Both(x, y) => {
let new_score = x + y;
if new_score >=10 {
transformed_scores.push(new_score / 10);
transformed_scores.push(new_score % 10);
} else {
transformed_scores.push(new_score);
}
},
Left(x) => transformed_scores.push(*x),
Right(_) => panic!("this shouldn't be happened!"),
}
}
println!("scores: {:?}", transformed_scores);
return calculate_score(&transformed_scores);
}
}
}
fn calculate(name1: &str, name2: &str) -> Score {
let concatenated = format!("{}{}", name1.to_lowercase(), name2.to_lowercase());
println!("str: {}", concatenated);
let mut index =0;
let mut occurrences : Vec<(char, usize)> = Vec::new();
concatenated.chars().for_each(|c| {
let count = concatenated.chars().skip(index).filter(|&x| x == c).count();
occurrences.push((c, count));
index += 1;
});
println!("occurrences without removed duplicates: {:?}", occurrences);
let mut uniques = HashSet::new();
occurrences.retain(|(e, _)| uniques.insert(e.clone()));
println!("occurrences after removed duplicates: {:?}", occurrences);
let scores : Vec<usize> = occurrences.into_iter().map(|(_, s)| s).collect();
println!("scores: {:?}", scores);
return calculate_score(&scores)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment