id/email
password
forgot password | create account
about | help | code help+videos | done | prefs
CodingBat code practice

 

david.white@denison.edu cs112fall2015 > diagonal
prev  |  next  |  chance

Write a function that takes an integer n as a parameter, and computes an n × n grid (list of lists) called H in which all cells on the main diagonal contain a 1 and the rest of the cells contain a 0. For example, if n = 5, your function should compute the grid

[[1, 0, 0, 0, 0],
[0, 1, 0, 0, 0],
[0, 0, 1, 0, 0],
[0, 0, 0, 1, 0],
[0, 0, 0, 0, 1]]

diagonal(3) → [[1, 0, 0], [0, 1, 0], [0, 0, 1]]
diagonal(4) → [[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]
diagonal(0) → []

...Save, Compile, Run (ctrl-enter)

def diagonal(n):

Editor font size %:
Shorter output


Forget It! -- delete my code for this problem

Progress graphs:
 Your progress graph for this problem
 Random user progress graph for this problem
 Random Epic Progress Graph

Python Help

Copyright Nick Parlante 2017 - privacy