AD1

Wednesday, 30 May 2018

Side Diagonal

Statement

Given an integer n, create a two-dimensional array of size (n×n) and populate it as follows, with spaces between each character:
  • The positions on the minor diagonal (from the upper right to the lower left corner) receive 1 .
  • The positions above this diagonal recieve 0 .
  • The positions below the diagonal receive 2 .
Print the elements of the resulting array

Solution

#Problem : https://snakify.org/lessons/two_dimensional_lists_arrays/problems/secondary_diagonal/
n = int(input())
a=[[0 for x in range(n)]for y in range(n)]
for i in range(n):
for j in range(n):
a[i][n-i-1]=1
for y in range (n-i,n):
a[i][y]=2
print(a[i][j],end=" ")
print('\n')
 

Friday, 11 May 2018

The diagonal parallel to the main

Statement

Given an integer n, produce a two-dimensional array of size and complete it according to the following rules, and print with a single space between characters:
  • On the main diagonal write .
  • On the diagonals adjacent to the main, write .
  • On the next adjacent diagonals write and so forth.
Print the elements of the resulting array.
sample 
n=7
0 1 2 3 4 5 6
1 0 1 2 3 4 5
2 1 0 1 2 3 4
3 2 1 0 1 2 3
4 3 2 1 0 1 2
5 4 3 2 1 0 1
6 5 4 3 2 1 0
n= int(input())
b=[[abs(x-y) for x in range(n)]for y in range(n)]
for i in range(n):
for j in range(n):
print(b[i][j],end=" ")
print('\n')
view raw diagonal.py hosted with ❤ by GitHub

Chess Board

Given two numbers  and . Create a two-dimensional array of size and populate it with the characters "." and "*" in a checkerboard pattern. The top left corner should have the character "." 


above figure shows the main chess board pattern, note that "." means white and "*" means black

n,m = map(int,input().split())
b=[['. ' for x in range(m)]for y in range(n)]
for i in range(n):
for j in range(m):
if(i%2==0 and j%2!=0):
b[i][j]='* '
elif(i%2!=0 and j%2==0):
b[i][j]='* '
for i in range(n):
for j in range(m):
print(b[i][j],end="")
print('\n')
view raw chess board.py hosted with ❤ by GitHub