Palindrome inventory codes
A warehouse flags memorable inventory codes. Read a list of lowercase codes and print how many are palindromes: codes that are identical when their characters are reversed. A one-character code counts.
Input
First line N. Next N lines each contain one lowercase code.
Output
One integer: the number of palindrome codes.
Example 1
Input
5 level crate r noon parts
Output
3
level, r and noon are palindromes.
Constraints
- 1 <= N <= 100
- Each code has 1 to 50 lowercase letters
Hints
Hint 1 of 3
Compare each code with a reversed copy.
Hint 2 of 3
Increase a counter only when the two strings match.
Hint 3 of 3
Python slicing [::-1] and JavaScript split/reverse/join can reverse a string.
Solution
Show a reference solution and explanation
count = int(input())
palindromes = 0
for _ in range(count):
code = input().strip()
if code == code[::-1]:
palindromes += 1
print(palindromes)
const count = Number(readline());
let palindromes = 0;
for (let i = 0; i < count; i++) {
const code = readline();
if (code === [...code].reverse().join('')) palindromes++;
}
console.log(palindromes);
Approach
Every code is tested independently against its reverse. The counter changes only for a match, so it directly represents the number of qualifying inventory codes after the input is exhausted.