mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-05-02 14:56:27 -04:00
add some exs
This commit is contained in:
parent
0d5e21b867
commit
9b4c8df7f7
7 changed files with 85 additions and 1 deletions
25
interview_cake/strings/palindrome.py
Normal file
25
interview_cake/strings/palindrome.py
Normal file
|
@ -0,0 +1,25 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
"""
|
||||
Write an efficient function that checks whether
|
||||
any permutation of an input string is a palindrome.
|
||||
"""
|
||||
|
||||
def has_palindrome_permutation(the_string):
|
||||
unpaired_characters = set()
|
||||
|
||||
for char in the_string:
|
||||
if char in unpaired_characters:
|
||||
unpaired_characters.remove(char)
|
||||
else:
|
||||
unpaired_characters.add(char)
|
||||
|
||||
return len(unpaired_characters) <= 1
|
||||
|
||||
str1 = "civic"
|
||||
print has_palindrome_permutation(str1)
|
||||
print "Should be True"
|
||||
|
||||
str2 = "ivilc"
|
||||
print has_palindrome_permutation(str2)
|
||||
print "Should be False"
|
17
interview_cake/strings/reverse_in_place.py
Normal file
17
interview_cake/strings/reverse_in_place.py
Normal file
|
@ -0,0 +1,17 @@
|
|||
#!/usr/bin/env python
|
||||
|
||||
"""
|
||||
Write a function that takes a list of characters and reverses the letters in place.
|
||||
O(n) time and O(1)O(1) space.
|
||||
"""
|
||||
|
||||
def reverse_in_place(char_list):
|
||||
return char_list[::-1]
|
||||
|
||||
|
||||
char_list = ['a', 'b', 'c', 'd', 'e', 'f']
|
||||
|
||||
|
||||
|
||||
print(char_list)
|
||||
print(reverse_in_place(char_list))
|
Loading…
Add table
Add a link
Reference in a new issue