Lab

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.

Hide code cell source

### Solution
nums = [5, 2, 8, 1, 9, 3]

def find_min_max(nums):
    current_min = nums[0]
    current_max = nums[0]
    for n in nums[1:]:
        if n < current_min:
            current_min = n
        if n > current_max:
            current_max = n
    return current_min, current_max

low, high = find_min_max(nums)
print("Minimum:", low)
print("Maximum:", high)
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.

Hide code cell source

### Solution
sorted_nums = [3, 8, 15, 22, 34, 47, 59, 68, 71, 90]

def binary_search(sorted_nums, target):
    low, high = 0, len(sorted_nums) - 1
    while low <= high:
        mid = (low + high) // 2
        if sorted_nums[mid] == target:
            return mid
        elif sorted_nums[mid] < target:
            low = mid + 1
        else:
            high = mid - 1
    return -1

print(binary_search(sorted_nums, 47))
print(binary_search(sorted_nums, 10))
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.

Hide code cell source

### Solution
import bisect

scores = [55, 62, 74, 81, 88, 95]
new_score = 79

position = bisect.bisect_left(scores, new_score)
bisect.insort(scores, new_score)

print("Insert position:", position)
print("Updated scores:", scores)
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.

Hide code cell source

### Solution
data = [8, 3, 9, 1, 6]

def insertion_sort(data):
    items = data[:]
    for i in range(1, len(items)):
        current = items[i]
        j = i - 1
        while j >= 0 and items[j] > current:
            items[j + 1] = items[j]
            j -= 1
        items[j + 1] = current
    return items

sorted_data = insertion_sort(data)
print("Sorted:", sorted_data)
print("Original unchanged:", data)
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.

Hide code cell source

### Solution
data = [12, 45, 7, 23, 56, 89, 34]

def linear_search_count(data, target):
    comparisons = 0
    for i, value in enumerate(data):
        comparisons += 1
        if value == target:
            return i, comparisons
    return -1, comparisons

print("Target 23:", linear_search_count(data, 23))
print("Target 100:", linear_search_count(data, 100))
Target 23: (3, 4)
Target 100: (-1, 7)