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)
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)
Comments
Leave a comment