Palindrome and Reverse a String Problems (JavaScript, Python)Reverse and Add: find a palindromeMake palindrome finder-builder function fasterString reverse functionRosalind string algorithm problemsComparing a string using a stackTest if a string is a palindromeReverse string in JavaScript without using reverse()Reverse vowels in a stringReverse complement of a DNA string

How to persuade players not to cheat?

Which Formula One races did Scuderia Ferrari not take part in?

What does it take to make metal music?

In what way were Renaissance battles like chess matches?

Instant coffee melts like chocolate

Why the real and imaginary parts of a complex analytic function are not independent?

How to check if python package is latest version programmatically?

A lightweight version of std::shared_ptr<T>

Cannot upgrade 19.04 to 19.10 because of held back packages: libsnmp30

Potential Postdoc advisor giving exams (assignment) as part of hiring process

Has an engineer called Trevor Jackson invented a revolutionary battery allowing for a car range of 1500 miles?

Would rocket engine exhaust create pockets of gas in space which could hinder further space exploration?

Modification of a public property - LWC

How can I prevent side-channel attacks against authentication?

What do you call the movement you do by car within a local distance?

Is a turbocharged piston aircraft the same thing as turboprop?

Is this bible in Koine Greek?

Gold plating versus Silver plating for electrical contacts?

What potential problems are there with dumping dex in bard build?

ASCII Pumpkin Carving

Hikers in Halloween

Have supporters of "right-wing populist" parties in Europe explained why they trust Trump?

Cheat at Rock-Paper-Scissors-Lizard-Spock

Is an SSH key with a passphrase a 2FA?



Palindrome and Reverse a String Problems (JavaScript, Python)


Reverse and Add: find a palindromeMake palindrome finder-builder function fasterString reverse functionRosalind string algorithm problemsComparing a string using a stackTest if a string is a palindromeReverse string in JavaScript without using reverse()Reverse vowels in a stringReverse complement of a DNA string






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty
margin-bottom:0;









4














$begingroup$


Problem 1



Given a string, the task is to reverse the string and return a reversed string.



Problem 2



Given a string, return true if the string is a palindrome or false if it is not.



Code



Just for practicing, I've implemented reverse string and palindrome problems using JavaScript and Python. If you'd like to review the codes and provide any change/improvement recommendations, please do so and I appreciate that.



JavaScript






// Problem 1
// Given a string, return true if the string is a palindrome
// or false if it is not. Palindromes are strings that
// form the same word if it is reversed. *Do* include spaces
// and punctuation in determining if the string is a palindrome.
// --- Examples:
// abba => true
// abc => false

// Problem 2
// Given a string, return a new string with the reversed
// order of characters
// --- Examples
// apple => leppa
// hello => olleh
// Greetings! => !sgniteerG



// This method tests if the string is a palindrome using four reverse string functions
function check_palindrome(original_string)
original_string = original_string.toLowerCase();
let reversed_string_1 = reverse_string_built_in(original_string);
let reversed_string_2 = reverse_string_reduce(original_string);
let reversed_string_3 = reverse_string_for_of_loop(original_string);
let reversed_string_4 = reverse_string_negative_for_loop(original_string);

// If the original string is a palindrome
if (reversed_string_1 === original_string && reversed_string_2 === original_string && reversed_string_3 === original_string && reversed_string_4 === original_string)
return true;
// If the original string is not a palindrome
else
return false;



// This method is an slightly optimized version of checking for palindrome
function check_palindrome_optimized(original_string)
original_string = original_string.toLowerCase();
count = 0;
return original_string.split('').every((char, i) =>
count++;
if (count > Math.floor(original_string.length / 2))
return true

return char === original_string[original_string.length - i - 1];
);


// This method uses reverse() built in function
function reverse_string_built_in(original_string)
return original_string.split('').reverse().join('');


// This method uses for of loop
function reverse_string_for_of_loop(original_string)
let reversed_string = '';

for (let char of original_string)
reversed_string = char + reversed_string;


return reversed_string;


// Using negative for loop and contact
function reverse_string_negative_for_loop(original_string)
let reversed_string = '';

for (var i = original_string.length - 1; i >= 0; i--)
reversed_string = reversed_string.concat(original_string[i]);

return reversed_string;



//Using reduce() method
function reverse_string_reduce(original_string)
return original_string.split('').reduce((reversed_string, char) => char + reversed_string, '');



// Here, we are testing our palindrome words
words_array = ['apple', 'kayak', 'abc1221cba', 'reviver', 'redivider', '1736', 'deified', 'Anna', 'Able was I ere I saw Elba', "Madam, I'm Adam", 'civic', 'RaDar', 'Never odd or even', 'LeVeL', 'kayak', 'racecar', 'Doc, note: I dissent. A fast never prevents a fatness. I diet on cod', 'redder', 'madam', '1991', 'refer'];

for (let word of words_array)
if (check_palindrome_optimized(word) === true && check_palindrome(word) === true)
console.log('🍏 "'.concat(word, '" is a palindrome!'));
else
console.log('🍎 "'.concat(word, '" is not a palindrome!'));






Python



def reverse_string(original_string):
'''
Returns a reversed string give a string
'''
reversed_string = ''
for char in original_string:
reversed_string = char + reversed_string

return reversed_string


def reverse_string_slice(original_string):
'''Returns a reversed string give a string'''
return original_string[::-1]


def check_palindrome(original_string):
'''Returns true if an input string is a palindrome'''
original_string = original_string.lower()
if reverse_string_slice(original_string) == original_string and reverse_string(original_string) == original_string:
return True
return False


# Here, we are testing our palindrome words
words_array = ['apple', 'kayak', 'abc1221cba', 'reviver', 'redivider', '1736', 'deified', 'Anna', 'Able was I ere I saw Elba', "Madam, I'm Adam", 'civic', 'RaDar',
'Never odd or even', 'LeVeL', 'kayak', 'racecar', 'Doc, note: I dissent. A fast never prevents a fatness. I diet on cod', 'redder', 'madam', '1991', 'refer']


delimiter = '-'*50

for word in words_array:
print(delimiter)
print(f'The reverse of "word" is "reverse_string(word)"')
if check_palindrome(word) == True:
print(f'🍏 "word" is a palindrome!')
else:
print(f'🍎 "word" is not a palindrome!')


Output



