Python入门 lab3

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:

  1. Changed the initial value of total from 1 to 0 to ensure accurate calculation of the sum.
  2. Fixed a syntax error in the for loop by replacing k-1 with k to correctly iterate over every k-th number.
  3. Fix the issue with the accumulation of the total variable, so that the total variable is being accumulated in each iteration of the loop instead of being overwritten.
  4. Added a return statement to return the calculated sum.
  5. 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

相关推荐

  1. Lab 5: Python Lists, Trees

    2024-07-12 22:32:05       53 阅读
  2. Python学习入门3)—— 高级技巧

    2024-07-12 22:32:05       38 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-12 22:32:05       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-12 22:32:05       72 阅读
  3. 在Django里面运行非项目文件

    2024-07-12 22:32:05       58 阅读
  4. Python语言-面向对象

    2024-07-12 22:32:05       69 阅读

热门阅读

  1. 留学生需要注意所谓的“写作套路”

    2024-07-12 22:32:05       23 阅读
  2. C++ 项目实践课设 图书馆管理系统

    2024-07-12 22:32:05       17 阅读
  3. 计算机网络 5.6网桥与交换机

    2024-07-12 22:32:05       15 阅读
  4. 安全开发--多语言基础知识

    2024-07-12 22:32:05       18 阅读
  5. Requests库如何用于发送HTTP请求

    2024-07-12 22:32:05       24 阅读
  6. Spring MVC中Restful风格引入

    2024-07-12 22:32:05       18 阅读
  7. 【25届秋招备战C++】算法篇-排序算法合集

    2024-07-12 22:32:05       18 阅读
  8. 国道省道乡道见闻

    2024-07-12 22:32:05       22 阅读
  9. 解锁深度学习黑箱:注意力机制的神秘力量

    2024-07-12 22:32:05       22 阅读