Docsity

CS 106A Practice Midterm 3, Exams of Nursing

Toggle sidebar
bg1
CS 106A Practice Midterm 3
Course: CS 106A — Programming Methodology
Exam Type: Practice Midterm 3
Total Points: 120
Suggested Time: 120 minutes
Coverage: Karel, Python fundamentals, control flow, functions, strings, lists, 2-D lists,
and image processing
This exam follows the standard CS 106A midterm structure: Karel, a complete Python
program, lists of lists, images, and strings. Stanford’s midterm preparation materials
identify images/pixels, nested loops, 2-D grids, strings, loops, and simple lists as
principal exam topics.
Table of Contents
1. Instructions
2. Problem 1 — Karel the Robot
3. Problem 2 — Complete Python Program
4. Problem 3 — 2-D Lists
5. Problem 4 — Image Processing
6. Problem 5 — Strings
7. Problem 6 — Code Analysis
8. Reference Solutions
Instructions
Write complete Python 3 or Karel code where requested.
You may assume standard CS 106A Karel commands.
Do not modify an input list, string, or image unless the problem explicitly requires in-
place modification.
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download CS 106A Practice Midterm 3 and more Exams Nursing in PDF only on Docsity!

CS 106A Practice Midterm 3

Course: CS 106A — Programming Methodology

Exam Type: Practice Midterm 3

Total Points: 120

Suggested Time: 120 minutes

Coverage: Karel, Python fundamentals, control flow, functions, strings, lists, 2-D lists,

and image processing

This exam follows the standard CS 106A midterm structure: Karel, a complete Python

program, lists of lists, images, and strings. Stanford’s midterm preparation materials

identify images/pixels, nested loops, 2-D grids, strings, loops, and simple lists as

principal exam topics.

Table of Contents

1. Instructions

2. Problem 1 — Karel the Robot

3. Problem 2 — Complete Python Program

4. Problem 3 — 2-D Lists

5. Problem 4 — Image Processing

6. Problem 5 — Strings

7. Problem 6 — Code Analysis

8. Reference Solutions

Instructions

 Write complete Python 3 or Karel code where requested.

 You may assume standard CS 106A Karel commands.

 Do not modify an input list, string, or image unless the problem explicitly requires in-

place modification.

 Helper functions are allowed.

 Point values indicate approximate suggested time.

Problem 1 — Karel the Robot

20 points

Write a Karel program that makes Karel place beepers in a checkerboard pattern

along the bottom row.

Assume:

 Karel starts in the bottom-left corner.

 Karel begins facing East.

 The row may have any length.

 Karel has an unlimited number of beepers.

 Karel should place a beeper on the first square, skip the second square, place a beeper

on the third square, and continue this pattern until reaching the wall.

Example for a 6-square row:

text B. B. B.

where B means a beeper is present and. means no beeper.

python from karel.stanfordkarel import * def main(): # Your code here pass if name == "main": main() Problem 2 — Complete Python Program

25 points

Write a complete Python program that reads integers from the user until the user enters

]

reverse_rows(grid) should return:

python [ [7, 8, 9], [4, 5, 6], [1, 2, 3] ]

Your function must not modify the original grid.

Problem 4 — Image Processing

30 points

An image is represented as a 2-D list of pixels. Each pixel is a list of three integers:

python [r, g, b]

Write a function:

python def remove_blue(img):

The function should return a new image of the same size in which the blue channel of

every pixel is set to 0.

The red and green channels should remain unchanged.

Example:

python img = [ [[100, 50, 30], [20, 40, 60]] ]

remove_blue(img) should return:

python [ [[100, 50, 0], [20, 40, 0]] ]

Your function must not modify the original image.

Problem 5 — Strings

20 points

Write a function:

python def capitalize_words(s):

The function takes a string s containing one or more words separated by single spaces.

It returns a new string in which the first letter of every word is uppercase.

All other characters should remain unchanged.

Examples:

python capitalize_words("hello world") # returns "Hello World" capitalize_words("cs 106a is fun") # returns "Cs 106a Is Fun" capitalize_words("python") # returns "Python"

You may not use the built-in title() method.

Problem 6 — Code Analysis

20 points

Consider the following code:

python def process(values): result = [] for i in range(len(values)): if i == 0 or i == len(values) - 1: result.append(values[i]) else : result.append(values[i] * 2) return result print (process([3, 4, 5, 6, 7])) Part A — 10 points

What exact output does this program produce?

Part B — 10 points

In one or two sentences, explain what process does for any nonempty list of integers.

negative_sum += value print () print ("Positive count:", positive_count) print ("Negative count:", negative_count) print ("Positive sum:", positive_sum) print ("Negative sum:", negative_sum) if name == "main": main()

Why this works: The sentinel-controlled loop stops when the user enters 0 , so the

zero is neither counted nor summed. The if/elif structure separates positive and

negative values cleanly.

Problem 3 — Reverse Rows python def reverse_rows(grid): result = [] for i in range(len(grid) - 1, -1, -1): result.append(grid[i]) return result

Why this works: The loop starts at the final row index and moves backward to index

0 , appending each original row to a new list. Because the rows themselves are appended

without copying or changing their contents, their internal order remains unchanged.

This matches CS 106A’s emphasis on constructing new 2-D list results without mutating

inputs.

Problem 4 — Remove Blue python def remove_blue(img): result = [] for row in img: new_row = [] for pixel in row: r = pixel[0] g = pixel[1] new_row.append([r, g, 0])

result.append(new_row) return result

Why this works: The function creates a new image structure rather than changing

img. It preserves red and green values while replacing each blue value with 0. Nested

loops over rows and pixels reflect the image-processing portion of the CS 106A midterm

scope.

Problem 5 — Capitalize Words python def capitalize_words(s): result = "" capitalize_next = True for ch in s: if capitalize_next and ch.isalpha(): result += ch.upper() capitalize_next = False else : result += ch if ch == " ": capitalize_next = True return result

Why this works: The Boolean flag becomes True after every space, so the next

alphabetic character is capitalized. Non-letter characters are copied unchanged, and

later characters in each word remain unchanged. This avoids title() while using

standard string traversal and character methods listed in CS 106A exam references.

Problem 6 — Code Analysis Part A text [3, 8, 10, 12, 7] Part B