





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
python [ [1, 0, 0], [0, 1, 0], [0, 0, 1] ] Problem 4 — Image Processing
python [r, g, b]
python def grayscale(img):
python img = [ [[100, 50, 30]] ]
python [ [[60, 60, 60]] ]
Problem 5 — Strings
python def remove_duplicates(s):
python remove_duplicates("banana") # returns "ban" remove_duplicates("stanford") # returns "stanford" remove_duplicates("aabbcc") # returns "abc" Problem 6 — Code Analysis
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
Part B — 10 points
if name == "main": main()
Problem 3 — Main Diagonal python def make_diagonal(grid): n = len(grid) for i in range(n): grid[i][i] = 1
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
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
Problem 6 — Code Analysis Part A text [2, 8, 5, 4, 4] Part B