Update remaining snippets

This commit is contained in:
Angelos Chalaris
2019-08-20 11:18:55 +03:00
parent a01dae2945
commit 62581cea2f
6 changed files with 54 additions and 48 deletions

View File

@ -1,17 +1,21 @@
---
title: palindrome
tags: string
tags: string,intermediate
---
Returns `True` if the given string is a palindrome, `False` otherwise.
Convert string `str.lower()` and use `re.sub` to remove non-alphanumeric characters from it. Then compare the new string to the reversed.
Use `str.lower()` and `re.sub()` to convert to lowercase and remove non-alphanumeric characters from the given string.
Then, compare the new string with its reverse.
```py
from re import sub
def palindrome(string):
from re import sub
s = sub('[\W_]', '', string.lower())
return s == s[::-1]
s = sub('[\W_]', '', string.lower())
return s == s[::-1]
```
```py
palindrome('taco cat') # True
```