14.4.2. Lab#
Use this lab to practice the chapter’s coding skills in runnable Python code.
Sign in from the account menu before you start working on the lab.
Launch Live Code (the rocket) to write and run code in the coding cells.
Multiple submissions are allowed. The highest score is kept.
### Question 1: Find the Minimum and Maximum in One Pass
# Write find_min_max(nums) that scans the list once (a single for
# loop) and returns a tuple (minimum, maximum) WITHOUT using the
# built-in min() or max() functions.
# Call it on nums, unpack the result into low and high, and print
# exactly these two lines:
# Minimum: 1
# Maximum: 9
nums = [5, 2, 8, 1, 9, 3]
### Your code starts here.
### Your code ends here.
Minimum: 1
Maximum: 9
### Question 2: Binary Search on a New Sorted List
# Write binary_search(sorted_nums, target) that returns the index
# of target in sorted_nums, or -1 if it is not present, using the
# standard low/high/mid halving algorithm (O(log n)).
# Call binary_search(sorted_nums, 47), then binary_search(sorted_nums, 10),
# and print each result on its own line. Print exactly:
# 5
# -1
sorted_nums = [3, 8, 15, 22, 34, 47, 59, 68, 71, 90]
### Your code starts here.
### Your code ends here.
5
-1
### Question 3: Insert-and-Report with bisect
# scores is already sorted. Use bisect.bisect_left(scores, new_score)
# to find the index where new_score would be inserted to keep the
# list sorted, and store it in position.
# Then use bisect.insort(scores, new_score) to actually insert it
# in place.
# Print exactly these two lines:
# Insert position: 3
# Updated scores: [55, 62, 74, 79, 81, 88, 95]
import bisect
scores = [55, 62, 74, 81, 88, 95]
new_score = 79
### Your code starts here.
### Your code ends here.
Insert position: 3
Updated scores: [55, 62, 74, 79, 81, 88, 95]
### Question 4: Implement Insertion Sort
# Write insertion_sort(data) that returns a NEW sorted list using
# insertion sort logic: work on a copy (items = data[:]) so the
# original data list is never mutated.
# For each index i from 1 to the end, slide items[i] left past any
# larger items in the sorted portion until it reaches its place.
# Call insertion_sort(data), then print the result and print data
# again to show it is unchanged. Print exactly:
# Sorted: [1, 3, 6, 8, 9]
# Original unchanged: [8, 3, 9, 1, 6]
data = [8, 3, 9, 1, 6]
### Your code starts here.
### Your code ends here.
Sorted: [1, 3, 6, 8, 9]
Original unchanged: [8, 3, 9, 1, 6]
### Question 5: Count Comparisons in Linear Search
# Write linear_search_count(data, target) that scans data left to
# right, counting every comparison made (increment comparisons once
# per element checked). Return a tuple (index, comparisons); index
# is -1 if target is never found, and comparisons still reflects the
# full scan in that case.
# Call it for target 23, then for target 100, printing each result
# with a label. Print exactly these two lines:
# Target 23: (3, 4)
# Target 100: (-1, 7)
data = [12, 45, 7, 23, 56, 89, 34]
### Your code starts here.
### Your code ends here.
Target 23: (3, 4)
Target 100: (-1, 7)
Question 1
Question 2
Question 3
Question 4
Question 5