new snippets

This commit is contained in:
Rohit Tanwar
2018-02-01 13:49:59 +05:30
parent b5a1fa18ba
commit 55f39b14bd
27 changed files with 292 additions and 34 deletions

15
snippets/palindrome.md Normal file
View File

@ -0,0 +1,15 @@
### palindrome
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.
```python
def palindrome(string):
from re import sub
s = sub('[\W_]', '', string.lower())
return s == s[::-1]
```
```python
palindrome('taco cat') # True
```