--------------------------------------------------
The reverse of "apple" is "elppa"
🍎 "apple" is not a palindrome!
--------------------------------------------------
The reverse of "kayak" is "kayak"
🍏 "kayak" is a palindrome!
--------------------------------------------------
The reverse of "abc1221cba" is "abc1221cba"
🍏 "abc1221cba" is a palindrome!
--------------------------------------------------
The reverse of "reviver" is "reviver"
🍏 "reviver" is a palindrome!
--------------------------------------------------
The reverse of "redivider" is "redivider"
🍏 "redivider" is a palindrome!
--------------------------------------------------
The reverse of "1736" is "6371"
🍎 "1736" is not a palindrome!
--------------------------------------------------
The reverse of "deified" is "deified"
🍏 "deified" is a palindrome!
--------------------------------------------------
The reverse of "Anna" is "annA"
🍏 "Anna" is a palindrome!
--------------------------------------------------
The reverse of "Able was I ere I saw Elba" is "ablE was I ere I saw elbA"
🍏 "Able was I ere I saw Elba" is a palindrome!
--------------------------------------------------
The reverse of "Madam, I'm Adam" is "madA m'I ,madaM"
🍎 "Madam, I'm Adam" is not a palindrome!
--------------------------------------------------
The reverse of "civic" is "civic"
🍏 "civic" is a palindrome!
--------------------------------------------------
The reverse of "RaDar" is "raDaR"
🍏 "RaDar" is a palindrome!
--------------------------------------------------
The reverse of "Never odd or even" is "neve ro ddo reveN"
🍎 "Never odd or even" is not a palindrome!
--------------------------------------------------
The reverse of "LeVeL" is "LeVeL"
🍏 "LeVeL" is a palindrome!
--------------------------------------------------
The reverse of "kayak" is "kayak"
🍏 "kayak" is a palindrome!
--------------------------------------------------
The reverse of "racecar" is "racecar"
🍏 "racecar" is a palindrome!
--------------------------------------------------
The reverse of "Doc, note: I dissent. A fast never prevents a fatness. I diet on cod" is "doc no teid I .ssentaf a stneverp reven tsaf A .tnessid I :eton ,coD"
🍎 "Doc, note: I dissent. A fast never prevents a fatness. I diet on cod" is not a palindrome!
--------------------------------------------------
The reverse of "redder" is "redder"
🍏 "redder" is a palindrome!
--------------------------------------------------
The reverse of "madam" is "madam"
🍏 "madam" is a palindrome!
--------------------------------------------------
The reverse of "1991" is "1991"
🍏 "1991" is a palindrome!
--------------------------------------------------
The reverse of "refer" is "refer"
🍏 "refer" is a palindrome!









share|improve this question












$endgroup$










  • 1




    $begingroup$
    I presume, by "string" you mean "a sequence of Latin letters"? Otherwise, your problems would be much harder to solve. See this comment: stackoverflow.com/a/16776621/989121
    $endgroup$
    – georg
    Oct 15 at 7:19






  • 1




    $begingroup$
    Typically, punctuation is ignored when testing for palindromes, so "Doc, note:..." should return true. Likewise, "Never odd or even" should return true.
    $endgroup$
    – Baldrickk
    Oct 15 at 10:47







  • 1




    $begingroup$
    You could also go one step further here and add a reverse method to the String prototype (in js) so you could do: "test".reverse(). This keeps the code nicely encapsulated and re-usable. With it being on the prototype, every instance gets the same function.
    $endgroup$
    – Magrangs
    Oct 15 at 15:16


















4














$begingroup$


Problem 1



Given a string, the task is to reverse the string and return a reversed string.



Problem 2



Given a string, return true if the string is a palindrome or false if it is not.



Code



Just for practicing, I've implemented reverse string and palindrome problems using JavaScript and Python. If you'd like to review the codes and provide any change/improvement recommendations, please do so and I appreciate that.



JavaScript






// Problem 1
// Given a string, return true if the string is a palindrome
// or false if it is not. Palindromes are strings that
// form the same word if it is reversed. *Do* include spaces
// and punctuation in determining if the string is a palindrome.
// --- Examples:
// abba => true
// abc => false

// Problem 2
// Given a string, return a new string with the reversed
// order of characters
// --- Examples
// apple => leppa
// hello => olleh
// Greetings! => !sgniteerG



// This method tests if the string is a palindrome using four reverse string functions
function check_palindrome(original_string)
original_string = original_string.toLowerCase();
let reversed_string_1 = reverse_string_built_in(original_string);
let reversed_string_2 = reverse_string_reduce(original_string);
let reversed_string_3 = reverse_string_for_of_loop(original_string);
let reversed_string_4 = reverse_string_negative_for_loop(original_string);

// If the original string is a palindrome
if (reversed_string_1 === original_string && reversed_string_2 === original_string && reversed_string_3 === original_string && reversed_string_4 === original_string)
return true;
// If the original string is not a palindrome
else
return false;



// This method is an slightly optimized version of checking for palindrome
function check_palindrome_optimized(original_string)
original_string = original_string.toLowerCase();
count = 0;
return original_string.split('').every((char, i) =>
count++;
if (count > Math.floor(original_string.length / 2))
return true

return char === original_string[original_string.length - i - 1];
);


// This method uses reverse() built in function
function reverse_string_built_in(original_string)
return original_string.split('').reverse().join('');


// This method uses for of loop
function reverse_string_for_of_loop(original_string)
let reversed_string = '';

for (let char of original_string)
reversed_string = char + reversed_string;


return reversed_string;


// Using negative for loop and contact
function reverse_string_negative_for_loop(original_string)
let reversed_string = '';

for (var i = original_string.length - 1; i >= 0; i--)
reversed_string = reversed_string.concat(original_string[i]);

return reversed_string;



//Using reduce() method
function reverse_string_reduce(original_string)
return original_string.split('').reduce((reversed_string, char) => char + reversed_string, '');



// Here, we are testing our palindrome words
words_array = ['apple', 'kayak', 'abc1221cba', 'reviver', 'redivider', '1736', 'deified', 'Anna', 'Able was I ere I saw Elba', "Madam, I'm Adam", 'civic', 'RaDar', 'Never odd or even', 'LeVeL', 'kayak', 'racecar', 'Doc, note: I dissent. A fast never prevents a fatness. I diet on cod', 'redder', 'madam', '1991', 'refer'];

for (let word of words_array)
if (check_palindrome_optimized(word) === true && check_palindrome(word) === true)
console.log('🍏 "'.concat(word, '" is a palindrome!'));
else
console.log('🍎 "'.concat(word, '" is not a palindrome!'));






Python



def reverse_string(original_string):
'''
Returns a reversed string give a string
'''
reversed_string = ''
for char in original_string:
reversed_string = char + reversed_string

return reversed_string


def reverse_string_slice(original_string):
'''Returns a reversed string give a string'''
return original_string[::-1]


def check_palindrome(original_string):
'''Returns true if an input string is a palindrome'''
original_string = original_string.lower()
if reverse_string_slice(original_string) == original_string and reverse_string(original_string) == original_string:
return True
return False


# Here, we are testing our palindrome words
words_array = ['apple', 'kayak', 'abc1221cba', 'reviver', 'redivider', '1736', 'deified', 'Anna', 'Able was I ere I saw Elba', "Madam, I'm Adam", 'civic', 'RaDar',
'Never odd or even', 'LeVeL', 'kayak', 'racecar', 'Doc, note: I dissent. A fast never prevents a fatness. I diet on cod', 'redder', 'madam', '1991', 'refer']


delimiter = '-'*50

for word in words_array:
print(delimiter)
print(f'The reverse of "word" is "reverse_string(word)"')
if check_palindrome(word) == True:
print(f'🍏 "word" is a palindrome!')
else:
print(f'🍎 "word" is not a palindrome!')


Output



--------------------------------------------------
The reverse of "apple" is "elppa"
🍎 "apple" is not a palindrome!
--------------------------------------------------
The reverse of "kayak" is "kayak"
🍏 "kayak" is a palindrome!
--------------------------------------------------
The reverse of "abc1221cba" is "abc1221cba"
🍏 "abc1221cba" is a palindrome!
--------------------------------------------------
The reverse of "reviver" is "reviver"
🍏 "reviver" is a palindrome!
--------------------------------------------------
The reverse of "redivider" is "redivider"
🍏 "redivider" is a palindrome!
--------------------------------------------------
The reverse of "1736" is "6371"
🍎 "1736" is not a palindrome!
--------------------------------------------------
The reverse of "deified" is "deified"
🍏 "deified" is a palindrome!
--------------------------------------------------
The reverse of "Anna" is "annA"
🍏 "Anna" is a palindrome!
--------------------------------------------------
The reverse of "Able was I ere I saw Elba" is "ablE was I ere I saw elbA"
🍏 "Able was I ere I saw Elba" is a palindrome!
--------------------------------------------------
The reverse of "Madam, I'm Adam" is "madA m'I ,madaM"
🍎 "Madam, I'm Adam" is not a palindrome!
--------------------------------------------------
The reverse of "civic" is "civic"
🍏 "civic" is a palindrome!
--------------------------------------------------
The reverse of "RaDar" is "raDaR"
🍏 "RaDar" is a palindrome!
--------------------------------------------------
The reverse of "Never odd or even" is "neve ro ddo reveN"
🍎 "Never odd or even" is not a palindrome!
--------------------------------------------------
The reverse of "LeVeL" is "LeVeL"
🍏 "LeVeL" is a palindrome!
--------------------------------------------------
The reverse of "kayak" is "kayak"
🍏 "kayak" is a palindrome!
--------------------------------------------------
The reverse of "racecar" is "racecar"
🍏 "racecar" is a palindrome!
--------------------------------------------------
The reverse of "Doc, note: I dissent. A fast never prevents a fatness. I diet on cod" is "doc no teid I .ssentaf a stneverp reven tsaf A .tnessid I :eton ,coD"
🍎 "Doc, note: I dissent. A fast never prevents a fatness. I diet on cod" is not a palindrome!
--------------------------------------------------
The reverse of "redder" is "redder"
🍏 "redder" is a palindrome!
--------------------------------------------------
The reverse of "madam" is "madam"
🍏 "madam" is a palindrome!
--------------------------------------------------
The reverse of "1991" is "1991"
🍏 "1991" is a palindrome!
--------------------------------------------------
The reverse of "refer" is "refer"
🍏 "refer" is a palindrome!









share|improve this question












$endgroup$










  • 1




    $begingroup$
    I presume, by "string" you mean "a sequence of Latin letters"? Otherwise, your problems would be much harder to solve. See this comment: stackoverflow.com/a/16776621/989121
    $endgroup$
    – georg
    Oct 15 at 7:19






  • 1




    $begingroup$
    Typically, punctuation is ignored when testing for palindromes, so "Doc, note:..." should return true. Likewise, "Never odd or even" should return true.
    $endgroup$
    – Baldrickk
    Oct 15 at 10:47







  • 1




    $begingroup$
    You could also go one step further here and add a reverse method to the String prototype (in js) so you could do: "test".reverse(). This keeps the code nicely encapsulated and re-usable. With it being on the prototype, every instance gets the same function.
    $endgroup$
    – Magrangs
    Oct 15 at 15:16














4












4








4


1



$begingroup$


Problem 1



Given a string, the task is to reverse the string and return a reversed string.



Problem 2



Given a string, return true if the string is a palindrome or false if it is not.



Code



Just for practicing, I've implemented reverse string and palindrome problems using JavaScript and Python. If you'd like to review the codes and provide any change/improvement recommendations, please do so and I appreciate that.



JavaScript






// Problem 1
// Given a string, return true if the string is a palindrome
// or false if it is not. Palindromes are strings that
// form the same word if it is reversed. *Do* include spaces
// and punctuation in determining if the string is a palindrome.
// --- Examples:
// abba => true
// abc => false

// Problem 2
// Given a string, return a new string with the reversed
// order of characters
// --- Examples
// apple => leppa
// hello => olleh
// Greetings! => !sgniteerG



// This method tests if the string is a palindrome using four reverse string functions
function check_palindrome(original_string)
original_string = original_string.toLowerCase();
let reversed_string_1 = reverse_string_built_in(original_string);
let reversed_string_2 = reverse_string_reduce(original_string);
let reversed_string_3 = reverse_string_for_of_loop(original_string);
let reversed_string_4 = reverse_string_negative_for_loop(original_string);

// If the original string is a palindrome
if (reversed_string_1 === original_string && reversed_string_2 === original_string && reversed_string_3 === original_string && reversed_string_4 === original_string)
return true;
// If the original string is not a palindrome
else
return false;



// This method is an slightly optimized version of checking for palindrome
function check_palindrome_optimized(original_string)
original_string = original_string.toLowerCase();
count = 0;
return original_string.split('').every((char, i) =>
count++;
if (count > Math.floor(original_string.length / 2))
return true

return char === original_string[original_string.length - i - 1];
);


// This method uses reverse() built in function
function reverse_string_built_in(original_string)
return original_string.split('').reverse().join('');


// This method uses for of loop
function reverse_string_for_of_loop(original_string)
let reversed_string = '';

for (let char of original_string)
reversed_string = char + reversed_string;


return reversed_string;


// Using negative for loop and contact
function reverse_string_negative_for_loop(original_string)
let reversed_string = '';

for (var i = original_string.length - 1; i >= 0; i--)
reversed_string = reversed_string.concat(original_string[i]);

return reversed_string;



//Using reduce() method
function reverse_string_reduce(original_string)
return original_string.split('').reduce((reversed_string, char) => char + reversed_string, '');



// Here, we are testing our palindrome words
words_array = ['apple', 'kayak', 'abc1221cba', 'reviver', 'redivider', '1736', 'deified', 'Anna', 'Able was I ere I saw Elba', "Madam, I'm Adam", 'civic', 'RaDar', 'Never odd or even', 'LeVeL', 'kayak', 'racecar', 'Doc, note: I dissent. A fast never prevents a fatness. I diet on cod', 'redder', 'madam', '1991', 'refer'];

for (let word of words_array)
if (check_palindrome_optimized(word) === true && check_palindrome(word) === true)
console.log('🍏 "'.concat(word, '" is a palindrome!'));
else
console.log('🍎 "'.concat(word, '" is not a palindrome!'));






Python



def reverse_string(original_string):
'''
Returns a reversed string give a string
'''
reversed_string = ''
for char in original_string:
reversed_string = char + reversed_string

