Skip to content

Commit d67d051

Browse files
committed
replace word with s, comment change to more succinct
1 parent 50a2ae1 commit d67d051

File tree

1 file changed

+22
-23
lines changed

1 file changed

+22
-23
lines changed

Python/chapter01/1.4 - PalinPerm/miguel_1.4_sol.py

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,20 @@
1111
from typing import Callable
1212

1313

14-
def is_permutation_of_palindrome(word: str) -> bool:
14+
def is_permutation_of_palindrome(s: str) -> bool:
1515
"""
16-
Checks if a word is a palindrome by analyzing the frequencies of characters.
16+
Checks if a string is a palindrome by analyzing the frequencies of characters.
1717
A palindrome is a word or phrase that is the same forwards and backwards.
1818
When determining character frequencies, word will be put to lowercase
1919
and spaces will not be counted
2020
A palindrome also has the following property:
21-
* all characters have an even count or all except one are even
21+
* at most one character appears an odd number of times
2222
Runtime: O(n)
2323
Space Complexity: O(n)
24-
:param word: the word we check, 'word' is a possible permutation of a palindrome
25-
:return: true if word is a permutation of a palindrome, false otherwise
24+
:param s: the string we check, 's' is a possible permutation of a palindrome
25+
:return: true if s is a permutation of a palindrome, false otherwise
2626
"""
27-
char_frequencies = collections.Counter(word.lower().replace(' ', ''))
27+
char_frequencies = collections.Counter(s.lower().replace(' ', ''))
2828
num_odd_freq_chars = 0
2929

3030
for key, val in char_frequencies.items():
@@ -35,21 +35,21 @@ def is_permutation_of_palindrome(word: str) -> bool:
3535
return True
3636

3737

38-
def _is_palindrome(word: str) -> bool:
38+
def _is_palindrome(s: str) -> bool:
3939
"""
40-
Checks if word is a palindrome by checking if the forward version is the same as the backward version.
40+
Checks if s is a palindrome by checking if the forward version is the same as the backward version.
4141
A palindrome is a word or phrase that is the same forwards and backwards.
4242
Whitespace will not be considered when determining palindrome.
4343
This function is case insensitive.
44-
:param word: the word we check, possible permutation of a palindrome
45-
:return: true if word is a palindrome, false otherwise
44+
:param s: the string we check, possible permutation of a palindrome
45+
:return: true if s is a palindrome, false otherwise
4646
"""
47-
word_no_spaces = word.replace(' ', '').lower()
48-
reversed_word = word_no_spaces[::-1]
49-
return word_no_spaces == reversed_word
47+
s_no_spaces = s.replace(' ', '').lower()
48+
reversed_s = s_no_spaces[::-1]
49+
return s_no_spaces == reversed_s
5050

5151

52-
def is_permutation_of_palindrome_brute_force(word: str) -> bool:
52+
def is_permutation_of_palindrome_brute_force(s: str) -> bool:
5353
"""
5454
Given a string, this function will return whether the string is a permutation of a palindrome.
5555
A palindrome is a word or phrase that is the same forwards and backwards.
@@ -62,12 +62,11 @@ def is_permutation_of_palindrome_brute_force(word: str) -> bool:
6262
Space complexity is O(n!)
6363
Given: Expect:
6464
Tact Coa True (permutations: "taco cat", "atco cta")
65-
66-
:param word: the string that we want to check for perm of a palindrome
67-
:return: True if word is a palindrome, False otherwise.
65+
:param s: the string that we want to check for perm of a palindrome
66+
:return: True if s is a palindrome, False otherwise.
6867
"""
69-
word_no_spaces = word.replace(' ', '')
70-
perms = [''.join(p) for p in it.permutations(word_no_spaces)]
68+
s_no_spaces = s.replace(' ', '')
69+
perms = [''.join(p) for p in it.permutations(s_no_spaces)]
7170
for p in perms:
7271
if _is_palindrome(p):
7372
return True
@@ -86,8 +85,8 @@ def _run_tests(self, f: Callable[[str], bool]) -> None:
8685
("", True),
8786
("a", True)
8887
]
89-
for word, expected in cases:
90-
self.assertEqual(f(word), expected, msg=word)
88+
for s, expected in cases:
89+
self.assertEqual(f(s), expected, msg=s)
9190

9291
def test_is_permutation_of_palindrome(self):
9392
self._run_tests(is_permutation_of_palindrome_brute_force)
@@ -106,8 +105,8 @@ def test_is_palindrome(self):
106105
("Tacoo Cat", True),
107106
("Tacooo Cat", True)
108107
]
109-
for word, expected in cases:
110-
self.assertEqual(_is_palindrome(word), expected, msg=word)
108+
for s, expected in cases:
109+
self.assertEqual(_is_palindrome(s), expected, msg=s)
111110

112111

113112
if __name__ == '__main__':

0 commit comments

Comments
 (0)