find_most_frequent.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. # data_analysis.py
  2. def find_most_frequent(data: list):
  3. """
  4. Finds the most frequent element in a list.
  5. If there is a tie, it returns the one that appeared first among the tied elements.
  6. Returns None if the list is empty.
  7. """
  8. if not data:
  9. return None
  10. # Step 1: Count occurrences of each item
  11. counts = {}
  12. for item in data:
  13. if item in counts:
  14. counts[item] += 1
  15. else:
  16. counts[item] = 1
  17. # Step 2: Find the item with the maximum count.
  18. # To handle ties by first appearance, we iterate through the original list.
  19. most_frequent_item = data[0]
  20. max_count = counts[most_frequent_item]
  21. for item in data:
  22. if counts[item] > max_count:
  23. max_count = counts[item]
  24. most_frequent_item = item
  25. return most_frequent_item
  26. if __name__ == '__main__':
  27. # Test case 1: Simple case with numbers
  28. data1 = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5]
  29. print("Data:", data1)
  30. print("Most frequent:", find_most_frequent(data1))
  31. print("-" * 20)
  32. # Test case 2: Case with strings
  33. data2 = ['apple', 'banana', 'apple', 'orange', 'banana', 'apple']
  34. print("Data:", data2)
  35. print("Most frequent:", find_most_frequent(data2))
  36. print("-" * 20)
  37. # Test case 3: Tie-breaking (should return 'a' because it appears first)
  38. data3 = ['a', 'b', 'c', 'a', 'b', 'c', 'a', 'b']
  39. print("Data:", data3)
  40. print("Most frequent:", find_most_frequent(data3))
  41. print("-" * 20)
  42. # Test case 4: Empty list
  43. data4 = []
  44. print("Data:", data4)
  45. print("Most frequent:", find_most_frequent(data4))
  46. print("-" * 20)
  47. # Test case 5: All unique elements
  48. data5 = [10, 20, 30, 40, 50]
  49. print("Data:", data5)
  50. print("Most frequent:", find_most_frequent(data5))
  51. print("-" * 20)