1 first_occurrence.py
1.1 code
use binary search to search for the index of the number
def first_occurrence(nums, threshold):
left = 0
right = len(nums) - 1
result = -1
while left <= right:
mid = (left + right) // 2
if nums[mid] >= threshold:
result = mid
right = mid - 1
else:
left = mid + 1
return result
1.2 test cases
# nums is an empty list
print("[], 2, expect: -1, get: ", first_occurrence([], 2))
# nums have multiple occurrences of threshold
print("[1,2,2,5,8], 2, expect: 1, get: ", first_occurrence([1,2,2,5,8], 2))
# all of the numbers in nums are smaller than threshold
print("[1,2,2,5,8], 100, expect: -1, get: ", first_occurrence([1,2,2,5,8], 100))
# there is at least one element in nums that is greater than or equal to threshold
print("[1,2,2,5,8], 4), expect: 3, get: ", first_occurrence([1,2,2,5,8], 4))
# there is only one element in nums
print("[2], 2, expect: 0, get: ", first_occurrence([2], 2))
# there exist negative numbers in nums or threshold
print("[-8,-5,-2,-2,-1,0,1], -4, expect: 2, get: ", first_occurrence([-8,-5,-2,-2,-1,0,1], -4))
running result:
- different arguments are tried as you can see in the code above
- every result matches the expectation
2 sum_every_kth.py
2.1 debug
'''
# original code
def sum_every_kth(start, end, k):
if k >=0 or start <= end:
return None
total = 1
for number in range(start, end, k-1)
total = number
'''
# modified code
def sum_every_kth(start, end, k):
if k >= 0 or start <= end:
return None
total = 0
for number in range(start, end, k):
total += number
return total
Fix the bugs:
- Changed the initial value of
total
from1
to0
to ensure accurate calculation of the sum. - Fixed a syntax error in the
for
loop by replacingk-1
withk
to correctly iterate over every k-th number. - Fix the issue with the accumulation of the
total
variable, so that thetotal
variable is being accumulated in each iteration of the loop instead of being overwritten. - Added a
return
statement to return the calculated sum. - Fixed the code’s syntax indentation issue to ensure proper usage of the
for
loop.
2.2 test cases
# ordinary cases (different values of start, end, k)
print("sum_every_kth(6, 3, -1), expect: 15, get: ", sum_every_kth(6, 3, -1))
print("sum_every_kth(6, 2, -1), expect: 18, get: ", sum_every_kth(6, 2, -1))
print("sum_every_kth(6, 4, -2), expect: 6, get: ", sum_every_kth(6, 4, -2))
# step size is larger than the distance between the starting value and the ending value
print("sum_every_kth(10, 4, -20), expect: 10, get: ", sum_every_kth(10, 4, -20))
# either start or end is negative
print("sum_every_kth(-5, -8, -2), expect: -12, get: ", sum_every_kth(-5, -8, -2))
# k = 0
print("sum_every_kth(2, 0, 0), expect: None, get: ", sum_every_kth(2, 0, 0))
# k > 0
print("sum_every_kth(6, 1, 2), expect: None, get: ", sum_every_kth(6, 1, 2))
# start = end
print("sum_every_kth(6, 6, -1), expect: None, get: ", sum_every_kth(6, 6, -1))
# start < end
print("sum_every_kth(0, 2, -1), expect: None, get: ", sum_every_kth(0, 2, -1))
running result:
- different arguments are tried as you can see in the code above
- every result matches the expectation