fork download
  1. from collections import Counter
  2.  
  3. def calculate_similarity_score(input_data):
  4. # Parse the input into two lists
  5. left_list = []
  6. right_list = []
  7.  
  8. for line in input_data.strip().split("\n"):
  9. left, right = map(int, line.split())
  10. left_list.append(left)
  11. right_list.append(right)
  12.  
  13. # Count occurrences in the right list
  14. right_counts = Counter(right_list)
  15.  
  16. # Calculate the similarity score
  17. similarity_score = 0
  18. for number in left_list:
  19. similarity_score += number * right_counts.get(number, 0)
  20.  
  21. return similarity_score
  22.  
  23.  
  24. # Example Input
  25. input_data = """
  26. 3 4
  27. 4 3
  28. 2 5
  29. 1 3
  30. 3 9
  31. 3 3
  32. """
  33.  
  34. # Calculate and print the result
  35. result = calculate_similarity_score(input_data)
  36. print("Similarity Score:", result)# your code goes here
Success #stdin #stdout 0.03s 9760KB
stdin
Standard input is empty
stdout
Similarity Score: 31