





Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!






Problem 1 — Karel the Robot
python from karel.stanfordkarel import * def main(): # Your code here pass if name == "main": main() Problem 2 — Complete Python Program
text Enter an integer (-1 to stop): 8
python [r, g, b]
python def darken_image(img):
python img = [ [[100, 200, 40]] ]
python [ [[75, 150, 30]] ] Problem 5 — Strings
python def remove_vowels(s):
python remove_vowels("stanford") # returns "stnfrd" remove_vowels("python") # returns "pythn" remove_vowels("xyz") # returns "xyz" Problem 6 — Code Analysis
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
Part B — 10 points
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
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
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
Problem 6 — Code Analysis Part A text [2, 2, 6, 4, 10] Part B