Question:
Write a program which takes 2 digits, X,Y as input and generates a 2-dimensional array. The element value in the i-th row and j-th column of the array should be i*j.Note: i=0,1…. X-1; j=0,1…,Y-1.
Also input is in a comma-separated form.
Program:
1 2 3 4 5 6 7 8 9 10 |
input_str = raw_input() dimensions=[int(x) for x in input_str.split(',')] rowNum=dimensions[0] colNum=dimensions[1] multilist = [[0 for col in range(colNum)] for row in range(rowNum)] for row in range(rowNum): for col in range(colNum): multilist[row][col]= row*col print multilist |
Explanation:
Range function generates a list of numbers, which is generally used to iterate over with for loops.
Output:
1 2 |
3,5 [[0, 0, 0, 0, 0], [0, 1, 2, 3, 4], [0, 2, 4, 6, 8]] |
Leave a Reply