SHARE
TWEET

Untitled

a guest Feb 22nd, 2019 65 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.     Kata:
  3.  
  4.     To participate in a prize draw each one gives his/her firstname.
  5.  
  6.     Each letter of a firstname has a value which is its rank in
  7.     the English alphabet. A and a have rank 1, B and b rank 2 and so on.
  8.  
  9.     The length of the firstname is added to the sum of these ranks
  10.     hence a number n. An array of random weights is linked to the firstnames
  11.     and each n is multiplied by its corresponding weight to get
  12.     what they call a winning number.
  13.  
  14.     Example: names: COLIN,AMANDBA,AMANDAB,CAROL,PauL,JOSEPH weights:
  15.     [1, 4, 4, 5, 2, 1]
  16.  
  17.     PAUL -> n = length of firstname + 16 + 1 + 21 + 12 = 4 + 50 -> 54
  18.     The weight associated with PAUL is 2 so Paul's winning number is
  19.     54 * 2 = 108.
  20.  
  21.     Now one can sort the firstnames in decreasing order of the winning numbers.
  22.     When two people have the same winning number sort them alphabetically by
  23.     their firstnames.
  24.  
  25.     #Task:
  26.  
  27.     parameters: st a string of firstnames, we an array of weights, n a rank
  28.  
  29.     return: the firstname of the participant whose rank is n
  30.     (ranks are numbered from 1)
  31.  
  32.     #Example: names: COLIN,AMANDBA,AMANDAB,CAROL,PauL,JOSEPH weights:
  33.     [1, 4, 4, 5, 2, 1] n: 4
  34.  
  35.     The function should return: PauL
  36.  
  37.     Note:
  38.     If st is empty return "No participants".
  39.  
  40.     If n is greater than the number of participants then return
  41.     "Not enough participants".
  42.  
  43.     See Examples Test Cases for more examples.
  44. */
  45.  
  46. use std::cmp;
  47.  
  48. fn rank(st: &str, we: Vec<i32>, n: usize) -> &str {
  49.     let mut people: Vec<(&str, i32)> = st.split(",").enumerate().map(|(i, name)| {
  50.         (name, name.bytes().fold(name.len() as u8, |mut acc, c| {
  51.             match c {
  52.                 b'a' ..= b'z' => acc + c - b'a' + 1,
  53.                 b'A' ..= b'Z' => acc + c - b'A' + 1,
  54.                 _ => acc,
  55.             }
  56.         }) as i32 * we[i])
  57.     }).collect();
  58.  
  59.     people.sort_by_key(|&(_, n)| cmp::Reverse(n));
  60.  
  61.     let (winner, _) = people[n - 1];
  62.  
  63.     winner
  64. }
  65.  
  66. fn testing(st: &str, we: Vec<i32>, n: usize, exp: &str) -> () {
  67.     assert_eq!(rank(st, we, n), exp)
  68. }
  69.  
  70. #[test]
  71. fn basics_rank() {
  72.     testing(
  73.         "Addison,Jayden,Sofia,Michael,Andrew,Lily,Benjamin",
  74.         vec![4, 2, 1, 4, 3, 1, 2],
  75.         4,
  76.         "Benjamin",
  77.     );
  78.     testing(
  79.         "Elijah,Chloe,Elizabeth,Matthew,Natalie,Jayden",
  80.         vec![1, 3, 5, 5, 3, 6],
  81.         2,
  82.         "Matthew",
  83.     );
  84.     testing(
  85.         "Aubrey,Olivai,Abigail,Chloe,Andrew,Elizabeth",
  86.         vec![3, 1, 4, 4, 3, 2],
  87.         4,
  88.         "Abigail",
  89.     );
  90.     testing(
  91.         "Lagon,Lily",
  92.         vec![1, 5],
  93.         2,
  94.         "Lagon",
  95.     );
  96. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top