CTCI Chapter 1 : Palindrome PermutationSimple palindrome function for single wordsPalindrome testerChecking if any permutation of a string can make it palindromePalindrome ValidatorNext PalindromeDetermining if a string is a palindrome of a permutationTest if a string is a palindromeCheck if a string is a permutation of a palindrome using PythonCheck string is permutation of palindromeCheck if a string is a permutation of a palindrome
Why did the RAAF procure the F/A-18 despite being purpose-built for carriers?
Can a fight scene, component-wise, be too complex and complicated?
How to mark beverage cans in a cooler for a blind person?
Is refreshing multiple times a test case for web applications?
Different inverter (logic gate) symbols
Is it okay for a ticket seller to grab a tip in the USA?
CTCI Chapter 1 : Palindrome Permutation
Double redundancy for the Saturn V LVDC computer memory, how were disagreements resolved?
How does 'AND' distribute over 'OR' (Set Theory)?
First amendment and employment: Can a police department terminate an officer for speech?
Can you castle with a "ghost" rook?
Why do funding agencies like the NSF not publish accepted grants?
constant evaluation when using differential equations.
Should I ask for permission to write an expository post about someone's else research?
Ex-contractor published company source code and secrets online
What is the maximum number of PC-controlled undead?
As a 16 year old, how can I keep my money safe from my mother?
(11 of 11: Meta) What is Pyramid Cult's All-Time Favorite?
How can you evade tax by getting employment income just in equity, then using this equity as collateral to take out loan?
Continuous vertical line using booktabs in tabularx table?
Why are Gatwick's runways too close together?
How are you supposed to know the strumming pattern for a song from the "chord sheet music"?
Is there a way to unplug the Raspberry pi safely without shutting down
Y2K... in 2019?
CTCI Chapter 1 : Palindrome Permutation
Simple palindrome function for single wordsPalindrome testerChecking if any permutation of a string can make it palindromePalindrome ValidatorNext PalindromeDetermining if a string is a palindrome of a permutationTest if a string is a palindromeCheck if a string is a permutation of a palindrome using PythonCheck string is permutation of palindromeCheck if a string is a permutation of a palindrome
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
$begingroup$
Below is my code for the problem 1.4 in CTCI. I would like a review of my code and if my approach to the problem is correct.
Problem Statement:
Palindrome Permutation: Given a string, write a function to check if it is a permutation of
a palindrome. A palindrome is a word or phrase that is the same forwards and backwards. A
permutation is a rearrangement of letters. The palindrome does not need to be limited to just
dictionary words.
EXAMPLE
Input: Tact Coa
Output: True (permutations:"taco cat'; "atco cta'; etc.)
My Solution (Python):
def checkPalindromeAndPermutation(inputstr):
lengthOfInputString = len(inputstr)
counterforodd = 0
actualCharactersInInput = 0
inputstr = inputstr.lower()
hashTable = dict()
for i in inputstr:
if i != " ":
hashTable[i] = hashTable.get(i, 0) + 1
actualCharactersInInput = actualCharactersInInput + 1
print(hashTable)
for item in hashTable:
# if input has even length, but each character's frequency is not even, then it's not a plaindrome
if actualCharactersInInput % 2 == 0 and hashTable[item] % 2 != 0:
return False
# if input has odd length, but more than one character's frequency is greater than 1 , then it's not a plaindrome
if actualCharactersInInput % 2 == 1 and hashTable[item] % 2 == 1:
counterforodd = counterforodd + 1
if counterforodd > 1:
return False
return True
print("Answer : " , checkPalindromeAndPermutation("abc bac"))
python palindrome
New contributor
$endgroup$
add a comment |
$begingroup$
Below is my code for the problem 1.4 in CTCI. I would like a review of my code and if my approach to the problem is correct.
Problem Statement:
Palindrome Permutation: Given a string, write a function to check if it is a permutation of
a palindrome. A palindrome is a word or phrase that is the same forwards and backwards. A
permutation is a rearrangement of letters. The palindrome does not need to be limited to just
dictionary words.
EXAMPLE
Input: Tact Coa
Output: True (permutations:"taco cat'; "atco cta'; etc.)
My Solution (Python):
def checkPalindromeAndPermutation(inputstr):
lengthOfInputString = len(inputstr)
counterforodd = 0
actualCharactersInInput = 0
inputstr = inputstr.lower()
hashTable = dict()
for i in inputstr:
if i != " ":
hashTable[i] = hashTable.get(i, 0) + 1
actualCharactersInInput = actualCharactersInInput + 1
print(hashTable)
for item in hashTable:
# if input has even length, but each character's frequency is not even, then it's not a plaindrome
if actualCharactersInInput % 2 == 0 and hashTable[item] % 2 != 0:
return False
# if input has odd length, but more than one character's frequency is greater than 1 , then it's not a plaindrome
if actualCharactersInInput % 2 == 1 and hashTable[item] % 2 == 1:
counterforodd = counterforodd + 1
if counterforodd > 1:
return False
return True
print("Answer : " , checkPalindromeAndPermutation("abc bac"))
python palindrome
New contributor
$endgroup$
add a comment |
$begingroup$
Below is my code for the problem 1.4 in CTCI. I would like a review of my code and if my approach to the problem is correct.
Problem Statement:
Palindrome Permutation: Given a string, write a function to check if it is a permutation of
a palindrome. A palindrome is a word or phrase that is the same forwards and backwards. A
permutation is a rearrangement of letters. The palindrome does not need to be limited to just
dictionary words.
EXAMPLE
Input: Tact Coa
Output: True (permutations:"taco cat'; "atco cta'; etc.)
My Solution (Python):
def checkPalindromeAndPermutation(inputstr):
lengthOfInputString = len(inputstr)
counterforodd = 0
actualCharactersInInput = 0
inputstr = inputstr.lower()
hashTable = dict()
for i in inputstr:
if i != " ":
hashTable[i] = hashTable.get(i, 0) + 1
actualCharactersInInput = actualCharactersInInput + 1
print(hashTable)
for item in hashTable:
# if input has even length, but each character's frequency is not even, then it's not a plaindrome
if actualCharactersInInput % 2 == 0 and hashTable[item] % 2 != 0:
return False
# if input has odd length, but more than one character's frequency is greater than 1 , then it's not a plaindrome
if actualCharactersInInput % 2 == 1 and hashTable[item] % 2 == 1:
counterforodd = counterforodd + 1
if counterforodd > 1:
return False
return True
print("Answer : " , checkPalindromeAndPermutation("abc bac"))
python palindrome
New contributor
$endgroup$
Below is my code for the problem 1.4 in CTCI. I would like a review of my code and if my approach to the problem is correct.
Problem Statement:
Palindrome Permutation: Given a string, write a function to check if it is a permutation of
a palindrome. A palindrome is a word or phrase that is the same forwards and backwards. A
permutation is a rearrangement of letters. The palindrome does not need to be limited to just
dictionary words.
EXAMPLE
Input: Tact Coa
Output: True (permutations:"taco cat'; "atco cta'; etc.)
My Solution (Python):
def checkPalindromeAndPermutation(inputstr):
lengthOfInputString = len(inputstr)
counterforodd = 0
actualCharactersInInput = 0
inputstr = inputstr.lower()
hashTable = dict()
for i in inputstr:
if i != " ":
hashTable[i] = hashTable.get(i, 0) + 1
actualCharactersInInput = actualCharactersInInput + 1
print(hashTable)
for item in hashTable:
# if input has even length, but each character's frequency is not even, then it's not a plaindrome
if actualCharactersInInput % 2 == 0 and hashTable[item] % 2 != 0:
return False
# if input has odd length, but more than one character's frequency is greater than 1 , then it's not a plaindrome
if actualCharactersInInput % 2 == 1 and hashTable[item] % 2 == 1:
counterforodd = counterforodd + 1
if counterforodd > 1:
return False
return True
print("Answer : " , checkPalindromeAndPermutation("abc bac"))
python palindrome
python palindrome
New contributor
New contributor
edited 3 hours ago
200_success
135k21 gold badges173 silver badges443 bronze badges
135k21 gold badges173 silver badges443 bronze badges
New contributor
asked 10 hours ago
Manas TripathiManas Tripathi
162 bronze badges
162 bronze badges
New contributor
New contributor
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
$begingroup$
Nice implementation.
Here are a couple suggestions
collections.defaultdict
Intead of hashTable[i] = hashTable.get(i, 0) + 1
, use collections.defaultdict
charcount = defaultdict(int)
for char in inputStr:
charcount[char] += 1
actualCharactersInInput = len(inputStr) - charcount[' ']
collections.Counter
Or better yet, use a Counter:
charcount = collections.Counter(inputStr)
actualCharactersInInput = len(inputStr) - charcount[' ']
other
if actualCharactersInInput % 2:
# odd number of chars
return sum(count%2 for count in charcount.values()) == 1
else:
# even number of chars
return not any(count % 2 for count in charcount.values())
$endgroup$
$begingroup$
Thank you so much, I learned something new today :)
$endgroup$
– Manas Tripathi
3 hours ago
add a comment |
$begingroup$
This looks correct! Here are my thoughts on the code:
- Per PEP-8, use
snake_case
for variable names andPascalCase
for class names. - Use Python builtins. Python makes frequency counting effortless using
collections.Counter
. - Unused variable:
lengthOfInputString
. A static code analysis tool like a linter can spot this. - Avoid variable names like
hashMap
. Something likefreq_count
,seen
orchar_count
is clearer. - Avoid using
i
as the loop block variable infor i in enumerable:
. Reservei
for index variables and prefer something likec
orelem
that describes the variable more accurately. - The function name,
checkPalindromeAndPermutation
, doesn't accurately describe what the function does, long as it is. I preferis_palindrome_permutation
orpalindrome_permutation
. - Remove all
print
statements from your functions to avoid side effects. - While I'm not a fan of inline comments, the comments in this program explain the logic nicely (typo and horizontal scrolling aside). Consider moving them to the function docstring, though, which summarizes the entire function neatly and gets out of the way of the code.
actualCharactersInInput
can be replaced withlen(s)
assuming you don't mind stripping whitespace beforehand. Having a separate cached variable for holdinglen()
is generally poor practice because the overhead of the function call is worth it to improve readability and reduce the risk of subtle bugs (len()
and cached value going out of sync).- Use
foo += 1
instead offoo = foo + 1
to increment an integer. Branching inside the
for
loop doesn't make much sense since the length ofactualCharactersInInput
is fixed. It makes more sense to pick a branch and stick to it as a human might do naturally if performing this task by hand.Instead of:
for item in hashTable:
if actualCharactersInInput % 2 == 0 and hashTable[item] % 2 != 0:
...
elif actualCharactersInInput % 2 == 1 and hashTable[item] % 2 == 1:
#^^^ we can use elif since the conditional is disjoint
...try:
if actualCharactersInInput % 2 == 0:
for item in hashTable:
if hashTable[item] % 2 != 0:
...
else:
for item in hashTable:
if hashTable[item] % 2 == 1:
...Luckily, branch prediction will make the performance impact negligible even if we apply the conditional inside the loop, so this is mostly about reducing cognitive load on the programmer and isn't a hard-line rule.
Here's a possible re-write:
from collections import Counter
def permuted_palindrome(s):
s = "".join(s.lower().split())
odds = [x for x in Counter(s).values() if x % 2]
if len(s) % 2 == 0:
return len(odds) < 1
return len(odds) < 2
This can cause a performance drop because of a lack of early return option. Benchmark the impact and make a call of performance vs brevity based on your use case.
I recommend validating correctness on any algorithm that's easily written using a clear-cut brute force method:
from collections import Counter
from itertools import permutations
from random import randint as rnd
def permuted_palindrome(s):
'''
Determines if a string is a permuted palindrome.
A string is a permuted palindrome if:
1. the string is of odd length and has 1 or fewer
characters with an odd number of occurrences.
- or -
2. the string is of even length and has no
characters with an odd number of occurrences.
>>> permuted_palindrome("aaa")
True
>>> permuted_palindrome("aaab")
False
>>> permuted_palindrome("aaaab")
True
>>> permuted_palindrome("aaaabc")
False
>>> permuted_palindrome("aaaabcc")
True
'''
s = "".join(s.lower().split())
odds = [x for x in Counter(s).values() if x % 2]
if len(s) % 2 == 0:
return len(odds) < 1
return len(odds) < 2
def brute_permuted_palindrome(s):
return any(x == x[::-1] for x in permutations("".join(s.lower().split())))
if __name__ == "__main__":
tests = 1000
passes = 0
for x in range(tests):
s = "".join(chr(rnd(65, 70)) for x in range(rnd(1, 10)))
if brute_permuted_palindrome(s) == permuted_palindrome(s):
passes += 1
print(f"passed passes/tests tests")
Randomization doesn't guarantee perfect coverage, but it's an easy way to be pretty certain your code works and can often catch edge cases that might be overlooked in enumeration (best to do both).
This snippet also shows how you might include a full docstring with doctests and uses the if __name__ == "__main__":
guard which makes your module easily importable.
$endgroup$
$begingroup$
Waow, Thank you for a detailed explanation. This is very helpful.
$endgroup$
– Manas Tripathi
3 hours ago
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "196"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Manas Tripathi is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f225943%2fctci-chapter-1-palindrome-permutation%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
Nice implementation.
Here are a couple suggestions
collections.defaultdict
Intead of hashTable[i] = hashTable.get(i, 0) + 1
, use collections.defaultdict
charcount = defaultdict(int)
for char in inputStr:
charcount[char] += 1
actualCharactersInInput = len(inputStr) - charcount[' ']
collections.Counter
Or better yet, use a Counter:
charcount = collections.Counter(inputStr)
actualCharactersInInput = len(inputStr) - charcount[' ']
other
if actualCharactersInInput % 2:
# odd number of chars
return sum(count%2 for count in charcount.values()) == 1
else:
# even number of chars
return not any(count % 2 for count in charcount.values())
$endgroup$
$begingroup$
Thank you so much, I learned something new today :)
$endgroup$
– Manas Tripathi
3 hours ago
add a comment |
$begingroup$
Nice implementation.
Here are a couple suggestions
collections.defaultdict
Intead of hashTable[i] = hashTable.get(i, 0) + 1
, use collections.defaultdict
charcount = defaultdict(int)
for char in inputStr:
charcount[char] += 1
actualCharactersInInput = len(inputStr) - charcount[' ']
collections.Counter
Or better yet, use a Counter:
charcount = collections.Counter(inputStr)
actualCharactersInInput = len(inputStr) - charcount[' ']
other
if actualCharactersInInput % 2:
# odd number of chars
return sum(count%2 for count in charcount.values()) == 1
else:
# even number of chars
return not any(count % 2 for count in charcount.values())
$endgroup$
$begingroup$
Thank you so much, I learned something new today :)
$endgroup$
– Manas Tripathi
3 hours ago
add a comment |
$begingroup$
Nice implementation.
Here are a couple suggestions
collections.defaultdict
Intead of hashTable[i] = hashTable.get(i, 0) + 1
, use collections.defaultdict
charcount = defaultdict(int)
for char in inputStr:
charcount[char] += 1
actualCharactersInInput = len(inputStr) - charcount[' ']
collections.Counter
Or better yet, use a Counter:
charcount = collections.Counter(inputStr)
actualCharactersInInput = len(inputStr) - charcount[' ']
other
if actualCharactersInInput % 2:
# odd number of chars
return sum(count%2 for count in charcount.values()) == 1
else:
# even number of chars
return not any(count % 2 for count in charcount.values())
$endgroup$
Nice implementation.
Here are a couple suggestions
collections.defaultdict
Intead of hashTable[i] = hashTable.get(i, 0) + 1
, use collections.defaultdict
charcount = defaultdict(int)
for char in inputStr:
charcount[char] += 1
actualCharactersInInput = len(inputStr) - charcount[' ']
collections.Counter
Or better yet, use a Counter:
charcount = collections.Counter(inputStr)
actualCharactersInInput = len(inputStr) - charcount[' ']
other
if actualCharactersInInput % 2:
# odd number of chars
return sum(count%2 for count in charcount.values()) == 1
else:
# even number of chars
return not any(count % 2 for count in charcount.values())
answered 5 hours ago
RootTwoRootTwo
1,0043 silver badges6 bronze badges
1,0043 silver badges6 bronze badges
$begingroup$
Thank you so much, I learned something new today :)
$endgroup$
– Manas Tripathi
3 hours ago
add a comment |
$begingroup$
Thank you so much, I learned something new today :)
$endgroup$
– Manas Tripathi
3 hours ago
$begingroup$
Thank you so much, I learned something new today :)
$endgroup$
– Manas Tripathi
3 hours ago
$begingroup$
Thank you so much, I learned something new today :)
$endgroup$
– Manas Tripathi
3 hours ago
add a comment |
$begingroup$
This looks correct! Here are my thoughts on the code:
- Per PEP-8, use
snake_case
for variable names andPascalCase
for class names. - Use Python builtins. Python makes frequency counting effortless using
collections.Counter
. - Unused variable:
lengthOfInputString
. A static code analysis tool like a linter can spot this. - Avoid variable names like
hashMap
. Something likefreq_count
,seen
orchar_count
is clearer. - Avoid using
i
as the loop block variable infor i in enumerable:
. Reservei
for index variables and prefer something likec
orelem
that describes the variable more accurately. - The function name,
checkPalindromeAndPermutation
, doesn't accurately describe what the function does, long as it is. I preferis_palindrome_permutation
orpalindrome_permutation
. - Remove all
print
statements from your functions to avoid side effects. - While I'm not a fan of inline comments, the comments in this program explain the logic nicely (typo and horizontal scrolling aside). Consider moving them to the function docstring, though, which summarizes the entire function neatly and gets out of the way of the code.
actualCharactersInInput
can be replaced withlen(s)
assuming you don't mind stripping whitespace beforehand. Having a separate cached variable for holdinglen()
is generally poor practice because the overhead of the function call is worth it to improve readability and reduce the risk of subtle bugs (len()
and cached value going out of sync).- Use
foo += 1
instead offoo = foo + 1
to increment an integer. Branching inside the
for
loop doesn't make much sense since the length ofactualCharactersInInput
is fixed. It makes more sense to pick a branch and stick to it as a human might do naturally if performing this task by hand.Instead of:
for item in hashTable:
if actualCharactersInInput % 2 == 0 and hashTable[item] % 2 != 0:
...
elif actualCharactersInInput % 2 == 1 and hashTable[item] % 2 == 1:
#^^^ we can use elif since the conditional is disjoint
...try:
if actualCharactersInInput % 2 == 0:
for item in hashTable:
if hashTable[item] % 2 != 0:
...
else:
for item in hashTable:
if hashTable[item] % 2 == 1:
...Luckily, branch prediction will make the performance impact negligible even if we apply the conditional inside the loop, so this is mostly about reducing cognitive load on the programmer and isn't a hard-line rule.
Here's a possible re-write:
from collections import Counter
def permuted_palindrome(s):
s = "".join(s.lower().split())
odds = [x for x in Counter(s).values() if x % 2]
if len(s) % 2 == 0:
return len(odds) < 1
return len(odds) < 2
This can cause a performance drop because of a lack of early return option. Benchmark the impact and make a call of performance vs brevity based on your use case.
I recommend validating correctness on any algorithm that's easily written using a clear-cut brute force method:
from collections import Counter
from itertools import permutations
from random import randint as rnd
def permuted_palindrome(s):
'''
Determines if a string is a permuted palindrome.
A string is a permuted palindrome if:
1. the string is of odd length and has 1 or fewer
characters with an odd number of occurrences.
- or -
2. the string is of even length and has no
characters with an odd number of occurrences.
>>> permuted_palindrome("aaa")
True
>>> permuted_palindrome("aaab")
False
>>> permuted_palindrome("aaaab")
True
>>> permuted_palindrome("aaaabc")
False
>>> permuted_palindrome("aaaabcc")
True
'''
s = "".join(s.lower().split())
odds = [x for x in Counter(s).values() if x % 2]
if len(s) % 2 == 0:
return len(odds) < 1
return len(odds) < 2
def brute_permuted_palindrome(s):
return any(x == x[::-1] for x in permutations("".join(s.lower().split())))
if __name__ == "__main__":
tests = 1000
passes = 0
for x in range(tests):
s = "".join(chr(rnd(65, 70)) for x in range(rnd(1, 10)))
if brute_permuted_palindrome(s) == permuted_palindrome(s):
passes += 1
print(f"passed passes/tests tests")
Randomization doesn't guarantee perfect coverage, but it's an easy way to be pretty certain your code works and can often catch edge cases that might be overlooked in enumeration (best to do both).
This snippet also shows how you might include a full docstring with doctests and uses the if __name__ == "__main__":
guard which makes your module easily importable.
$endgroup$
$begingroup$
Waow, Thank you for a detailed explanation. This is very helpful.
$endgroup$
– Manas Tripathi
3 hours ago
add a comment |
$begingroup$
This looks correct! Here are my thoughts on the code:
- Per PEP-8, use
snake_case
for variable names andPascalCase
for class names. - Use Python builtins. Python makes frequency counting effortless using
collections.Counter
. - Unused variable:
lengthOfInputString
. A static code analysis tool like a linter can spot this. - Avoid variable names like
hashMap
. Something likefreq_count
,seen
orchar_count
is clearer. - Avoid using
i
as the loop block variable infor i in enumerable:
. Reservei
for index variables and prefer something likec
orelem
that describes the variable more accurately. - The function name,
checkPalindromeAndPermutation
, doesn't accurately describe what the function does, long as it is. I preferis_palindrome_permutation
orpalindrome_permutation
. - Remove all
print
statements from your functions to avoid side effects. - While I'm not a fan of inline comments, the comments in this program explain the logic nicely (typo and horizontal scrolling aside). Consider moving them to the function docstring, though, which summarizes the entire function neatly and gets out of the way of the code.
actualCharactersInInput
can be replaced withlen(s)
assuming you don't mind stripping whitespace beforehand. Having a separate cached variable for holdinglen()
is generally poor practice because the overhead of the function call is worth it to improve readability and reduce the risk of subtle bugs (len()
and cached value going out of sync).- Use
foo += 1
instead offoo = foo + 1
to increment an integer. Branching inside the
for
loop doesn't make much sense since the length ofactualCharactersInInput
is fixed. It makes more sense to pick a branch and stick to it as a human might do naturally if performing this task by hand.Instead of:
for item in hashTable:
if actualCharactersInInput % 2 == 0 and hashTable[item] % 2 != 0:
...
elif actualCharactersInInput % 2 == 1 and hashTable[item] % 2 == 1:
#^^^ we can use elif since the conditional is disjoint
...try:
if actualCharactersInInput % 2 == 0:
for item in hashTable:
if hashTable[item] % 2 != 0:
...
else:
for item in hashTable:
if hashTable[item] % 2 == 1:
...Luckily, branch prediction will make the performance impact negligible even if we apply the conditional inside the loop, so this is mostly about reducing cognitive load on the programmer and isn't a hard-line rule.
Here's a possible re-write:
from collections import Counter
def permuted_palindrome(s):
s = "".join(s.lower().split())
odds = [x for x in Counter(s).values() if x % 2]
if len(s) % 2 == 0:
return len(odds) < 1
return len(odds) < 2
This can cause a performance drop because of a lack of early return option. Benchmark the impact and make a call of performance vs brevity based on your use case.
I recommend validating correctness on any algorithm that's easily written using a clear-cut brute force method:
from collections import Counter
from itertools import permutations
from random import randint as rnd
def permuted_palindrome(s):
'''
Determines if a string is a permuted palindrome.
A string is a permuted palindrome if:
1. the string is of odd length and has 1 or fewer
characters with an odd number of occurrences.
- or -
2. the string is of even length and has no
characters with an odd number of occurrences.
>>> permuted_palindrome("aaa")
True
>>> permuted_palindrome("aaab")
False
>>> permuted_palindrome("aaaab")
True
>>> permuted_palindrome("aaaabc")
False
>>> permuted_palindrome("aaaabcc")
True
'''
s = "".join(s.lower().split())
odds = [x for x in Counter(s).values() if x % 2]
if len(s) % 2 == 0:
return len(odds) < 1
return len(odds) < 2
def brute_permuted_palindrome(s):
return any(x == x[::-1] for x in permutations("".join(s.lower().split())))
if __name__ == "__main__":
tests = 1000
passes = 0
for x in range(tests):
s = "".join(chr(rnd(65, 70)) for x in range(rnd(1, 10)))
if brute_permuted_palindrome(s) == permuted_palindrome(s):
passes += 1
print(f"passed passes/tests tests")
Randomization doesn't guarantee perfect coverage, but it's an easy way to be pretty certain your code works and can often catch edge cases that might be overlooked in enumeration (best to do both).
This snippet also shows how you might include a full docstring with doctests and uses the if __name__ == "__main__":
guard which makes your module easily importable.
$endgroup$
$begingroup$
Waow, Thank you for a detailed explanation. This is very helpful.
$endgroup$
– Manas Tripathi
3 hours ago
add a comment |
$begingroup$
This looks correct! Here are my thoughts on the code:
- Per PEP-8, use
snake_case
for variable names andPascalCase
for class names. - Use Python builtins. Python makes frequency counting effortless using
collections.Counter
. - Unused variable:
lengthOfInputString
. A static code analysis tool like a linter can spot this. - Avoid variable names like
hashMap
. Something likefreq_count
,seen
orchar_count
is clearer. - Avoid using
i
as the loop block variable infor i in enumerable:
. Reservei
for index variables and prefer something likec
orelem
that describes the variable more accurately. - The function name,
checkPalindromeAndPermutation
, doesn't accurately describe what the function does, long as it is. I preferis_palindrome_permutation
orpalindrome_permutation
. - Remove all
print
statements from your functions to avoid side effects. - While I'm not a fan of inline comments, the comments in this program explain the logic nicely (typo and horizontal scrolling aside). Consider moving them to the function docstring, though, which summarizes the entire function neatly and gets out of the way of the code.
actualCharactersInInput
can be replaced withlen(s)
assuming you don't mind stripping whitespace beforehand. Having a separate cached variable for holdinglen()
is generally poor practice because the overhead of the function call is worth it to improve readability and reduce the risk of subtle bugs (len()
and cached value going out of sync).- Use
foo += 1
instead offoo = foo + 1
to increment an integer. Branching inside the
for
loop doesn't make much sense since the length ofactualCharactersInInput
is fixed. It makes more sense to pick a branch and stick to it as a human might do naturally if performing this task by hand.Instead of:
for item in hashTable:
if actualCharactersInInput % 2 == 0 and hashTable[item] % 2 != 0:
...
elif actualCharactersInInput % 2 == 1 and hashTable[item] % 2 == 1:
#^^^ we can use elif since the conditional is disjoint
...try:
if actualCharactersInInput % 2 == 0:
for item in hashTable:
if hashTable[item] % 2 != 0:
...
else:
for item in hashTable:
if hashTable[item] % 2 == 1:
...Luckily, branch prediction will make the performance impact negligible even if we apply the conditional inside the loop, so this is mostly about reducing cognitive load on the programmer and isn't a hard-line rule.
Here's a possible re-write:
from collections import Counter
def permuted_palindrome(s):
s = "".join(s.lower().split())
odds = [x for x in Counter(s).values() if x % 2]
if len(s) % 2 == 0:
return len(odds) < 1
return len(odds) < 2
This can cause a performance drop because of a lack of early return option. Benchmark the impact and make a call of performance vs brevity based on your use case.
I recommend validating correctness on any algorithm that's easily written using a clear-cut brute force method:
from collections import Counter
from itertools import permutations
from random import randint as rnd
def permuted_palindrome(s):
'''
Determines if a string is a permuted palindrome.
A string is a permuted palindrome if:
1. the string is of odd length and has 1 or fewer
characters with an odd number of occurrences.
- or -
2. the string is of even length and has no
characters with an odd number of occurrences.
>>> permuted_palindrome("aaa")
True
>>> permuted_palindrome("aaab")
False
>>> permuted_palindrome("aaaab")
True
>>> permuted_palindrome("aaaabc")
False
>>> permuted_palindrome("aaaabcc")
True
'''
s = "".join(s.lower().split())
odds = [x for x in Counter(s).values() if x % 2]
if len(s) % 2 == 0:
return len(odds) < 1
return len(odds) < 2
def brute_permuted_palindrome(s):
return any(x == x[::-1] for x in permutations("".join(s.lower().split())))
if __name__ == "__main__":
tests = 1000
passes = 0
for x in range(tests):
s = "".join(chr(rnd(65, 70)) for x in range(rnd(1, 10)))
if brute_permuted_palindrome(s) == permuted_palindrome(s):
passes += 1
print(f"passed passes/tests tests")
Randomization doesn't guarantee perfect coverage, but it's an easy way to be pretty certain your code works and can often catch edge cases that might be overlooked in enumeration (best to do both).
This snippet also shows how you might include a full docstring with doctests and uses the if __name__ == "__main__":
guard which makes your module easily importable.
$endgroup$
This looks correct! Here are my thoughts on the code:
- Per PEP-8, use
snake_case
for variable names andPascalCase
for class names. - Use Python builtins. Python makes frequency counting effortless using
collections.Counter
. - Unused variable:
lengthOfInputString
. A static code analysis tool like a linter can spot this. - Avoid variable names like
hashMap
. Something likefreq_count
,seen
orchar_count
is clearer. - Avoid using
i
as the loop block variable infor i in enumerable:
. Reservei
for index variables and prefer something likec
orelem
that describes the variable more accurately. - The function name,
checkPalindromeAndPermutation
, doesn't accurately describe what the function does, long as it is. I preferis_palindrome_permutation
orpalindrome_permutation
. - Remove all
print
statements from your functions to avoid side effects. - While I'm not a fan of inline comments, the comments in this program explain the logic nicely (typo and horizontal scrolling aside). Consider moving them to the function docstring, though, which summarizes the entire function neatly and gets out of the way of the code.
actualCharactersInInput
can be replaced withlen(s)
assuming you don't mind stripping whitespace beforehand. Having a separate cached variable for holdinglen()
is generally poor practice because the overhead of the function call is worth it to improve readability and reduce the risk of subtle bugs (len()
and cached value going out of sync).- Use
foo += 1
instead offoo = foo + 1
to increment an integer. Branching inside the
for
loop doesn't make much sense since the length ofactualCharactersInInput
is fixed. It makes more sense to pick a branch and stick to it as a human might do naturally if performing this task by hand.Instead of:
for item in hashTable:
if actualCharactersInInput % 2 == 0 and hashTable[item] % 2 != 0:
...
elif actualCharactersInInput % 2 == 1 and hashTable[item] % 2 == 1:
#^^^ we can use elif since the conditional is disjoint
...try:
if actualCharactersInInput % 2 == 0:
for item in hashTable:
if hashTable[item] % 2 != 0:
...
else:
for item in hashTable:
if hashTable[item] % 2 == 1:
...Luckily, branch prediction will make the performance impact negligible even if we apply the conditional inside the loop, so this is mostly about reducing cognitive load on the programmer and isn't a hard-line rule.
Here's a possible re-write:
from collections import Counter
def permuted_palindrome(s):
s = "".join(s.lower().split())
odds = [x for x in Counter(s).values() if x % 2]
if len(s) % 2 == 0:
return len(odds) < 1
return len(odds) < 2
This can cause a performance drop because of a lack of early return option. Benchmark the impact and make a call of performance vs brevity based on your use case.
I recommend validating correctness on any algorithm that's easily written using a clear-cut brute force method:
from collections import Counter
from itertools import permutations
from random import randint as rnd
def permuted_palindrome(s):
'''
Determines if a string is a permuted palindrome.
A string is a permuted palindrome if:
1. the string is of odd length and has 1 or fewer
characters with an odd number of occurrences.
- or -
2. the string is of even length and has no
characters with an odd number of occurrences.
>>> permuted_palindrome("aaa")
True
>>> permuted_palindrome("aaab")
False
>>> permuted_palindrome("aaaab")
True
>>> permuted_palindrome("aaaabc")
False
>>> permuted_palindrome("aaaabcc")
True
'''
s = "".join(s.lower().split())
odds = [x for x in Counter(s).values() if x % 2]
if len(s) % 2 == 0:
return len(odds) < 1
return len(odds) < 2
def brute_permuted_palindrome(s):
return any(x == x[::-1] for x in permutations("".join(s.lower().split())))
if __name__ == "__main__":
tests = 1000
passes = 0
for x in range(tests):
s = "".join(chr(rnd(65, 70)) for x in range(rnd(1, 10)))
if brute_permuted_palindrome(s) == permuted_palindrome(s):
passes += 1
print(f"passed passes/tests tests")
Randomization doesn't guarantee perfect coverage, but it's an easy way to be pretty certain your code works and can often catch edge cases that might be overlooked in enumeration (best to do both).
This snippet also shows how you might include a full docstring with doctests and uses the if __name__ == "__main__":
guard which makes your module easily importable.
edited 4 hours ago
answered 4 hours ago
ggorlenggorlen
1,0671 gold badge4 silver badges15 bronze badges
1,0671 gold badge4 silver badges15 bronze badges
$begingroup$
Waow, Thank you for a detailed explanation. This is very helpful.
$endgroup$
– Manas Tripathi
3 hours ago
add a comment |
$begingroup$
Waow, Thank you for a detailed explanation. This is very helpful.
$endgroup$
– Manas Tripathi
3 hours ago
$begingroup$
Waow, Thank you for a detailed explanation. This is very helpful.
$endgroup$
– Manas Tripathi
3 hours ago
$begingroup$
Waow, Thank you for a detailed explanation. This is very helpful.
$endgroup$
– Manas Tripathi
3 hours ago
add a comment |
Manas Tripathi is a new contributor. Be nice, and check out our Code of Conduct.
Manas Tripathi is a new contributor. Be nice, and check out our Code of Conduct.
Manas Tripathi is a new contributor. Be nice, and check out our Code of Conduct.
Manas Tripathi is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f225943%2fctci-chapter-1-palindrome-permutation%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown