-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
1.4 Palindrome Permutation #3
Comments
https://repl.it/@megantaylor/PalindromePermutation function isPalindromicPermutation(str) {
let chars = {};
let lowerCaseStr = str.toLowerCase();
if(str.length < 1) {
return false;
}
for(let i = 0; i < str.length; i++){
if(lowerCaseStr[i] !== " ") {
if(chars[lowerCaseStr[i]]) {
chars[lowerCaseStr[i]] += 1;
} else {
chars[lowerCaseStr[i]] = 1;
}
}
}
let numOddChars = Object.values(chars).filter(char => char % 2 !== 0);
return numOddChars.length > 1 ? false : true;
}
function isPalindromicPermutationFancy(string) {
let oddCharacters = [...new Set(string)].filter( (char)=> {
let numberOfOccurences = [...string].filter( (letter) => {
return letter.toLowerCase() === char.toLowerCase() && letter !== " ";
});
return numberOfOccurences.length % 2 === 1;
});
if( oddCharacters.length === 1 || oddCharacters.length === 0 ){
return true;
} else {
return false;
}
} |
https://repl.it/@jtkaufman737/QuickwittedMundaneWatchdog let palindrome = "taco cat"; // should pass
let palindrome2 = "aa"; // should pass
let palindrome3 = "ra ce car"; // should pass
let palindrome4 = "si de car"; // should fail
function isPalindrome(str) {
let keys = {}, vals = [];
Array.from(str.split(" ").join("")).map(m => {
keys[m] = 0; // initial object with letters as keys, each count = 0
})
Array.from(str).map(m => {
keys[m] += 1; // For all array items, increments the count of each letter
})
let odds = Object.values(keys).filter(f => {
return f % 2 === 1; // if MORE THAN ONE odd number is there, it isn't a palindrome
})
odds.length > 1 ? console.log("Sorry, not a palindrome") : console.log("Congrats, its a palindrome!");
}
isPalindrome(palindrome);
isPalindrome(palindrome2);
isPalindrome(palindrome3);
isPalindrome(palindrome4); |
ooh looking at @megantaylor mine would def fail on capital letters! Good thinking |
here's some python: https://repl.it/@jtkaufman737/FrenchZealousTechnologies def testPalin(string):
string = list(string.lower()) # eliminate case issues
odds = 0
# print(string)
for str in string:
if str not in ' ':
keys = { str : 0 }
# print(keys)
for str in string:
if str not in ' ':
keys[str] = keys.get(str, 0) + 1
# print(keys)
for val in keys.values():
if val % 2 == 1:
odds += 1
if odds > 1:
print("Sorry, not a palindrome")
else:
print("Yay, you've got a palindrome!")
testPalin("tacoc AT") # should pass
testPalin("AaBbCcDd") # should pass
testPalin("AaAbBbCcC") # should fail
testPalin("racecar") # should pass
testPalin("sidecar") # should fail |
https://repl.it/@rmorabia/ThoughtfulBoringWorkspace O(n^2) function isPalindrome(str) {
let string = str
.replace(/[^a-z]/gi, "")
.toLowerCase()
.split("");
let justOne = 0;
if (string.length <= 1) {
return "idk i guess this is a palindrome leave me alone 2k18";
}
for (let i = 0; i < string.length; i++) {
let count = 0;
for (let z = 0; z < string.length; z++) {
if (string[i] === string[z]) {
count++;
}
}
if (count % 2 !== 0 && count !== 1) {
return "sry m8 u failed";
} else if (count === 1) {
justOne++;
if (justOne >= 2) {
return "sry m8 u failed";
}
}
}
return "w0w";
}
console.log(isPalindrome("Tact Coa"));
console.log(isPalindrome("abcd"));
console.log(isPalindrome("tttt"));
console.log(isPalindrome("taco bell Lleb oCat"));
console.log(isPalindrome("taco bell Llbb oCat")); EDIT: THE SOLUTION BELOW IS TO CHECK IF A STRING IS A PALINDROME! Because apparently I forgot the problem halfway through! https://repl.it/@rmorabia/EmptyUnrealisticProjector function isPalindrome(str) {
let string = str
.replace(/[^a-z]/gi, "")
.toLowerCase()
.split("");
if (string.length <= 1) {
return "do a better string next time";
}
for (let i = 0; i < string.length;) {
if (string[0] == string[string.length - 1]) {
string.shift();
string.pop();
} else {
return "do a better string next time"
}
}
if (string.length <= 1) {
return "w0w much p4l1ndr0m3";
} else {
return "do a better string next time";
}
}
console.log(isPalindrome("madam"))
console.log(isPalindrome("taco bell Lleb oCat"));
console.log(isPalindrome("taco bell Llbb oCat")); |
https://repl.it/@lpatmo/FilthyTimelySale function palPerm(str) {
//Check if there is an even number of each possible letter
//It is a permutation of a palindrome IF after you remove each pair of letters, there is 0 or 1 chars remaining
//Create temp string
//Loop through original str
//If I do not see it in temp, add it to temp.
//If I see it in temp, remove it from temp.
//At the end, the temp string will hold ALL of the odd counts of chars.
//If the length of temp string is greater than 1, return false.
let temp = [];
for (let i = 0; i < str.length; i++) {
let charPosition = temp.indexOf(str[i]);
if ( charPosition > -1 ) {
temp.splice(charPosition, 1);
} else if (str[i] !== ' ') {
temp.push(str[i]);
}
}
//console.log(temp)
return temp.length < 2
}
console.log(palPerm("tcao cat")) |
|
The text was updated successfully, but these errors were encountered: