6.3.5 Cmu Cs Academy May 2026

def alternating_colors(rows, cols): grid = [] for r in range(rows): current_row = [] for c in range(cols): if (r + c) % 2 == 0: current_row.append('red') else: current_row.append('blue') grid.append(current_row) return grid When tackling 6.3.5, students often make the following mistakes: 1. Mismatched Indentation Error: Placing the grid.append(current_row) inside the inner loop instead of the outer loop. Result: A grid where each row has only one element. Fix: Ensure grid.append(current_row) is aligned with the for r loop, not the for c loop. 2. Off-by-One Errors in Range Error: Using range(rows-1) or range(1, rows) . Result: Missing the first or last row/column. Fix: Always use range(rows) and range(cols) for full coverage. 3. Hardcoding Values Error: Writing if r % 2 == 0 and c % 2 == 0 but forgetting the mixed parity cells. Result: Only corners or specific cells become red; the rest are incorrect. Fix: Use the (r + c) % 2 == 0 pattern—it's mathematically robust for alternating checks. 4. Forgetting to Return the Grid Error: The function builds grid but does not return it. Result: The function returns None (since no return statement exists). Fix: Add return grid as the last line of the function. Visualizing the Output in CMU Graphics One of the strengths of CMU CS Academy is that you can immediately visualize your grid. To test 6.3.5 within the CMU environment, you would add drawing code:

for r in range(rows): new_row = [] # Inner loop will go here grid.append(new_row) For each row, the inner loop iterates over each column number from 0 to cols - 1 . Inside, we check if (r + c) % 2 == 0 to decide the color. 6.3.5 Cmu Cs Academy

Remember: the solution is not just about typing if (r+c)%2==0 . It’s about understanding the . Once you internalize that, no grid-based problem will intimidate you. def alternating_colors(rows, cols): grid = [] for r