return reversed_string


def reverse_string_slice(original_string):
'''Returns a reversed string give a string'''
return original_string[::-1]


def check_palindrome(original_string):
'''Returns true if an input string is a palindrome'''
original_string = original_string.lower()
if reverse_string_slice(original_string) == original_string and reverse_string(original_string) == original_string:
return True
return False


# Here, we are testing our palindrome words
words_array = ['apple', 'kayak', 'abc1221cba', 'reviver', 'redivider', '1736', 'deified', 'Anna', 'Able was I ere I saw Elba', "Madam, I'm Adam", 'civic', 'RaDar',
'Never odd or even', 'LeVeL', 'kayak', 'racecar', 'Doc, note: I dissent. A fast never prevents a fatness. I diet on cod', 'redder', 'madam', '1991', 'refer']


delimiter = '-'*50

for word in words_array:
print(delimiter)
print(f'The reverse of "word" is "reverse_string(word)"')
if check_palindrome(word) == True:
print(f'🍏 "word" is a palindrome!')
else:
print(f'🍎 "word" is not a palindrome!')


Output



--------------------------------------------------
The reverse of "apple" is "elppa"
🍎 "apple" is not a palindrome!
--------------------------------------------------
The reverse of "kayak" is "kayak"
🍏 "kayak" is a palindrome!
--------------------------------------------------
The reverse of "abc1221cba" is "abc1221cba"
🍏 "abc1221cba" is a palindrome!
--------------------------------------------------
The reverse of "reviver" is "reviver"
🍏 "reviver" is a palindrome!
--------------------------------------------------
The reverse of "redivider" is "redivider"
🍏 "redivider" is a palindrome!
--------------------------------------------------
The reverse of "1736" is "6371"
🍎 "1736" is not a palindrome!
--------------------------------------------------
The reverse of "deified" is "deified"
🍏 "deified" is a palindrome!
--------------------------------------------------
The reverse of "Anna" is "annA"
🍏 "Anna" is a palindrome!
--------------------------------------------------
The reverse of "Able was I ere I saw Elba" is "ablE was I ere I saw elbA"
🍏 "Able was I ere I saw Elba" is a palindrome!
--------------------------------------------------
The reverse of "Madam, I'm Adam" is "madA m'I ,madaM"
🍎 "Madam, I'm Adam" is not a palindrome!
--------------------------------------------------
The reverse of "civic" is "civic"
🍏 "civic" is a palindrome!
--------------------------------------------------
The reverse of "RaDar" is "raDaR"
🍏 "RaDar" is a palindrome!
--------------------------------------------------
The reverse of "Never odd or even" is "neve ro ddo reveN"
🍎 "Never odd or even" is not a palindrome!
--------------------------------------------------
The reverse of "LeVeL" is "LeVeL"
🍏 "LeVeL" is a palindrome!
--------------------------------------------------
The reverse of "kayak" is "kayak"
🍏 "kayak" is a palindrome!
--------------------------------------------------
The reverse of "racecar" is "racecar"
🍏 "racecar" is a palindrome!
--------------------------------------------------
The reverse of "Doc, note: I dissent. A fast never prevents a fatness. I diet on cod" is "doc no teid I .ssentaf a stneverp reven tsaf A .tnessid I :eton ,coD"
🍎 "Doc, note: I dissent. A fast never prevents a fatness. I diet on cod" is not a palindrome!
--------------------------------------------------
The reverse of "redder" is "redder"
🍏 "redder" is a palindrome!
--------------------------------------------------
The reverse of "madam" is "madam"
🍏 "madam" is a palindrome!
--------------------------------------------------
The reverse of "1991" is "1991"
🍏 "1991" is a palindrome!
--------------------------------------------------
The reverse of "refer" is "refer"
🍏 "refer" is a palindrome!









share|improve this question












$endgroup$




Problem 1



Given a string, the task is to reverse the string and return a reversed string.



Problem 2



Given a string, return true if the string is a palindrome or false if it is not.



Code



Just for practicing, I've implemented reverse string and palindrome problems using JavaScript and Python. If you'd like to review the codes and provide any change/improvement recommendations, please do so and I appreciate that.



JavaScript






// Problem 1
// Given a string, return true if the string is a palindrome
// or false if it is not. Palindromes are strings that
// form the same word if it is reversed. *Do* include spaces
// and punctuation in determining if the string is a palindrome.
// --- Examples:
// abba => true
// abc => false

// Problem 2
// Given a string, return a new string with the reversed
// order of characters
// --- Examples
// apple => leppa
// hello => olleh
// Greetings! => !sgniteerG



// This method tests if the string is a palindrome using four reverse string functions
function check_palindrome(original_string)
original_string = original_string.toLowerCase();
let reversed_string_1 = reverse_string_built_in(original_string);
let reversed_string_2 = reverse_string_reduce(original_string);
let reversed_string_3 = reverse_string_for_of_loop(original_string);
let reversed_string_4 = reverse_string_negative_for_loop(original_string);

// If the original string is a palindrome
if (reversed_string_1 === original_string && reversed_string_2 === original_string && reversed_string_3 === original_string && reversed_string_4 === original_string)
return true;
// If the original string is not a palindrome
else
return false;



// This method is an slightly optimized version of checking for palindrome
function check_palindrome_optimized(original_string)
original_string = original_string.toLowerCase();
count = 0;
return original_string.split('').every((char, i) =>
count++;
if (count > Math.floor(original_string.length / 2))
return true

return char === original_string[original_string.length - i - 1];
);


// This method uses reverse() built in function
function reverse_string_built_in(original_string)
return original_string.split('').reverse().join('');


// This method uses for of loop
function reverse_string_for_of_loop(original_string)
let reversed_string = '';

for (let char of original_string)
reversed_string = char + reversed_string;


return reversed_string;


// Using negative for loop and contact
function reverse_string_negative_for_loop(original_string)
let reversed_string = '';

for (var i = original_string.length - 1; i >= 0; i--)
reversed_string = reversed_string.concat(original_string[i]);

return reversed_string;



//Using reduce() method
function reverse_string_reduce(original_string)
return original_string.split('').reduce((reversed_string, char) => char + reversed_string, '');



// Here, we are testing our palindrome words
words_array = ['apple', 'kayak', 'abc1221cba', 'reviver', 'redivider', '1736', 'deified', 'Anna', 'Able was I ere I saw Elba', "Madam, I'm Adam", 'civic', 'RaDar', 'Never odd or even', 'LeVeL', 'kayak', 'racecar', 'Doc, note: I dissent. A fast never prevents a fatness. I diet on cod', 'redder', 'madam', '1991', 'refer'];

for (let word of words_array)
if (check_palindrome_optimized(word) === true && check_palindrome(word) === true)
console.log('🍏 "'.concat(word, '" is a palindrome!'));
else
console.log('🍎 "'.concat(word, '" is not a palindrome!'));






Python



def reverse_string(original_string):
'''
Returns a reversed string give a string
'''
reversed_string = ''
for char in original_string:
reversed_string = char + reversed_string

return reversed_string


def reverse_string_slice(original_string):
'''Returns a reversed string give a string'''
return original_string[::-1]


def check_palindrome(original_string):
'''Returns true if an input string is a palindrome'''
original_string = original_string.lower()
if reverse_string_slice(original_string) == original_string and reverse_string(original_string) == original_string:
return True
return False


# Here, we are testing our palindrome words
words_array = ['apple', 'kayak', 'abc1221cba', 'reviver', 'redivider', '1736', 'deified', 'Anna', 'Able was I ere I saw Elba', "Madam, I'm Adam", 'civic', 'RaDar',
'Never odd or even', 'LeVeL', 'kayak', 'racecar', 'Doc, note: I dissent. A fast never prevents a fatness. I diet on cod', 'redder', 'madam', '1991', 'refer']


delimiter = '-'*50

for word in words_array:
print(delimiter)
print(f'The reverse of "word" is "reverse_string(word)"')
if check_palindrome(word) == True:
print(f'🍏 "word" is a palindrome!')
else:
print(f'🍎 "word" is not a palindrome!')


Output



--------------------------------------------------
The reverse of "apple" is "elppa"
🍎 "apple" is not a palindrome!
--------------------------------------------------
The reverse of "kayak" is "kayak"
🍏 "kayak" is a palindrome!
--------------------------------------------------
The reverse of "abc1221cba" is "abc1221cba"
🍏 "abc1221cba" is a palindrome!
--------------------------------------------------
The reverse of "reviver" is "reviver"
🍏 "reviver" is a palindrome!
--------------------------------------------------
The reverse of "redivider" is "redivider"
🍏 "redivider" is a palindrome!
--------------------------------------------------
The reverse of "1736" is "6371"
🍎 "1736" is not a palindrome!
--------------------------------------------------
The reverse of "deified" is "deified"
🍏 "deified" is a palindrome!
--------------------------------------------------
The reverse of "Anna" is "annA"
🍏 "Anna" is a palindrome!
--------------------------------------------------
The reverse of "Able was I ere I saw Elba" is "ablE was I ere I saw elbA"
🍏 "Able was I ere I saw Elba" is a palindrome!
--------------------------------------------------
The reverse of "Madam, I'm Adam" is "madA m'I ,madaM"
🍎 "Madam, I'm Adam" is not a palindrome!
--------------------------------------------------
The reverse of "civic" is "civic"
🍏 "civic" is a palindrome!
--------------------------------------------------
The reverse of "RaDar" is "raDaR"
🍏 "RaDar" is a palindrome!
--------------------------------------------------
The reverse of "Never odd or even" is "neve ro ddo reveN"
🍎 "Never odd or even" is not a palindrome!
--------------------------------------------------
The reverse of "LeVeL" is "LeVeL"
🍏 "LeVeL" is a palindrome!
--------------------------------------------------
The reverse of "kayak" is "kayak"
🍏 "kayak" is a palindrome!
--------------------------------------------------
The reverse of "racecar" is "racecar"
🍏 "racecar" is a palindrome!
--------------------------------------------------
The reverse of "Doc, note: I dissent. A fast never prevents a fatness. I diet on cod" is "doc no teid I .ssentaf a stneverp reven tsaf A .tnessid I :eton ,coD"
🍎 "Doc, note: I dissent. A fast never prevents a fatness. I diet on cod" is not a palindrome!
--------------------------------------------------
The reverse of "redder" is "redder"
🍏 "redder" is a palindrome!
--------------------------------------------------
The reverse of "madam" is "madam"
🍏 "madam" is a palindrome!
--------------------------------------------------
The reverse of "1991" is "1991"
🍏 "1991" is a palindrome!
--------------------------------------------------
The reverse of "refer" is "refer"
🍏 "refer" is a palindrome!





// Problem 1
// Given a string, return true if the string is a palindrome
// or false if it is not. Palindromes are strings that
// form the same word if it is reversed. *Do* include spaces
// and punctuation in determining if the string is a palindrome.
// --- Examples:
// abba => true
// abc => false

// Problem 2
// Given a string, return a new string with the reversed
// order of characters
// --- Examples
// apple => leppa
// hello => olleh
// Greetings! => !sgniteerG



// This method tests if the string is a palindrome using four reverse string functions
function check_palindrome(original_string)
original_string = original_string.toLowerCase();
let reversed_string_1 = reverse_string_built_in(original_string);
let reversed_string_2 = reverse_string_reduce(original_string);
let reversed_string_3 = reverse_string_for_of_loop(original_string);
let reversed_string_4 = reverse_string_negative_for_loop(original_string);

// If the original string is a palindrome
if (reversed_string_1 === original_string && reversed_string_2 === original_string && reversed_string_3 === original_string && reversed_string_4 === original_string)
return true;
// If the original string is not a palindrome
else
return false;



// This method is an slightly optimized version of checking for palindrome
function check_palindrome_optimized(original_string)
original_string = original_string.toLowerCase();
count = 0;
return original_string.split('').every((char, i) =>
count++;
if (count > Math.floor(original_string.length / 2))
return true

return char === original_string[original_string.length - i - 1];
);


// This method uses reverse() built in function
function reverse_string_built_in(original_string)
return original_string.split('').reverse().join('');


// This method uses for of loop
function reverse_string_for_of_loop(original_string)
let reversed_string = '';

for (let char of original_string)
reversed_string = char + reversed_string;


return reversed_string;


// Using negative for loop and contact
function reverse_string_negative_for_loop(original_string)
let reversed_string = '';

for (var i = original_string.length - 1; i >= 0; i--)
reversed_string = reversed_string.concat(original_string[i]);

return reversed_string;



//Using reduce() method
function reverse_string_reduce(original_string)
return original_string.split('').reduce((reversed_string, char) => char + reversed_string, '');



// Here, we are testing our palindrome words
words_array = ['apple', 'kayak', 'abc1221cba', 'reviver', 'redivider', '1736', 'deified', 'Anna', 'Able was I ere I saw Elba', "Madam, I'm Adam", 'civic', 'RaDar', 'Never odd or even', 'LeVeL', 'kayak', 'racecar', 'Doc, note: I dissent. A fast never prevents a fatness. I diet on cod', 'redder', 'madam', '1991', 'refer'];

for (let word of words_array)
if (check_palindrome_optimized(word) === true && check_palindrome(word) === true)
console.log('🍏 "'.concat(word, '" is a palindrome!'));
else
console.log('🍎 "'.concat(word, '" is not a palindrome!'));






// Problem 1
// Given a string, return true if the string is a palindrome
// or false if it is not. Palindromes are strings that
// form the same word if it is reversed. *Do* include spaces
// and punctuation in determining if the string is a palindrome.
// --- Examples:
// abba => true
// abc => false

