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
How to estimate Scoville level of home-made pepper sauce??
What to say to a student who has failed?
How to prevent clipped screen edges on my TV, HDMI-connected?
Would this system work to purify water?
Numbers Decrease while Letters Increase
Is gzip atomic?
Why did MS-DOS applications built using Turbo Pascal fail to start with a division by zero error on faster systems?
Does norwegian.no airline overbook flights?
Irish Snap: Variant Rules
Can't stopover at Sapporo when going from Asahikawa to Chitose airport?
Can more than one wizard copy a spell from a spellbook?
Did the British navy fail to take into account the ballistics correction due to Coriolis force during WW1 Falkland Islands battle?
Who was president of the USA?
Why does The Ancient One think differently about Doctor Strange in Endgame than the film Doctor Strange?
Why were the crew so desperate to catch Truman and return him to Seahaven?
Fried gnocchi with spinach, bacon, cream sauce in a single pan
Why do all fields in a QFT transform like *irreducible* representations of some group?
Thank God it's Friday, tomorrow is THE weekend. Why the definite article?
Pair trading - short / long the spread
Why in most German places is the church the tallest building?
Disambiguation of "nobis vobis" and "nobis nobis"
Is there any music source code for sound chips?
What are some interesting features that are common cross-linguistically but don't exist in English?
Mathematical uses of string theory
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
$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
$endgroup$
1
$begingroup$
The logic can be simplified. You only need to verify that at most 1 character has an odd count.
$endgroup$
– Florian F
Aug 12 at 11:28
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
$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
edited Aug 12 at 2:04
200_success
135k21 gold badges173 silver badges443 bronze badges
135k21 gold badges173 silver badges443 bronze badges
asked Aug 11 at 19:50
Manas TripathiManas Tripathi
312 bronze badges
312 bronze badges
1
$begingroup$
The logic can be simplified. You only need to verify that at most 1 character has an odd count.
$endgroup$
– Florian F
Aug 12 at 11:28
add a comment |
1
$begingroup$
The logic can be simplified. You only need to verify that at most 1 character has an odd count.
$endgroup$
– Florian F
Aug 12 at 11:28
1
1
$begingroup$
The logic can be simplified. You only need to verify that at most 1 character has an odd count.
$endgroup$
– Florian F
Aug 12 at 11:28
$begingroup$
The logic can be simplified. You only need to verify that at most 1 character has an odd count.
$endgroup$
– Florian F
Aug 12 at 11:28
add a comment |
3 Answers
3
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
Aug 12 at 2:21
$begingroup$
You can "smarten" the final check. If the length is odd there must be 1 character with an odd frequency. If the length is even there must be 0 characters with an odd frequency. Changenot any(count % 2 for count in charcount.values())
tosum(count%2 for count in charcount.values()) == 0
. Then the check is one linereturn sum(count%2 for count in charcount.values()) == actualCharactersInInput % 2
$endgroup$
– spyr03
Aug 12 at 14:19
$begingroup$
@spyr03, I thought of that too, but opted to usenot any(...)
because it would stop when it found the first odd count rather thansum
the entire list. Probably a case of premature optimization.
$endgroup$
– RootTwo
Aug 12 at 18:38
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
Aug 12 at 2:21
$begingroup$
Why ` s = "".join(s.lower().split())` instead ofs.lower()
ors.casefold()
. And if you doodds = sum(x % 2 for x in Counter(s).values())
,you don't need the len, and can doreturn odds < 1 + (len(s) % 2)
$endgroup$
– Maarten Fabré
Aug 12 at 8:48
$begingroup$
You can reduce the return logic toreturn len(odds) == len(s) % 2
. Even length strings can have no odds, while odd length strings can have one.
$endgroup$
– spyr03
Aug 12 at 14:22
$begingroup$
Thanks for the feedback.s = "".join(s.lower().split())
removes whitespace and lowers the string. After removing whitespace, the rest of the logic is much cleaner, and this handles more than just spaces. I keptlen
oversum
becausesum
seemed to obfuscate the logic a bit.return len(odds) == len(s) % 2
seems reasonable but I think going terser at this point has diminishing returns.
$endgroup$
– ggorlen
Aug 12 at 14:51
add a comment |
$begingroup$
Revised from my C# version
Using a set instead of a dictionary or hashtable uses less space
We casefold to ignore case sensitivity and then by sorting this we can try to fail out early. If we have any more than 2 odd letters (which will happen any time we check a third unique char) then we fail out. But we don't want to fail when checking the second member, because it could possibly be removed.
If we didn't fail early we still want to make sure we have at most one odd letter. Therefore the final boolean comparison returns appropriately.
def isPermutedPalindrome(input_string):
if (len(input_string) < 2):
return True
input_string = sorted(input_string.casefold())
char_set = set()
for letter in input_string:
if letter == ' ':
continue
if letter in char_set:
char_set.remove(letter)
else:
char_set.add(letter)
if (len(char_set) > 2):
return False
return (len(char_set) < 2)
$endgroup$
$begingroup$
When answering a question, you should at least try to review the original code in some way or another. Answering in a different language is also confusing and should be avoided.
$endgroup$
– dfhwze
Aug 15 at 19:54
1
$begingroup$
Ok, I wasn't sure how to do it in Python right away, so here's the converted snippet.
$endgroup$
– Drubuntu
Aug 15 at 22:57
1
$begingroup$
Thank's for taking the time to convert the snippet. Could you also explain the differences between your snippet and OP code to make us understand why your solution could be better?
$endgroup$
– dfhwze
Aug 16 at 7:12
$begingroup$
In python, aset
would be a more natural datastructure for thischar_list
than a list. And theletter is ' '
only works because CPython interns a number of strings that occur a lot.letter == ' '
is more universally correct
$endgroup$
– Maarten Fabré
Aug 16 at 7:17
$begingroup$
Thanks for the feedback everyone!
$endgroup$
– Drubuntu
Aug 16 at 18:27
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
);
);
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
3 Answers
3
active
oldest
votes
3 Answers
3
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
Aug 12 at 2:21
$begingroup$
You can "smarten" the final check. If the length is odd there must be 1 character with an odd frequency. If the length is even there must be 0 characters with an odd frequency. Changenot any(count % 2 for count in charcount.values())
tosum(count%2 for count in charcount.values()) == 0
. Then the check is one linereturn sum(count%2 for count in charcount.values()) == actualCharactersInInput % 2
$endgroup$
– spyr03
Aug 12 at 14:19
$begingroup$
@spyr03, I thought of that too, but opted to usenot any(...)
because it would stop when it found the first odd count rather thansum
the entire list. Probably a case of premature optimization.
$endgroup$
– RootTwo
Aug 12 at 18:38
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
Aug 12 at 2:21
$begingroup$
You can "smarten" the final check. If the length is odd there must be 1 character with an odd frequency. If the length is even there must be 0 characters with an odd frequency. Changenot any(count % 2 for count in charcount.values())
tosum(count%2 for count in charcount.values()) == 0
. Then the check is one linereturn sum(count%2 for count in charcount.values()) == actualCharactersInInput % 2
$endgroup$
– spyr03
Aug 12 at 14:19
$begingroup$
@spyr03, I thought of that too, but opted to usenot any(...)
because it would stop when it found the first odd count rather thansum
the entire list. Probably a case of premature optimization.
$endgroup$
– RootTwo
Aug 12 at 18:38
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 Aug 12 at 0:32
RootTwoRootTwo
1,5843 silver badges9 bronze badges
1,5843 silver badges9 bronze badges
$begingroup$
Thank you so much, I learned something new today :)
$endgroup$
– Manas Tripathi
Aug 12 at 2:21
$begingroup$
You can "smarten" the final check. If the length is odd there must be 1 character with an odd frequency. If the length is even there must be 0 characters with an odd frequency. Changenot any(count % 2 for count in charcount.values())
tosum(count%2 for count in charcount.values()) == 0
. Then the check is one linereturn sum(count%2 for count in charcount.values()) == actualCharactersInInput % 2
$endgroup$
– spyr03
Aug 12 at 14:19
$begingroup$
@spyr03, I thought of that too, but opted to usenot any(...)
because it would stop when it found the first odd count rather thansum
the entire list. Probably a case of premature optimization.
$endgroup$
– RootTwo
Aug 12 at 18:38
add a comment |
$begingroup$
Thank you so much, I learned something new today :)
$endgroup$
– Manas Tripathi
Aug 12 at 2:21
$begingroup$
You can "smarten" the final check. If the length is odd there must be 1 character with an odd frequency. If the length is even there must be 0 characters with an odd frequency. Changenot any(count % 2 for count in charcount.values())
tosum(count%2 for count in charcount.values()) == 0
. Then the check is one linereturn sum(count%2 for count in charcount.values()) == actualCharactersInInput % 2
$endgroup$
– spyr03
Aug 12 at 14:19
$begingroup$
@spyr03, I thought of that too, but opted to usenot any(...)
because it would stop when it found the first odd count rather thansum
the entire list. Probably a case of premature optimization.
$endgroup$
– RootTwo
Aug 12 at 18:38
$begingroup$
Thank you so much, I learned something new today :)
$endgroup$
– Manas Tripathi
Aug 12 at 2:21
$begingroup$
Thank you so much, I learned something new today :)
$endgroup$
– Manas Tripathi
Aug 12 at 2:21
$begingroup$
You can "smarten" the final check. If the length is odd there must be 1 character with an odd frequency. If the length is even there must be 0 characters with an odd frequency. Change
not any(count % 2 for count in charcount.values())
to sum(count%2 for count in charcount.values()) == 0
. Then the check is one line return sum(count%2 for count in charcount.values()) == actualCharactersInInput % 2
$endgroup$
– spyr03
Aug 12 at 14:19
$begingroup$
You can "smarten" the final check. If the length is odd there must be 1 character with an odd frequency. If the length is even there must be 0 characters with an odd frequency. Change
not any(count % 2 for count in charcount.values())
to sum(count%2 for count in charcount.values()) == 0
. Then the check is one line return sum(count%2 for count in charcount.values()) == actualCharactersInInput % 2
$endgroup$
– spyr03
Aug 12 at 14:19
$begingroup$
@spyr03, I thought of that too, but opted to use
not any(...)
because it would stop when it found the first odd count rather than sum
the entire list. Probably a case of premature optimization.$endgroup$
– RootTwo
Aug 12 at 18:38
$begingroup$
@spyr03, I thought of that too, but opted to use
not any(...)
because it would stop when it found the first odd count rather than sum
the entire list. Probably a case of premature optimization.$endgroup$
– RootTwo
Aug 12 at 18:38
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
Aug 12 at 2:21
$begingroup$
Why ` s = "".join(s.lower().split())` instead ofs.lower()
ors.casefold()
. And if you doodds = sum(x % 2 for x in Counter(s).values())
,you don't need the len, and can doreturn odds < 1 + (len(s) % 2)
$endgroup$
– Maarten Fabré
Aug 12 at 8:48
$begingroup$
You can reduce the return logic toreturn len(odds) == len(s) % 2
. Even length strings can have no odds, while odd length strings can have one.
$endgroup$
– spyr03
Aug 12 at 14:22
$begingroup$
Thanks for the feedback.s = "".join(s.lower().split())
removes whitespace and lowers the string. After removing whitespace, the rest of the logic is much cleaner, and this handles more than just spaces. I keptlen
oversum
becausesum
seemed to obfuscate the logic a bit.return len(odds) == len(s) % 2
seems reasonable but I think going terser at this point has diminishing returns.
$endgroup$
– ggorlen
Aug 12 at 14:51
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
Aug 12 at 2:21
$begingroup$
Why ` s = "".join(s.lower().split())` instead ofs.lower()
ors.casefold()
. And if you doodds = sum(x % 2 for x in Counter(s).values())
,you don't need the len, and can doreturn odds < 1 + (len(s) % 2)
$endgroup$
– Maarten Fabré
Aug 12 at 8:48
$begingroup$
You can reduce the return logic toreturn len(odds) == len(s) % 2
. Even length strings can have no odds, while odd length strings can have one.
$endgroup$
– spyr03
Aug 12 at 14:22
$begingroup$
Thanks for the feedback.s = "".join(s.lower().split())
removes whitespace and lowers the string. After removing whitespace, the rest of the logic is much cleaner, and this handles more than just spaces. I keptlen
oversum
becausesum
seemed to obfuscate the logic a bit.return len(odds) == len(s) % 2
seems reasonable but I think going terser at this point has diminishing returns.
$endgroup$
– ggorlen
Aug 12 at 14:51
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 Aug 12 at 1:31
answered Aug 12 at 1:05
ggorlenggorlen
1,0971 gold badge4 silver badges15 bronze badges
1,0971 gold badge4 silver badges15 bronze badges
$begingroup$
Waow, Thank you for a detailed explanation. This is very helpful.
$endgroup$
– Manas Tripathi
Aug 12 at 2:21
$begingroup$
Why ` s = "".join(s.lower().split())` instead ofs.lower()
ors.casefold()
. And if you doodds = sum(x % 2 for x in Counter(s).values())
,you don't need the len, and can doreturn odds < 1 + (len(s) % 2)
$endgroup$
– Maarten Fabré
Aug 12 at 8:48
$begingroup$
You can reduce the return logic toreturn len(odds) == len(s) % 2
. Even length strings can have no odds, while odd length strings can have one.
$endgroup$
– spyr03
Aug 12 at 14:22
$begingroup$
Thanks for the feedback.s = "".join(s.lower().split())
removes whitespace and lowers the string. After removing whitespace, the rest of the logic is much cleaner, and this handles more than just spaces. I keptlen
oversum
becausesum
seemed to obfuscate the logic a bit.return len(odds) == len(s) % 2
seems reasonable but I think going terser at this point has diminishing returns.
$endgroup$
– ggorlen
Aug 12 at 14:51
add a comment |
$begingroup$
Waow, Thank you for a detailed explanation. This is very helpful.
$endgroup$
– Manas Tripathi
Aug 12 at 2:21
$begingroup$
Why ` s = "".join(s.lower().split())` instead ofs.lower()
ors.casefold()
. And if you doodds = sum(x % 2 for x in Counter(s).values())
,you don't need the len, and can doreturn odds < 1 + (len(s) % 2)
$endgroup$
– Maarten Fabré
Aug 12 at 8:48
$begingroup$
You can reduce the return logic toreturn len(odds) == len(s) % 2
. Even length strings can have no odds, while odd length strings can have one.
$endgroup$
– spyr03
Aug 12 at 14:22
$begingroup$
Thanks for the feedback.s = "".join(s.lower().split())
removes whitespace and lowers the string. After removing whitespace, the rest of the logic is much cleaner, and this handles more than just spaces. I keptlen
oversum
becausesum
seemed to obfuscate the logic a bit.return len(odds) == len(s) % 2
seems reasonable but I think going terser at this point has diminishing returns.
$endgroup$
– ggorlen
Aug 12 at 14:51
$begingroup$
Waow, Thank you for a detailed explanation. This is very helpful.
$endgroup$
– Manas Tripathi
Aug 12 at 2:21
$begingroup$
Waow, Thank you for a detailed explanation. This is very helpful.
$endgroup$
– Manas Tripathi
Aug 12 at 2:21
$begingroup$
Why ` s = "".join(s.lower().split())` instead of
s.lower()
or s.casefold()
. And if you do odds = sum(x % 2 for x in Counter(s).values())
,you don't need the len, and can do return odds < 1 + (len(s) % 2)
$endgroup$
– Maarten Fabré
Aug 12 at 8:48
$begingroup$
Why ` s = "".join(s.lower().split())` instead of
s.lower()
or s.casefold()
. And if you do odds = sum(x % 2 for x in Counter(s).values())
,you don't need the len, and can do return odds < 1 + (len(s) % 2)
$endgroup$
– Maarten Fabré
Aug 12 at 8:48
$begingroup$
You can reduce the return logic to
return len(odds) == len(s) % 2
. Even length strings can have no odds, while odd length strings can have one.$endgroup$
– spyr03
Aug 12 at 14:22
$begingroup$
You can reduce the return logic to
return len(odds) == len(s) % 2
. Even length strings can have no odds, while odd length strings can have one.$endgroup$
– spyr03
Aug 12 at 14:22
$begingroup$
Thanks for the feedback.
s = "".join(s.lower().split())
removes whitespace and lowers the string. After removing whitespace, the rest of the logic is much cleaner, and this handles more than just spaces. I kept len
over sum
because sum
seemed to obfuscate the logic a bit. return len(odds) == len(s) % 2
seems reasonable but I think going terser at this point has diminishing returns.$endgroup$
– ggorlen
Aug 12 at 14:51
$begingroup$
Thanks for the feedback.
s = "".join(s.lower().split())
removes whitespace and lowers the string. After removing whitespace, the rest of the logic is much cleaner, and this handles more than just spaces. I kept len
over sum
because sum
seemed to obfuscate the logic a bit. return len(odds) == len(s) % 2
seems reasonable but I think going terser at this point has diminishing returns.$endgroup$
– ggorlen
Aug 12 at 14:51
add a comment |
$begingroup$
Revised from my C# version
Using a set instead of a dictionary or hashtable uses less space
We casefold to ignore case sensitivity and then by sorting this we can try to fail out early. If we have any more than 2 odd letters (which will happen any time we check a third unique char) then we fail out. But we don't want to fail when checking the second member, because it could possibly be removed.
If we didn't fail early we still want to make sure we have at most one odd letter. Therefore the final boolean comparison returns appropriately.
def isPermutedPalindrome(input_string):
if (len(input_string) < 2):
return True
input_string = sorted(input_string.casefold())
char_set = set()
for letter in input_string:
if letter == ' ':
continue
if letter in char_set:
char_set.remove(letter)
else:
char_set.add(letter)
if (len(char_set) > 2):
return False
return (len(char_set) < 2)
$endgroup$
$begingroup$
When answering a question, you should at least try to review the original code in some way or another. Answering in a different language is also confusing and should be avoided.
$endgroup$
– dfhwze
Aug 15 at 19:54
1
$begingroup$
Ok, I wasn't sure how to do it in Python right away, so here's the converted snippet.
$endgroup$
– Drubuntu
Aug 15 at 22:57
1
$begingroup$
Thank's for taking the time to convert the snippet. Could you also explain the differences between your snippet and OP code to make us understand why your solution could be better?
$endgroup$
– dfhwze
Aug 16 at 7:12
$begingroup$
In python, aset
would be a more natural datastructure for thischar_list
than a list. And theletter is ' '
only works because CPython interns a number of strings that occur a lot.letter == ' '
is more universally correct
$endgroup$
– Maarten Fabré
Aug 16 at 7:17
$begingroup$
Thanks for the feedback everyone!
$endgroup$
– Drubuntu
Aug 16 at 18:27
add a comment |
$begingroup$
Revised from my C# version
Using a set instead of a dictionary or hashtable uses less space
We casefold to ignore case sensitivity and then by sorting this we can try to fail out early. If we have any more than 2 odd letters (which will happen any time we check a third unique char) then we fail out. But we don't want to fail when checking the second member, because it could possibly be removed.
If we didn't fail early we still want to make sure we have at most one odd letter. Therefore the final boolean comparison returns appropriately.
def isPermutedPalindrome(input_string):
if (len(input_string) < 2):
return True
input_string = sorted(input_string.casefold())
char_set = set()
for letter in input_string:
if letter == ' ':
continue
if letter in char_set:
char_set.remove(letter)
else:
char_set.add(letter)
if (len(char_set) > 2):
return False
return (len(char_set) < 2)
$endgroup$
$begingroup$
When answering a question, you should at least try to review the original code in some way or another. Answering in a different language is also confusing and should be avoided.
$endgroup$
– dfhwze
Aug 15 at 19:54
1
$begingroup$
Ok, I wasn't sure how to do it in Python right away, so here's the converted snippet.
$endgroup$
– Drubuntu
Aug 15 at 22:57
1
$begingroup$
Thank's for taking the time to convert the snippet. Could you also explain the differences between your snippet and OP code to make us understand why your solution could be better?
$endgroup$
– dfhwze
Aug 16 at 7:12
$begingroup$
In python, aset
would be a more natural datastructure for thischar_list
than a list. And theletter is ' '
only works because CPython interns a number of strings that occur a lot.letter == ' '
is more universally correct
$endgroup$
– Maarten Fabré
Aug 16 at 7:17
$begingroup$
Thanks for the feedback everyone!
$endgroup$
– Drubuntu
Aug 16 at 18:27
add a comment |
$begingroup$
Revised from my C# version
Using a set instead of a dictionary or hashtable uses less space
We casefold to ignore case sensitivity and then by sorting this we can try to fail out early. If we have any more than 2 odd letters (which will happen any time we check a third unique char) then we fail out. But we don't want to fail when checking the second member, because it could possibly be removed.
If we didn't fail early we still want to make sure we have at most one odd letter. Therefore the final boolean comparison returns appropriately.
def isPermutedPalindrome(input_string):
if (len(input_string) < 2):
return True
input_string = sorted(input_string.casefold())
char_set = set()
for letter in input_string:
if letter == ' ':
continue
if letter in char_set:
char_set.remove(letter)
else:
char_set.add(letter)
if (len(char_set) > 2):
return False
return (len(char_set) < 2)
$endgroup$
Revised from my C# version
Using a set instead of a dictionary or hashtable uses less space
We casefold to ignore case sensitivity and then by sorting this we can try to fail out early. If we have any more than 2 odd letters (which will happen any time we check a third unique char) then we fail out. But we don't want to fail when checking the second member, because it could possibly be removed.
If we didn't fail early we still want to make sure we have at most one odd letter. Therefore the final boolean comparison returns appropriately.
def isPermutedPalindrome(input_string):
if (len(input_string) < 2):
return True
input_string = sorted(input_string.casefold())
char_set = set()
for letter in input_string:
if letter == ' ':
continue
if letter in char_set:
char_set.remove(letter)
else:
char_set.add(letter)
if (len(char_set) > 2):
return False
return (len(char_set) < 2)
edited Aug 19 at 16:26
answered Aug 15 at 19:46
DrubuntuDrubuntu
114 bronze badges
114 bronze badges
$begingroup$
When answering a question, you should at least try to review the original code in some way or another. Answering in a different language is also confusing and should be avoided.
$endgroup$
– dfhwze
Aug 15 at 19:54
1
$begingroup$
Ok, I wasn't sure how to do it in Python right away, so here's the converted snippet.
$endgroup$
– Drubuntu
Aug 15 at 22:57
1
$begingroup$
Thank's for taking the time to convert the snippet. Could you also explain the differences between your snippet and OP code to make us understand why your solution could be better?
$endgroup$
– dfhwze
Aug 16 at 7:12
$begingroup$
In python, aset
would be a more natural datastructure for thischar_list
than a list. And theletter is ' '
only works because CPython interns a number of strings that occur a lot.letter == ' '
is more universally correct
$endgroup$
– Maarten Fabré
Aug 16 at 7:17
$begingroup$
Thanks for the feedback everyone!
$endgroup$
– Drubuntu
Aug 16 at 18:27
add a comment |
$begingroup$
When answering a question, you should at least try to review the original code in some way or another. Answering in a different language is also confusing and should be avoided.
$endgroup$
– dfhwze
Aug 15 at 19:54
1
$begingroup$
Ok, I wasn't sure how to do it in Python right away, so here's the converted snippet.
$endgroup$
– Drubuntu
Aug 15 at 22:57
1
$begingroup$
Thank's for taking the time to convert the snippet. Could you also explain the differences between your snippet and OP code to make us understand why your solution could be better?
$endgroup$
– dfhwze
Aug 16 at 7:12
$begingroup$
In python, aset
would be a more natural datastructure for thischar_list
than a list. And theletter is ' '
only works because CPython interns a number of strings that occur a lot.letter == ' '
is more universally correct
$endgroup$
– Maarten Fabré
Aug 16 at 7:17
$begingroup$
Thanks for the feedback everyone!
$endgroup$
– Drubuntu
Aug 16 at 18:27
$begingroup$
When answering a question, you should at least try to review the original code in some way or another. Answering in a different language is also confusing and should be avoided.
$endgroup$
– dfhwze
Aug 15 at 19:54
$begingroup$
When answering a question, you should at least try to review the original code in some way or another. Answering in a different language is also confusing and should be avoided.
$endgroup$
– dfhwze
Aug 15 at 19:54
1
1
$begingroup$
Ok, I wasn't sure how to do it in Python right away, so here's the converted snippet.
$endgroup$
– Drubuntu
Aug 15 at 22:57
$begingroup$
Ok, I wasn't sure how to do it in Python right away, so here's the converted snippet.
$endgroup$
– Drubuntu
Aug 15 at 22:57
1
1
$begingroup$
Thank's for taking the time to convert the snippet. Could you also explain the differences between your snippet and OP code to make us understand why your solution could be better?
$endgroup$
– dfhwze
Aug 16 at 7:12
$begingroup$
Thank's for taking the time to convert the snippet. Could you also explain the differences between your snippet and OP code to make us understand why your solution could be better?
$endgroup$
– dfhwze
Aug 16 at 7:12
$begingroup$
In python, a
set
would be a more natural datastructure for this char_list
than a list. And the letter is ' '
only works because CPython interns a number of strings that occur a lot. letter == ' '
is more universally correct$endgroup$
– Maarten Fabré
Aug 16 at 7:17
$begingroup$
In python, a
set
would be a more natural datastructure for this char_list
than a list. And the letter is ' '
only works because CPython interns a number of strings that occur a lot. letter == ' '
is more universally correct$endgroup$
– Maarten Fabré
Aug 16 at 7:17
$begingroup$
Thanks for the feedback everyone!
$endgroup$
– Drubuntu
Aug 16 at 18:27
$begingroup$
Thanks for the feedback everyone!
$endgroup$
– Drubuntu
Aug 16 at 18:27
add a comment |
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
1
$begingroup$
The logic can be simplified. You only need to verify that at most 1 character has an odd count.
$endgroup$
– Florian F
Aug 12 at 11:28