4

Here I have a word list as:

[['r', 'o', 't', 'o', 'r'], ['e', 'v', 'e', 'i', 'a'], ['f', 'i', 'n', 'e', 'd'], ['e', 'n', 'e', 't', 'a'], ['r', 'a', 't', 'e', 'r']]

And I have to display all the palindromes in this list which are in rows as well as columns. I have coded to find all the palindromes in the rows. But cannot implement a method to find the palindromes in the columns.

Here is my code so far:

result_1=""
if len(palindrome)==len_line_str:
    for row in range(len(palindrome)):
        for horizontal_line in range(len(palindrome[row])):
            if ''.join(palindrome[row])==''.join(reversed(palindrome[row])):
                result_1=''.join(palindrome[row])+" is a palindrome starting at ["+str(row)+"]["+str(row)+"] and is a row in the table"
    print(result_1)

Which will display the output:

rotor is a palindrome starting at [0][0] and is a row in the table

Where "rotor" is a palindrome.

I need a method to get the palindromes in the columns which are: "refer", "tenet", "radar"

Any help is much appreciated. Thanks in advance!

eye
  • 91
  • 1
  • 3
  • 14

6 Answers6

4

You can use zip to transpose your lists:

>>> t = [['r', 'o', 't', 'o', 'r'], ['e', 'v', 'e', 'i', 'a'], ['f', 'i', 'n', 'e', 'd'], ['e', 'n', 'e', 't', 'a'], ['r', 'a', 't', 'e', 'r']]
[['r', 'o', 't', 'o', 'r'], ['e', 'v', 'e', 'i', 'a'], ['f', 'i', 'n', 'e', 'd'], ['e', 'n', 'e', 't', 'a'], ['r', 'a', 't', 'e', 'r']]
>>> list(zip(*t))
[('r', 'e', 'f', 'e', 'r'), ('o', 'v', 'i', 'n', 'a'), ('t', 'e', 'n', 'e', 't'), ('o', 'i', 'e', 't', 'e'), ('r', 'a', 'd', 'a', 'r')]

Your columns are now rows, and you can apply the same method than before. If you just need the words, you can use list comprehensions:

>>> rows = [['r', 'o', 't', 'o', 'r'], ['e', 'v', 'e', 'i', 'a'], ['f', 'i', 'n', 'e', 'd'], ['e', 'n', 'e', 't', 'a'], ['r', 'a', 't', 'e', 'r']]
>>> [''.join(row) for row in rows if row[::-1] == row ]
['rotor']
>>> [''.join(column) for column in zip(*rows) if column[::-1] == column ]
['refer', 'tenet', 'radar']
Eric Duminil
  • 52,989
  • 9
  • 71
  • 124
2

This will do the job:

palindrome=[['r', 'o', 't', 'o', 'r'], ['e', 'v', 'e', 'i', 'a'], ['f', 'i', 'n', 'e', 'd'], ['e', 'n', 'e', 't', 'a'], ['r', 'a', 't', 'e', 'r']]
n=len(palindrome)
for col in range(len(palindrome[0])):
    col_word=[palindrome[i][col] for i in range(n)]
    if ''.join(col_word)==''.join(reversed(col_word)):
        result=''.join(col_word)+" is a palindrome starting at ["+str(col)+"] and is a col in the table"
        print(result)

This prints

refer is a palindrome starting at [0] and is a col in the table
tenet is a palindrome starting at [2] and is a col in the table
radar is a palindrome starting at [4] and is a col in the table

Basically, in order to access the words in the column, you can do

col_word=[palindrome[i][col] for i in range(n)] 

This fixes the column and iterates over the rows. The rest of the code is structures similarly to yours.

Miriam Farber
  • 18,986
  • 14
  • 61
  • 76
  • That's great Miriam! I didn't think in that, it's a plus one, you can check my answer to see another alternative if you like :) – developer_hatch Jul 23 '17 at 10:04
1

I saw you did not want to use Zip (which I would recommend using):

Alternative answer:

list_ = [['r', 'o', 't', 'o', 'r'], ['e', 'v', 'e', 'i', 'a'], ['f', 'i', 'n', 'e', 'd'], ['e', 'n', 'e', 't', 'a'], ['r', 'a', 't', 'e', 'r']]

You can get the palindromes (rows) by checking each list with the reversed list [::-1]:

[i==i[::-1] for i in list_]
# prints [True, False, False, False, False]

And get the palindromes (columns) by 1. create the column list (called list_2 below) with a list comprehension and 2. same principle as above:

list_2 = [[i[ind] for i in list_] for ind in range(len(list_))]
[i==i[::-1] for i in list_2]
# prints [True, False, True, False, True]

Update

If you want the answers directly you can do:

[i for i in list_ if i==i[::-1]] 
# prints [['r', 'o', 't', 'o', 'r']]
# and list_2: [['r', 'e', 'f', 'e', 'r'],['t', 'e', 'n', 'e', 't'],['r', 'a', 'd', 'a', 'r']]
Community
  • 1
  • 1
Anton vBR
  • 18,287
  • 5
  • 40
  • 46
1

There are a lot of ways to do it. I will take as example your code because of your effort on it

Another alternative following your code, is creating the columns in another list and check wich of them are palindromes:

palindrome = [['r', 'o', 't', 'o', 'r'], 
              ['e', 'v', 'e', 'i', 'a'], 
              ['f', 'i', 'n', 'e', 'd'], 
              ['e', 'n', 'e', 't', 'a'], 
              ['r', 'a', 't', 'e', 'r']]

len_line_str = 5

result_1=""

def is_pal(string):
  return string == reversed(string)

colums = []
if len(palindrome)==len_line_str:
  for row in range(len(palindrome)):
    vertical = []
    if ''.join(palindrome[row])==''.join(reversed(palindrome[row])):
      result_1+=''.join(palindrome[row])+" is a palindrome starting at ["+str(0)+"]["+str(row)+"] and is a row in the table. " + "\n"
    for horizontal_line in range(len(palindrome[row])):
      if(len_line_str-1 > horizontal_line): 
        vertical += [palindrome[horizontal_line][row]]
      else:
        vertical += [palindrome[horizontal_line][row]]
        colums += [(vertical,row)]

  for word in colums:
    if ''.join(word[0])==''.join(reversed(word[0])):
        result_1+=''.join(word[0])+" is a palindrome starting at ["+str(0)+"]["+str(word[1])+"] and is a column in the table" + "\n"
  print(result_1)
developer_hatch
  • 15,898
  • 3
  • 42
  • 75
0

This should work. First loop iterates through the list s and the second loop iterates through each list.

Assuming s is the name of the list- [['r', 'o', 't', 'o', 'r'], ['e', 'v', 'e', 'i', 'a'], ['f', 'i', 'n', 'e', 'd'], ['e', 'n', 'e', 't', 'a'], ['r', 'a', 't', 'e', 'r']]

for i in xrange(0,len(s),1):
    str = ""
    for j in s:
        str = str + j[i]
    print str
    if str == str[::-1]:
        print str," is a pallindrome - column", i
    else:
        print str," is not a pallindrome - column", i
0

There is no column wise traversal in Python. One hacky way you can follow is to perform transpose operation on your input matrix. Below is a simple way to implement transpose using list comprehensions.

def transpose(matrix):
    if not matrix:
        return []
    return [[row[i] for row in matrix] for i in range(len(matrix[0]))]

Your same logic should work once modify your input using transpose.

Hope this helps!!

krisnik
  • 1,406
  • 11
  • 18