Source Code:
def find_word_vertical(matrix,word):
ret_lst=[]
rows=len(matrix)
cols=len(matrix[0])
for i in range(0,rows):
temp=""
for j in range(0,cols):
temp=temp+matrix[j][i]
if(temp==word):
ret_lst.append(i)
return ret_lst
Strings=["cdb","aoi","tgg"]
matrix=[['c','d','b'],
['a','o','i'],
['t','g','g']]
print("Strings: ",Strings)
print("\nMatrix of characters:")
print(" ",end=" ")
for k in range(0,len(matrix[0])):
print(k,end=" ")
print()
for i in range(0,len(matrix)):
print(i,end=" ")
for j in range(0,len(matrix[0])):
print(matrix[i][j],end=" ")
print()
word=input("\nEnter the word to search for: ")
res=find_word_vertical(matrix,word)
print("The word is found in columns ",res)![1 def find word vertical(matrix, word): 2 ret ist=[] 3 rows=len (matrix) 4 cols=len (matrix[0]) 5 for i in range(0, rows) : 6](//img.homeworklib.com/questions/d4b8e350-6a47-11eb-9f49-854287baeb35.png?x-oss-process=image/resize,w_560)
Sample input and output:
![Strings: [cdb, aoi, tgg] Matrix of characters: 0 1 2 ocdb 1 a oi 2 tgg Enter the word to search for: dog The word is fo](//img.homeworklib.com/questions/d52092a0-6a47-11eb-9b9b-c79a05a1d4c3.png?x-oss-process=image/resize,w_560)
1 def find word vertical(matrix, word): 2 ret ist=[] 3 rows=len (matrix) 4 cols=len (matrix[0]) 5 for i in range(0, rows) : 6 temp="" 7 for j in range(0, cols): 8 temp=temp-matrix[j][i] 9 if(temp==word): 10 ret_lst.append(i) 11 return ret_lst 12 13 Strings=["cdb", "aoi","tgg"] 14 matrix=[['c','d', 'b'], 15 ['a','o', 'i'), 16 ['t','g','g']] 17 print("Strings: ", Strings) 18 print("\nMatrix of characters:") 19 print(" ", end=" ") 20 for k in range(0, len(matrix[0])): 21 print(k, end="") 22 print() for i in range(0, len(matrix)): 24 print(i,end=" ") for j in range(0, len(matrix[0])): 26 print(matrix[i][j], end=" ") 27 print() 28 word=input("\nEnter the word to search for: ") 29 res=find_word_vertical (matrix, word) 30 print("The word is found in columns ", res) 25
Strings: ['cdb', 'aoi', 'tgg'] Matrix of characters: 0 1 2 ocdb 1 a oi 2 tgg Enter the word to search for: dog The word is found in columns [1] (base)
[email protected]:-/Desktop
.