// Problem 2
// Given a string, return a new string with the reversed
// order of characters
// --- Examples
// apple => leppa
// hello => olleh
// Greetings! => !sgniteerG



// This method tests if the string is a palindrome using four reverse string functions
function check_palindrome(original_string)
original_string = original_string.toLowerCase();
let reversed_string_1 = reverse_string_built_in(original_string);
let reversed_string_2 = reverse_string_reduce(original_string);
let reversed_string_3 = reverse_string_for_of_loop(original_string);
let reversed_string_4 = reverse_string_negative_for_loop(original_string);

// If the original string is a palindrome
if (reversed_string_1 === original_string && reversed_string_2 === original_string && reversed_string_3 === original_string && reversed_string_4 === original_string)
return true;
// If the original string is not a palindrome
else
return false;



// This method is an slightly optimized version of checking for palindrome
function check_palindrome_optimized(original_string)
original_string = original_string.toLowerCase();
count = 0;
return original_string.split('').every((char, i) =>
count++;
if (count > Math.floor(original_string.length / 2))
return true

return char === original_string[original_string.length - i - 1];
);


// This method uses reverse() built in function
function reverse_string_built_in(original_string)
return original_string.split('').reverse().join('');


// This method uses for of loop
function reverse_string_for_of_loop(original_string)
let reversed_string = '';

for (let char of original_string)
reversed_string = char + reversed_string;


return reversed_string;


// Using negative for loop and contact
function reverse_string_negative_for_loop(original_string)
let reversed_string = '';

for (var i = original_string.length - 1; i >= 0; i--)
reversed_string = reversed_string.concat(original_string[i]);

return reversed_string;



//Using reduce() method
function reverse_string_reduce(original_string)
return original_string.split('').reduce((reversed_string, char) => char + reversed_string, '');



// Here, we are testing our palindrome words
words_array = ['apple', 'kayak', 'abc1221cba', 'reviver', 'redivider', '1736', 'deified', 'Anna', 'Able was I ere I saw Elba', "Madam, I'm Adam", 'civic', 'RaDar', 'Never odd or even', 'LeVeL', 'kayak', 'racecar', 'Doc, note: I dissent. A fast never prevents a fatness. I diet on cod', 'redder', 'madam', '1991', 'refer'];

for (let word of words_array)
if (check_palindrome_optimized(word) === true && check_palindrome(word) === true)
console.log('🍏 "'.concat(word, '" is a palindrome!'));
else
console.log('🍎 "'.concat(word, '" is not a palindrome!'));







python javascript beginner algorithm strings






share|improve this question
















share|improve this question













share|improve this question




share|improve this question








edited Oct 14 at 17:14







Emma

















asked Oct 14 at 15:33









EmmaEmma

8512 gold badges2 silver badges21 bronze badges




8512 gold badges2 silver badges21 bronze badges










  • 1




    $begingroup$
    I presume, by "string" you mean "a sequence of Latin letters"? Otherwise, your problems would be much harder to solve. See this comment: stackoverflow.com/a/16776621/989121
    $endgroup$
    – georg
    Oct 15 at 7:19






  • 1




    $begingroup$
    Typically, punctuation is ignored when testing for palindromes, so "Doc, note:..." should return true. Likewise, "Never odd or even" should return true.
    $endgroup$
    – Baldrickk
    Oct 15 at 10:47







  • 1




    $begingroup$
    You could also go one step further here and add a reverse method to the String prototype (in js) so you could do: "test".reverse(). This keeps the code nicely encapsulated and re-usable. With it being on the prototype, every instance gets the same function.
    $endgroup$
    – Magrangs
    Oct 15 at 15:16













  • 1




    $begingroup$
    I presume, by "string" you mean "a sequence of Latin letters"? Otherwise, your problems would be much harder to solve. See this comment: stackoverflow.com/a/16776621/989121
    $endgroup$
    – georg
    Oct 15 at 7:19






  • 1




    $begingroup$
    Typically, punctuation is ignored when testing for palindromes, so "Doc, note:..." should return true. Likewise, "Never odd or even" should return true.
    $endgroup$
    – Baldrickk
    Oct 15 at 10:47







  • 1




    $begingroup$
    You could also go one step further here and add a reverse method to the String prototype (in js) so you could do: "test".reverse(). This keeps the code nicely encapsulated and re-usable. With it being on the prototype, every instance gets the same function.
    $endgroup$
    – Magrangs
    Oct 15 at 15:16








1




1




$begingroup$
I presume, by "string" you mean "a sequence of Latin letters"? Otherwise, your problems would be much harder to solve. See this comment: stackoverflow.com/a/16776621/989121
$endgroup$
– georg
Oct 15 at 7:19




$begingroup$
I presume, by "string" you mean "a sequence of Latin letters"? Otherwise, your problems would be much harder to solve. See this comment: stackoverflow.com/a/16776621/989121
$endgroup$
– georg
Oct 15 at 7:19




1




1




$begingroup$
Typically, punctuation is ignored when testing for palindromes, so "Doc, note:..." should return true. Likewise, "Never odd or even" should return true.
$endgroup$
– Baldrickk
Oct 15 at 10:47





$begingroup$
Typically, punctuation is ignored when testing for palindromes, so "Doc, note:..." should return true. Likewise, "Never odd or even" should return true.
$endgroup$
– Baldrickk
Oct 15 at 10:47





1




1




$begingroup$
You could also go one step further here and add a reverse method to the String prototype (in js) so you could do: "test".reverse(). This keeps the code nicely encapsulated and re-usable. With it being on the prototype, every instance gets the same function.
$endgroup$
– Magrangs
Oct 15 at 15:16





$begingroup$
You could also go one step further here and add a reverse method to the String prototype (in js) so you could do: "test".reverse(). This keeps the code nicely encapsulated and re-usable. With it being on the prototype, every instance gets the same function.
$endgroup$
– Magrangs
Oct 15 at 15:16











2 Answers
2






active

oldest

votes


















9
















$begingroup$

Boolean expression returns



This applies to both your Javascript and Python implementations:



if (reversed_string_1 === original_string && reversed_string_2 === original_string && reversed_string_3 === original_string && reversed_string_4 === original_string) 
return true;
// If the original string is not a palindrome
else
return false;



if reverse_string_slice(original_string) == original_string and reverse_string(original_string) == original_string:
return True
return False


Your expression is already a boolean; you don't need an if. In other words,



return reverse_string_slice(original_string) == original_string and reverse_string(original_string) == original_string


Boolean comparison



if check_palindrome(word) == True:


should simply be



if check_palindrome(word):


Also, the convention is that boolean-valued functions are named like is_palindrome.



words_array



First of all: This isn't an array, because Python doesn't have those. It's a list. Second, just call it words - usually it's not useful to include the type of a variable in its name. Finally: you shouldn't even be using a list, because you won't be mutating (modifying) it; use a tuple (in parens, not brackets).






share|improve this answer










$endgroup$














  • $begingroup$
    Technically, Python does have arrays. However, they are rarely used. The term "array" is also used in the context of numpy.
    $endgroup$
    – GZ0
    Oct 15 at 1:56










  • $begingroup$
    That's fair. It's more accurate to say that Python has arrays, but they aren't first-class.
    $endgroup$
    – Reinderien
    Oct 15 at 2:17


















4
















$begingroup$

In Python, your check_palindrome function can be made more efficient and pythonic:



def check_palindrome(original_string):
"""Returns true if an input string is a palindrome"""
original_string = original_string.lower()
return all(r == o for r, o in zip(reversed(original_string), original_string[:len(original_string)//2]))


This only iterates over half of the characters as opposed to your version. You could also use islice from itertools, for example:



def check_palindrome(original_string):
"""Returns true if an input string is a palindrome"""
original_string = original_string.lower()
original = islice(original_string, len(original_string) // 2)
return all(r == o for r, o in zip(reversed(original_string), original))


Also note that docstrings should be double quoted.






share|improve this answer












$endgroup$










  • 1




    $begingroup$
    original_string[::len(original_string)//2] should be original_string[:len(original_string)//2].
    $endgroup$
    – GZ0
    Oct 15 at 1:59










  • $begingroup$
    @GZ0 Thanks for the catch.
    $endgroup$
    – Daniel Mesejo
    Oct 15 at 2:02












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/4.0/"u003ecc by-sa 4.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
);



);














draft saved

draft discarded
















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f230709%2fpalindrome-and-reverse-a-string-problems-javascript-python%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









9
















$begingroup$

Boolean expression returns



This applies to both your Javascript and Python implementations:



if (reversed_string_1 === original_string && reversed_string_2 === original_string && reversed_string_3 === original_string && reversed_string_4 === original_string) 
return true;
// If the original string is not a palindrome
else
return false;



if reverse_string_slice(original_string) == original_string and reverse_string(original_string) == original_string:
return True
return False


Your expression is already a boolean; you don't need an if. In other words,



return reverse_string_slice(original_string) == original_string and reverse_string(original_string) == original_string


Boolean comparison



if check_palindrome(word) == True:


should simply be



if check_palindrome(word):


Also, the convention is that boolean-valued functions are named like is_palindrome.



words_array



First of all: This isn't an array, because Python doesn't have those. It's a list. Second, just call it words - usually it's not useful to include the type of a variable in its name. Finally: you shouldn't even be using a list, because you won't be mutating (modifying) it; use a tuple (in parens, not brackets).






share|improve this answer










$endgroup$














  • $begingroup$
    Technically, Python does have arrays. However, they are rarely used. The term "array" is also used in the context of numpy.
    $endgroup$
    – GZ0
    Oct 15 at 1:56










  • $begingroup$
    That's fair. It's more accurate to say that Python has arrays, but they aren't first-class.
    $endgroup$
    – Reinderien
    Oct 15 at 2:17















9
















$begingroup$

Boolean expression returns



This applies to both your Javascript and Python implementations:



if (reversed_string_1 === original_string && reversed_string_2 === original_string && reversed_string_3 === original_string && reversed_string_4 === original_string) 
return true;
// If the original string is not a palindrome
else
return false;



if reverse_string_slice(original_string) == original_string and reverse_string(original_string) == original_string:
return True
return False


Your expression is already a boolean; you don't need an if. In other words,



return reverse_string_slice(original_string) == original_string and reverse_string(original_string) == original_string


Boolean comparison



if check_palindrome(word) == True:


should simply be



if check_palindrome(word):


Also, the convention is that boolean-valued functions are named like is_palindrome.



words_array



First of all: This isn't an array, because Python doesn't have those. It's a list. Second, just call it words - usually it's not useful to include the type of a variable in its name. Finally: you shouldn't even be using a list, because you won't be mutating (modifying) it; use a tuple (in parens, not brackets).






share|improve this answer










$endgroup$














  • $begingroup$
    Technically, Python does have arrays. However, they are rarely used. The term "array" is also used in the context of numpy.
    $endgroup$
    – GZ0
    Oct 15 at 1:56










  • $begingroup$
    That's fair. It's more accurate to say that Python has arrays, but they aren't first-class.
    $endgroup$
    – Reinderien
    Oct 15 at 2:17













9














9










9







$begingroup$

Boolean expression returns



This applies to both your Javascript and Python implementations:



if (reversed_string_1 === original_string && reversed_string_2 === original_string && reversed_string_3 === original_string && reversed_string_4 === original_string) 
return true;
// If the original string is not a palindrome
else
return false;



if reverse_string_slice(original_string) == original_string and reverse_string(original_string) == original_string:
return True
return False


Your expression is already a boolean; you don't need an if. In other words,



return reverse_string_slice(original_string) == original_string and reverse_string(original_string) == original_string


Boolean comparison



if check_palindrome(word) == True:


should simply be



if check_palindrome(word):


Also, the convention is that boolean-valued functions are named like is_palindrome.



words_array



First of all: This isn't an array, because Python doesn't have those. It's a list. Second, just call it words - usually it's not useful to include the type of a variable in its name. Finally: you shouldn't even be using a list, because you won't be mutating (modifying) it; use a tuple (in parens, not brackets).






share|improve this answer










$endgroup$



Boolean expression returns



This applies to both your Javascript and Python implementations:



if (reversed_string_1 === original_string && reversed_string_2 === original_string && reversed_string_3 === original_string && reversed_string_4 === original_string) 
return true;
// If the original string is not a palindrome
else
return false;



if reverse_string_slice(original_string) == original_string and reverse_string(original_string) == original_string:
return True
return False


Your expression is already a boolean; you don't need an if. In other words,



return reverse_string_slice(original_string) == original_string and reverse_string(original_string) == original_string


Boolean comparison



if check_palindrome(word) == True:


should simply be



if check_palindrome(word):


Also, the convention is that boolean-valued functions are named like is_palindrome.



words_array



First of all: This isn't an array, because Python doesn't have those. It's a list. Second, just call it words - usually it's not useful to include the type of a variable in its name. Finally: you shouldn't even be using a list, because you won't be mutating (modifying) it; use a tuple (in parens, not brackets).







share|improve this answer













share|improve this answer




share|improve this answer










answered Oct 14 at 17:20









ReinderienReinderien

12.6k22 silver badges50 bronze badges




12.6k22 silver badges50 bronze badges














  • $begingroup$
    Technically, Python does have arrays. However, they are rarely used. The term "array" is also used in the context of numpy.
    $endgroup$
    – GZ0
    Oct 15 at 1:56










  • $begingroup$
    That's fair. It's more accurate to say that Python has arrays, but they aren't first-class.
    $endgroup$
    – Reinderien
    Oct 15 at 2:17
















  • $begingroup$
    Technically, Python does have arrays. However, they are rarely used. The term "array" is also used in the context of numpy.
    $endgroup$
    – GZ0
    Oct 15 at 1:56










  • $begingroup$
    That's fair. It's more accurate to say that Python has arrays, but they aren't first-class.
    $endgroup$
    – Reinderien
    Oct 15 at 2:17















$begingroup$
Technically, Python does have arrays. However, they are rarely used. The term "array" is also used in the context of numpy.
$endgroup$
– GZ0
Oct 15 at 1:56




$begingroup$
Technically, Python does have arrays. However, they are rarely used. The term "array" is also used in the context of numpy.
$endgroup$
– GZ0
Oct 15 at 1:56












$begingroup$
That's fair. It's more accurate to say that Python has arrays, but they aren't first-class.
$endgroup$
– Reinderien
Oct 15 at 2:17




$begingroup$
That's fair. It's more accurate to say that Python has arrays, but they aren't first-class.
$endgroup$
– Reinderien
Oct 15 at 2:17













4
















$begingroup$

In Python, your check_palindrome function can be made more efficient and pythonic:



def check_palindrome(original_string):
"""Returns true if an input string is a palindrome"""
original_string = original_string.lower()
return all(r == o for r, o in zip(reversed(original_string), original_string[:len(original_string)//2]))


This only iterates over half of the characters as opposed to your version. You could also use islice from itertools, for example:



def check_palindrome(original_string):
"""Returns true if an input string is a palindrome"""
original_string = original_string.lower()
original = islice(original_string, len(original_string) // 2)
return all(r == o for r, o in zip(reversed(original_string), original))


Also note that docstrings should be double quoted.






share|improve this answer












$endgroup$










  • 1




    $begingroup$
    original_string[::len(original_string)//2] should be original_string[:len(original_string)//2].
    $endgroup$
    – GZ0
    Oct 15 at 1:59










  • $begingroup$
    @GZ0 Thanks for the catch.
    $endgroup$
    – Daniel Mesejo
    Oct 15 at 2:02















4
















$begingroup$

In Python, your check_palindrome function can be made more efficient and pythonic:



def check_palindrome(original_string):
"""Returns true if an input string is a palindrome"""
original_string = original_string.lower()
return all(r == o for r, o in zip(reversed(original_string), original_string[:len(original_string)//2]))


This only iterates over half of the characters as opposed to your version. You could also use islice from itertools, for example:



def check_palindrome(original_string):
"""Returns true if an input string is a palindrome"""
original_string = original_string.lower()
original = islice(original_string, len(original_string) // 2)
return all(r == o for r, o in zip(reversed(original_string), original))


Also note that docstrings should be double quoted.






share|improve this answer












$endgroup$










  • 1




    $begingroup$
    original_string[::len(original_string)//2] should be original_string[:len(original_string)//2].
    $endgroup$
    – GZ0
    Oct 15 at 1:59










  • $begingroup$
    @GZ0 Thanks for the catch.
    $endgroup$
    – Daniel Mesejo
    Oct 15 at 2:02













4














4










4







$begingroup$

In Python, your check_palindrome function can be made more efficient and pythonic:



def check_palindrome(original_string):
"""Returns true if an input string is a palindrome"""
original_string = original_string.lower()
return all(r == o for r, o in zip(reversed(original_string), original_string[:len(original_string)//2]))


This only iterates over half of the characters as opposed to your version. You could also use islice from itertools, for example:



def check_palindrome(original_string):
"""Returns true if an input string is a palindrome"""
original_string = original_string.lower()
original = islice(original_string, len(original_string) // 2)
return all(r == o for r, o in zip(reversed(original_string), original))


Also note that docstrings should be double quoted.






share|improve this answer












$endgroup$



In Python, your check_palindrome function can be made more efficient and pythonic:



def check_palindrome(original_string):
"""Returns true if an input string is a palindrome"""
original_string = original_string.lower()
return all(r == o for r, o in zip(reversed(original_string), original_string[:len(original_string)//2]))


This only iterates over half of the characters as opposed to your version. You could also use islice from itertools, for example:



def check_palindrome(original_string):
"""Returns true if an input string is a palindrome"""
original_string = original_string.lower()
original = islice(original_string, len(original_string) // 2)
return all(r == o for r, o in zip(reversed(original_string), original))


Also note that docstrings should be double quoted.







share|improve this answer















share|improve this answer




share|improve this answer








edited Oct 15 at 2:01

























answered Oct 14 at 23:05









Daniel MesejoDaniel Mesejo

1414 bronze badges




1414 bronze badges










  • 1




    $begingroup$
    original_string[::len(original_string)//2] should be original_string[:len(original_string)//2].
    $endgroup$
    – GZ0
    Oct 15 at 1:59










  • $begingroup$
    @GZ0 Thanks for the catch.
    $endgroup$
    – Daniel Mesejo
    Oct 15 at 2:02












  • 1




    $begingroup$
    original_string[::len(original_string)//2] should be original_string[:len(original_string)//2].
    $endgroup$
    – GZ0
    Oct 15 at 1:59










  • $begingroup$
    @GZ0 Thanks for the catch.
    $endgroup$
    – Daniel Mesejo
    Oct 15 at 2:02







1




1




$begingroup$
original_string[::len(original_string)//2] should be original_string[:len(original_string)//2].
$endgroup$
– GZ0
Oct 15 at 1:59




$begingroup$
original_string[::len(original_string)//2] should be original_string[:len(original_string)//2].
$endgroup$
– GZ0
Oct 15 at 1:59












$begingroup$
@GZ0 Thanks for the catch.
$endgroup$
– Daniel Mesejo
Oct 15 at 2:02




$begingroup$
@GZ0 Thanks for the catch.
$endgroup$
– Daniel Mesejo
Oct 15 at 2:02


















draft saved

draft discarded















































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.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f230709%2fpalindrome-and-reverse-a-string-problems-javascript-python%23new-answer', 'question_page');

);

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









Popular posts from this blog

Canceling a color specificationRandomly assigning color to Graphics3D objects?Default color for Filling in Mathematica 9Coloring specific elements of sets with a prime modified order in an array plotHow to pick a color differing significantly from the colors already in a given color list?Detection of the text colorColor numbers based on their valueCan color schemes for use with ColorData include opacity specification?My dynamic color schemes

Invision Community Contents History See also References External links Navigation menuProprietaryinvisioncommunity.comIPS Community ForumsIPS Community Forumsthis blog entry"License Changes, IP.Board 3.4, and the Future""Interview -- Matt Mecham of Ibforums""CEO Invision Power Board, Matt Mecham Is a Liar, Thief!"IPB License Explanation 1.3, 1.3.1, 2.0, and 2.1ArchivedSecurity Fixes, Updates And Enhancements For IPB 1.3.1Archived"New Demo Accounts - Invision Power Services"the original"New Default Skin"the original"Invision Power Board 3.0.0 and Applications Released"the original"Archived copy"the original"Perpetual licenses being done away with""Release Notes - Invision Power Services""Introducing: IPS Community Suite 4!"Invision Community Release Notes

199年 目錄 大件事 到箇年出世嗰人 到箇年死嗰人 節慶、風俗習慣 導覽選單