Advertisement
Guest User

Untitled

a guest
Dec 3rd, 2021
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.76 KB | None | 0 0
  1. def andy(a, b):
  2. inverse_a = {}
  3. for k,v in a.items():
  4. inverse_a[v.lower()] = str(k)
  5. # ^ v remove these lower() calls for case sensitivity
  6. return {k: [inverse_a.get(item.lower(), item) for item in v] for k,v in b.items()}
  7.  
  8. #Andy K's test case
  9. a = {1: "Vue network", 2: "Blockbuster"}
  10. b = {1: ['Mann theater','LA','vue network'], 2 : ['Apollo Theatre', 'NY', 'Blockbuster']}
  11. expected_output = {1: ['Mann theater','LA','1'], 2 : ['Apollo Theatre', 'NY', '2']}
  12. assert andy(a,b) == expected_output
  13.  
  14.  
  15. #Kevin's test cases
  16. #basic usage
  17. assert andy(
  18. {1: "a", 2: "b"},
  19. {3: ["x", "y", "a"], 4: ["u", "v", "b"]}
  20. ) == {3: ["x", "y", "1"], 4: ["u", "v", "2"]}
  21.  
  22. #case insensitivity
  23. assert andy(
  24. {1: "a", 2: "b"},
  25. {3: ["X", "Y", "A"], 4: ["U", "V", "B"]}
  26. ) == {3: ["X", "Y", "1"], 4: ["U", "V", "2"]}
  27.  
  28. #if b has no matches, return a plain copy of b
  29. assert andy(
  30. {1: "foo", 2: "bar"},
  31. {3: ["x", "y", "a"], 4: ["u", "v", "b"]}
  32. ) == {3: ["x", "y", "a"], 4: ["u", "v", "b"]}
  33.  
  34. #if a is empty: same behavior as "b has no matches"
  35. assert andy(
  36. {},
  37. {3: ["x", "y", "a"], 4: ["u", "v", "b"]}
  38. ) == {3: ["x", "y", "a"], 4: ["u", "v", "b"]}
  39.  
  40. #replacements can occur at any point in the list
  41. assert andy(
  42. {1: "a", 2: "b"},
  43. {3: ["a", "y", "z"], 4: ["u", "b", "w"]}
  44. ) == {3: ["1", "y", "z"], 4: ["u", "2", "w"]}
  45.  
  46. #multiple replacements are possible
  47. assert andy(
  48. {1: "a", 2: "b"},
  49. {3: ["a", "a", "a"], 4: ["a", "b", "a"]}
  50. ) == {3: ["1", "1", "1"], 4: ["1", "2", "1"]}
  51.  
  52. #if a has duplicate values, ignore all but the last one
  53. assert andy(
  54. {1: "a", 2: "a"},
  55. {3: ["x", "y", "a"], 4: ["u", "v", "a"]}
  56. ) == {3: ["x", "y", "2"], 4: ["u", "v", "2"]}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement