Answer to Question #295282 in Python for Sai

Question #295282

Consider a mx n matrix inmatrix of string elements containing alphanumeric values, where m>1, n >=1. Identify and print outarr based on the logic below:





• Traverse the inmatrix in clockwise spiral way starting from the element at (0,0) •





traversal until one of the two conditions is met • The element or the concatenated string is a palindrome with length greater





than 1 There are no more elements left for traversal





. Add the concatenated string to outarr





. If there is any element which has not yet been visited in the traversal, repeat the two steps, forming a new concatenated string that starts with the next element present after the last element previously traversed





Note: Perform case-sensitive comparison





Input format:





First line will contain number of rows m of inmatrix





The next m lines will contain the elements of inmatrix. Each line will have n string





elements separated by (comma)

1
Expert's answer
2022-02-09T05:55:12-0500
import numpy as np

given_file = open('test.txt', 'r')
lines = given_file.readlines()
print('Number of lines: ', lines[0])
palindrome = []
for line in lines[1:]:
    line = line.split(',')
    for i in line:
        i = i.strip()
        i = i.replace('\n', '')
        if i == i[::-1]:
            palindrome.append(i)
given_file.close()
print('Palindrome: ', palindrome)

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS