Guest User

Untitled

a guest
Oct 11th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.01 KB | None | 0 0
  1. const OP_TABLE: [[usize; 10]; 10] = [
  2. [0, 3, 1, 7, 5, 9, 8, 6, 4, 2],
  3. [7, 0, 9, 2, 1, 5, 4, 8, 6, 3],
  4. [4, 2, 0, 6, 8, 7, 1, 3, 5, 9],
  5. [1, 7, 5, 0, 9, 8, 3, 4, 2, 6],
  6. [6, 1, 2, 3, 0, 4, 5, 9, 7, 8],
  7. [3, 6, 7, 4, 2, 0, 9, 5, 8, 1],
  8. [5, 8, 6, 9, 7, 2, 0, 1, 3, 4],
  9. [8, 9, 4, 5, 3, 6, 2, 0, 1, 7],
  10. [9, 4, 3, 8, 6, 1, 7, 2, 0, 5],
  11. [2, 5, 8, 1, 4, 3, 6, 7, 9, 0]
  12. ];
  13.  
  14. /// Calculates the check digit for a given ID
  15. fn check_digit(input: &[usize]) -> usize {
  16. let mut interim = 0;
  17. for &digit in input.iter() {
  18. interim = OP_TABLE[interim][digit];
  19. }
  20. interim
  21. }
  22.  
  23. /// Is an ID valid?
  24. pub fn verify(id: &[usize]) -> bool {
  25. check_digit(id) == 0
  26. }
  27.  
  28. /// Append a checksum to an existing ID
  29. pub fn id(mut input: Vec<usize>) -> Vec<usize> {
  30. let id = check_digit(&input);
  31. input.push(id);
  32. input
  33. }
  34.  
  35. fn main() {
  36. let mut id = vec![ 7, 8, 9, 3, 5, 7, 2 ];
  37. let sum = check_digit(&id);
  38. id.push(sum);
  39. //println!("{:?}, {}", id, );
  40. println!("{}", verify(&id));
  41. }
Add Comment
Please, Sign In to add comment