Docsity

CS 106A Practice Midterm 1, Exams of Nursing

Toggle sidebar
bg1
CS 106A Practice Midterm 1
Course: CS 106A — Programming Methodology
Exam Type: Practice Midterm 1
Total Points: 120
Suggested Time: 120 minutes
Coverage: Karel, Python fundamentals, control flow, functions, strings, lists, 2-D lists, and
image processing
This practice exam follows the documented CS 106A midterm scope, which runs from the
beginning of the course through 2-dimensional lists and emphasizes writing functions or short
code passages, analyzing code, and answering short conceptual questions. The problem style
below is modeled on the general structure of Stanford practice midterms—Karel, a complete
Python program, lists of lists, images, and strings—without reproducing the original exam
content.web.stanford+1
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 clear, complete Python or Karel code where requested.
You may assume standard CS 106A Karel commands and Python 3 syntax.
Unless a problem says otherwise, comments are optional but may help earn partial credit.
Do not modify an input list or image unless the problem specifically asks you to do so.
Point values roughly indicate the suggested number of minutes per problem.
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

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

CS 106A Practice Midterm 1

Course: CS 106A — Programming Methodology

Exam Type: Practice Midterm 1

Total Points: 120

Suggested Time: 120 minutes

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

image processing

This practice exam follows the documented CS 106A midterm scope, which runs from the

beginning of the course through 2-dimensional lists and emphasizes writing functions or short

code passages, analyzing code, and answering short conceptual questions. The problem style

below is modeled on the general structure of Stanford practice midterms—Karel, a complete

Python program, lists of lists, images, and strings—without reproducing the original exam

content.web.stanford+

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 clear, complete Python or Karel code where requested.

 You may assume standard CS 106A Karel commands and Python 3 syntax.

 Unless a problem says otherwise, comments are optional but may help earn partial credit.

 Do not modify an input list or image unless the problem specifically asks you to do so.

 Point values roughly indicate the suggested number of minutes per problem.

Problem 1 — Karel the Robot

20 points

Write a Karel program that makes Karel place exactly one beeper on every square in the first

row until Karel reaches a wall.

Assume:

 Karel starts in the bottom-left corner.

 Karel begins facing East.

 Karel has an infinite number of beepers.

 The row may have any length.

 Karel’s final position and direction do not matter.

Your program should place a beeper on the starting square and on every square before the wall.

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 repeatedly asks the user to enter integers. The user enters

-1 as a sentinel to stop.

After input ends, print:

 The sum of all entered values.

 The average , rounded to two decimal places.

 The smallest value entered.

You may assume the user enters at least one valid integer before -1.

Example:

text Enter an integer (-1 to stop): 8

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 darken_image(img):

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

pixel is reduced by 25%.

Rules:

 Do not modify the original image.

 Use integer division (//) when reducing each channel.

 Each new channel value should be calculated as:

new value=original value×3//4\text{new value} = \text{original value} \times 3 //

4new value=original value×3//

Example:

python img = [ [[100, 200, 40]] ]

darken_image(img) should return:

python [ [[75, 150, 30]] ] Problem 5 — Strings

20 points

Write a function:

python def remove_vowels(s):

The function takes a string s and returns a new string containing every character from s except

the vowels "a", "e", "i", "o", and "u".

You may assume all letters are lowercase.

Examples:

python remove_vowels("stanford") # returns "stnfrd" remove_vowels("python") # returns "pythn" remove_vowels("xyz") # returns "xyz" Problem 6 — Code Analysis

Short Answer — included in the 120-point total

Consider the following code:

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

What exact output does this program produce?

Part B — 10 points

In one or two sentences, explain what the mystery function does in general.

Reference Solutions Problem 1 — Karel python from karel.stanfordkarel import * def main(): put_beeper()

rows = len(grid) cols = len(grid[0]) for c in range(cols): grid[0][c] = 1 grid[rows - 1][c] = 1 for r in range(rows): grid[r][0] = 1 grid[r][cols - 1] = 1

Why this works: The first loop fills the top and bottom rows; the second fills the left and right

columns. Revisiting corner cells does not cause a problem because they are already set to 1. This

uses row-and-column indexing over a 2-D list, a core midterm topic.web.stanford+

Problem 4 — Darken Image python def darken_image(img): result = [] for row in img: new_row = [] for pixel in row: r = pixel[0] * 3 // 4 g = pixel[1] * 3 // 4 b = pixel[2] * 3 // 4 new_row.append([r, g, b]) result.append(new_row) return result

Why this works: The function builds an entirely new 2-D list rather than changing img. Each

RGB channel is independently reduced by 25% using integer division, as required. Image

processing with nested loops is part of the stated midterm coverage.web.stanford+

Problem 5 — Remove Vowels python def remove_vowels(s): result = "" vowels = "aeiou" for ch in s: if ch not in vowels: result += ch

return result

Why this works: The function traverses each character, keeps non-vowels, and returns a newly

constructed string. String traversal and filtering are standard CS 106A midterm

skills.web.stanford+

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

The function returns a new list in which values at even indexes are doubled, while values at odd

indexes remain unchanged.

Why this works: i % 2 == 0 identifies indexes 0, 2, and 4. Those values— 1 , 3 , and 5 —

become 2 , 6 , and 10 ; the values at indexes 1 and 3 remain 2 and 4.