main.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import select_kth
  2. # Python baseline function that works in PikaPython environment
  3. def py_select_kth(arr, k):
  4. """Baseline implementation using simple sorting"""
  5. if len(arr) == 0:
  6. return None
  7. if k < 0 or k >= len(arr):
  8. return None
  9. # Manual sorting since PikaPython may not support sorted()
  10. sorted_arr = []
  11. for i in range(len(arr)):
  12. sorted_arr.append(arr[i])
  13. # Simple bubble sort (not efficient but works in restricted environment)
  14. for i in range(len(sorted_arr)):
  15. for j in range(i + 1, len(sorted_arr)):
  16. if sorted_arr[i] > sorted_arr[j]:
  17. temp = sorted_arr[i]
  18. sorted_arr[i] = sorted_arr[j]
  19. sorted_arr[j] = temp
  20. return sorted_arr[k]
  21. # Test with first dataset
  22. data1 = [3, 1, 5, 9, 2]
  23. print("[EXAMPLE] Testing with data1:", data1)
  24. # Test k=0 (smallest)
  25. k = 0
  26. py_result1 = py_select_kth(data1, k)
  27. c_result1 = select_kth.SelectKth().select_kth(data1, k)
  28. print("[EXAMPLE] k=", k, "Python:", py_result1, "C module:", c_result1)
  29. assert py_result1 == c_result1, "Test 1 failed: k=0"
  30. # Test k=2 (median)
  31. k = 2
  32. py_result2 = py_select_kth(data1, k)
  33. c_result2 = select_kth.SelectKth().select_kth(data1, k)
  34. print("[EXAMPLE] k=", k, "Python:", py_result2, "C module:", c_result2)
  35. assert py_result2 == c_result2, "Test 2 failed: k=2"
  36. # Test k=4 (largest)
  37. k = 4
  38. py_result3 = py_select_kth(data1, k)
  39. c_result3 = select_kth.SelectKth().select_kth(data1, k)
  40. print("[EXAMPLE] k=", k, "Python:", py_result3, "C module:", c_result3)
  41. assert py_result3 == c_result3, "Test 3 failed: k=4"
  42. # Test with second dataset
  43. data2 = [10, 20, 30, 5, 15]
  44. print("[EXAMPLE] Testing with data2:", data2)
  45. # Test k=0 (smallest)
  46. k = 0
  47. py_result4 = py_select_kth(data2, k)
  48. c_result4 = select_kth.SelectKth().select_kth(data2, k)
  49. print("[EXAMPLE] k=", k, "Python:", py_result4, "C module:", c_result4)
  50. assert py_result4 == c_result4, "Test 4 failed: k=0"
  51. # Test k=2 (median)
  52. k = 2
  53. py_result5 = py_select_kth(data2, k)
  54. c_result5 = select_kth.SelectKth().select_kth(data2, k)
  55. print("[EXAMPLE] k=", k, "Python:", py_result5, "C module:", c_result5)
  56. assert py_result5 == c_result5, "Test 5 failed: k=2"
  57. # Test k=4 (largest)
  58. k = 4
  59. py_result6 = py_select_kth(data2, k)
  60. c_result6 = select_kth.SelectKth().select_kth(data2, k)
  61. print("[EXAMPLE] k=", k, "Python:", py_result6, "C module:", c_result6)
  62. assert py_result6 == c_result6, "Test 6 failed: k=4"
  63. # Test edge cases
  64. print("[EXAMPLE] Testing edge cases")
  65. # Empty list
  66. empty_list = []
  67. py_empty = py_select_kth(empty_list, 0)
  68. c_empty = select_kth.SelectKth().select_kth(empty_list, 0)
  69. print("[EXAMPLE] Empty list - Python:", py_empty, "C module:", c_empty)
  70. assert py_empty is None, "Empty list Python should return None"
  71. assert c_empty is None, "Empty list C module should return None"
  72. # k out of range
  73. py_out_of_range = py_select_kth(data1, 10)
  74. c_out_of_range = select_kth.SelectKth().select_kth(data1, 10)
  75. print("[EXAMPLE] k out of range - Python:", py_out_of_range, "C module:", c_out_of_range)
  76. assert py_out_of_range is None, "Out of range Python should return None"
  77. assert c_out_of_range is None, "Out of range C module should return None"
  78. # Single element list
  79. single = [42]
  80. py_single = py_select_kth(single, 0)
  81. c_single = select_kth.SelectKth().select_kth(single, 0)
  82. print("[EXAMPLE] Single element - Python:", py_single, "C module:", c_single)
  83. assert py_single == c_single, "Single element test failed"
  84. # Duplicate elements
  85. duplicates = [5, 5, 5, 5, 5]
  86. py_dup = py_select_kth(duplicates, 2)
  87. c_dup = select_kth.SelectKth().select_kth(duplicates, 2)
  88. print("[EXAMPLE] Duplicates k=2 - Python:", py_dup, "C module:", c_dup)
  89. assert py_dup == c_dup, "Duplicates test failed"
  90. print("[EXAMPLE] All functional tests passed!")
  91. # Performance test (only if functional tests pass)
  92. print("[EXAMPLE] Starting performance tests...")
  93. import time
  94. # Larger dataset for performance testing
  95. large_data = []
  96. for i in range(100):
  97. large_data.append(100 - i) # Reverse sorted to make it challenging
  98. ITER = 1000
  99. # Time Python baseline
  100. py_start = time.time()
  101. for _ in range(ITER):
  102. result_py = py_select_kth(large_data, 50)
  103. py_end = time.time()
  104. py_total = py_end - py_start
  105. py_mean = py_total / ITER
  106. print("[PERF] python_total:", py_total, "seconds")
  107. print("[PERF] python_mean:", py_mean, "seconds per call")
  108. # Time C module
  109. c_start = time.time()
  110. for _ in range(ITER):
  111. result_c = select_kth.SelectKth().select_kth(large_data, 50)
  112. c_end = time.time()
  113. c_total = c_end - c_start
  114. c_mean = c_total / ITER
  115. print("[PERF] cmod_total:", c_total, "seconds")
  116. print("[PERF] cmod_mean:", c_mean, "seconds per call")
  117. speedup = py_mean / c_mean if c_mean > 0 else float('inf')
  118. print("[PERF] speedup:", speedup, "x")
  119. # Verify performance test results match
  120. assert result_py == result_c, "Performance test results mismatch"
  121. print("[EXAMPLE][SELFTEST] OK - All tests passed successfully!")