forked from ngiengkianyew/daily-coding-problem
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem_121.py
More file actions
29 lines (23 loc) · 787 Bytes
/
problem_121.py
File metadata and controls
29 lines (23 loc) · 787 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
def is_palindrome(string):
return bool(string) and string == string[::-1]
def make_palindrome(string, num_delete):
if is_palindrome(string):
return True
if not num_delete:
return False
for i, _ in enumerate(string):
new_string = string[:i] + string[i+1:]
if make_palindrome(new_string, num_delete - 1):
return True
return False
# Tests
assert make_palindrome("a", 0)
assert make_palindrome("aaa", 2)
assert not make_palindrome("add", 0)
assert make_palindrome("waterrfetawx", 2)
assert not make_palindrome("waterrfetawx", 1)
assert make_palindrome("waterrfetawx", 3)
assert make_palindrome("malayalam", 0)
assert make_palindrome("malayalam", 1)
assert make_palindrome("asdf", 5)
assert not make_palindrome("asdf", 2)