Docsity

CS 106A Practice Midterm 2, Exams of Nursing

Toggle sidebar
bg1
CS 106A Practice Midterm 2
Course: CS 106A — Programming Methodology
Exam Type: Practice Midterm 2
Total Points: 120
Suggested Time: 120 minutes
Coverage: Karel, Python control flow, functions, strings, lists, 2-D lists, and image
processing
This practice exam uses the standard CS 106A midterm problem categories—Karel, a
complete Python program, lists of lists, images, and strings—and is designed to be
comparable in format and difficulty to official practice exams without reproducing their
content. Stanford’s midterm materials identify images/pixels, nested loops, 2-D grids,
strings, loops, and simple lists as central 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, syntactically valid Python or Karel code.
Assume Python 3 and standard CS 106A Karel commands.
Do not modify an input data structure unless the problem explicitly asks for in-place
modification.
You may define helper functions when helpful.
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

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

CS 106A Practice Midterm 2

Course: CS 106A — Programming Methodology

Exam Type: Practice Midterm 2

Total Points: 120

Suggested Time: 120 minutes

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

processing

This practice exam uses the standard CS 106A midterm problem categories—Karel, a

complete Python program, lists of lists, images, and strings—and is designed to be

comparable in format and difficulty to official practice exams without reproducing their

content. Stanford’s midterm materials identify images/pixels, nested loops, 2-D grids,

strings, loops, and simple lists as central 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, syntactically valid Python or Karel code.

 Assume Python 3 and standard CS 106A Karel commands.

 Do not modify an input data structure unless the problem explicitly asks for in-place

modification.

 You may define helper functions when helpful.

 Point values indicate approximate suggested time.

Problem 1 — Karel the Robot

20 points

Write a Karel program that creates a vertical line of beepers in the leftmost column of

the world.

Assume:

 Karel starts in the bottom-left corner.

 Karel begins facing East.

 The world has at least one row and one column.

 Karel has an unlimited number of beepers.

 The height of the world is unknown.

Your program should place one beeper on every square in the first column, from the

bottom row to the top row.

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 a sequence of words from the user, one per

line, until the user enters done.

After input ends, print:

 The total number of words entered.

 The longest word entered.

After the call:

python [ [1, 0, 0], [0, 1, 0], [0, 0, 1] ] 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 grayscale(img):

The function should return a new image of the same dimensions in which every pixel

is converted to grayscale.

For each pixel:

1. Compute the average of its red, green, and blue values using integer division.

2. Replace all three channels with that average.

3. Do not modify the original image.

Example:

python img = [ [[100, 50, 30]] ]

grayscale(img) should return:

python [ [[60, 60, 60]] ]

Problem 5 — Strings

20 points

Write a function:

python def remove_duplicates(s):

The function takes a string s and returns a new string that contains each character from

s in its original order, but removes any character that has already appeared earlier in

the string.

Examples:

python remove_duplicates("banana") # returns "ban" remove_duplicates("stanford") # returns "stanford" remove_duplicates("aabbcc") # returns "abc" Problem 6 — Code Analysis

20 points

Consider the following code:

python def transform(values): result = [] for i in range(len(values)): if values[i] % 2 == 0: result.append(values[i] // 2) else : result.append(values[i] + 1) return result print (transform([4, 7, 10, 3, 8])) Part A — 10 points

What exact output does this program produce?

Part B — 10 points

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

if name == "main": main()

Why this works: The program stores valid words in a list, then makes one pass to find

the first longest word and count words beginning with "a". Because the comparison

uses > rather than >=, the first word of maximum length is preserved.

Problem 3 — Main Diagonal python def make_diagonal(grid): n = len(grid) for i in range(n): grid[i][i] = 1

Why this works: In a square grid, the main diagonal contains exactly the positions

where the row and column indexes are equal. A single loop from 0 through n - 1

updates each diagonal cell in place. Stanford practice solutions use similar row-and-

column indexing for list-of-lists problems.

Problem 4 — Grayscale Image python def grayscale(img): result = [] for row in img: new_row = [] for pixel in row: average = (pixel[0] + pixel[1] + pixel[2]) // 3 new_row.append([average, average, average]) result.append(new_row) return result

Why this works: The function creates a new outer list and new pixel lists, preserving

the original image. For each pixel, it averages the RGB channels with integer division

and assigns that value to all three channels. Nested loops over image rows and pixels

match the documented CS 106A image-processing exam style.

Problem 5 — Remove Duplicates python def remove_duplicates(s): result = "" seen = "" for ch in s: if ch not in seen: result += ch seen += ch return result

Why this works: The seen string records characters already added to the result. Each

character is appended only the first time it appears, preserving original order. This

follows the common CS 106A pattern of building a new string while traversing an input

string.

Problem 6 — Code Analysis Part A text [2, 8, 5, 4, 4] Part B

The function returns a new list in which each even number is divided by 2 using integer

division, while each odd number is increased by 1.

Why this works: The code checks values[i] % 2 == 0 for even values. For example,

4 becomes 2 , 7 becomes 8 , 10 becomes 5 , 3 becomes 4 , and 8 becomes 4. This tests list

traversal, conditionals, integer division, and return-value construction—core CS 106A

midterm skills.