Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python loops instead of recursion

Tags:

python

list

I am a newbie in python . Guys I have written one function which checks that the list is palindrome or not but I wanted to replace it with pure looping statements .Do u have any solution or do I have to use recursive function compulsorily .

import json

def reverse(x):
    if isinstance(x, list):
        return [reverse(x) for x in x[::-1]]
    return x

def palindrome(x):
    return x == reverse(x)

st=raw_input("Enter List : ")

lst=json.loads(st)
print palindrome(lst)
like image 492
Zebronix_777 Avatar asked Jul 18 '26 09:07

Zebronix_777


1 Answers

check this...

>>> def palindrome(n):
     return n == n[::-1]

>>> palindrome('chk')
False
>>> palindrome('chc')
True
>>> 
like image 172
chk Avatar answered Jul 20 '26 23:07

chk