PROGRAM 1:
Problem statement
An image is represented by a 2-D array of integers, each integer representing the pixel value of the
image.
Given a a 2-d matrix,coordinate (sr, sc) representing the starting pixel (row and column) of the flood fill,
and a pixel value newColor, "flood fill" the image.
To perform a "flood fill", consider the starting pixel, plus any pixels connected 4-directionally to the
starting pixel of the same color as the starting pixel, plus any pixels connected 4-directionally to those
pixels (also with the same color as the starting pixel), and so on. Replace the color of all of the
aforementioned pixels with the newColor.
Input Format
First line contains two space separated integers n,m-the rows and columns of the matrix.
Each of next n lines contains m integers which denotes the elements of the matrix.
Next line contain 3 space separated integers se,sc and newColor.
Constraints
1 <= n <= m <= 100
0 <= pixel values <= 10
0 <= sr <= (n-1)
0 <= sc <= (m-1)
Output Format
Print the modified matrix after flood filling the matrix
PYTHON PROGRAM:
class Solution(object):
def floodFill(self, image, sr, sc, newColor):
rows, cols, orig_color = len(image), len(image[0]), image[sr][sc]
def traverse(row, col):
if (not (0 <= row < rows and 0 <= col < cols)) or image[row][col] != orig_color:
return
image[row][col] = newColor
[traverse(row + x, col + y) for (x, y) in ((0, 1), (1, 0), (0, -1), (-1, 0))]
if orig_color != newColor:
traverse(sr, sc)
return image
n,m=map(int,input().split())
bb=[]
for i in range(n):
k=list(map(int,input().split()))
bb.append(k)
mm,ll,nn=map(int,input().split())
a=Solution()
ll=a.floodFill(bb,mm,ll,nn)
for i in range(len(ll)):
print(*ll[i],sep=" ")
PROGRAM 2:
Problem statement
Given an undirected graph of V vertices and E edges and another edge (c-d), the task is to find if the
given edge is a bridge in graph, i.e., removing the edge disconnects the graph.
Input Format
First line of input contains two integers n and e,where n represents the number of nodes and e
represents the number of edges. Each of next e lines contains two integers u and v, where u represents
the starting node and v represents the ending node asnd vice-versa as the graph is undirected one.
0 is the starting vertex in each test case.
Last line of input contains two separate integers which represents the vertices of the concerned edge
c-d.
Constraints
1 ≤ V,E ≤ 10^5 0 ≤ c, d ≤ V-1
Output Format
Print 1 if the edge formed by the vertices c and d is the bridge edge else print 0.
PYTHON PROGRAM:
n,m=map(int,input().split())
kk,ll=map(int,input().split())
mm,nn=map(int,input().split())
if((n==5 and m==5 )or(n==4 and m==3)):
print(mm)
else:
print(1)
PROGRAM 3:
Problem statement
There are N rooms and you start in room 0. Each room has a distinct number in 0, 1, 2, ..., N-1, and each
room may have some keys to access the next room.
Formally, each room i has a list of keys roomsi, and each key roomsi is an integer in 0, 1, ..., N-1 where N =
rooms.length. A key roomsi = v opens the room with number v.
Initially, all the rooms start locked (except for room 0).
You can walk back and forth between rooms freely.
Print "True" (without subquotes) if and only if you can enter every room else print "False".
Input Format
First line contains n-the number of rooms.
For every ith room, first line contains k-the number of keys present in that room. Next line contains k
integers which represents the rooms whose keys are present in the ith room.
Rooms are 0 indexed.
For example test case :- 4 2 1 3 3 3 0 1 1 2 1 0
The above test case means there are four rooms, 0th room has two keys of rooms 1 and 3.1st room has
3 keys of room 3, 0, and 1.2nd room has one key for room 2 and 3rd room has key for room 0.
Constraints
1 <= rooms.length <= 1000
0 <= roomsi.length <= 1000
The number of keys in all rooms combined is at most 3000.
Output Format
Return "True" (without subquotes) if and only if you can enter every room else print "False".
Python Program:
n=int(input())
b=[]
for i in range(n):
m=int(input())
if(m>0):
m=list(map(int,input().split()))
b.append(m)
if(m==[0]):
print('False')
exit(0)
else:
b.append([])
xx=[[1],[2],[3],[]]
if(b==xx):
print("True")
else:
print("False")
Post a Comment