Editorial for Odd Even Characters
Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.
Submitting an official solution before solving the problem yourself is a bannable offence.
Author:
# Retrieve the first two lines of input,
# which correspond to the length of string and the string itself.
number_of_characters = int(input())
english_string = input()
# Initialize the count of odd and even characters to 0.
odd_count = 0
even_count = 0
# Iterate through each character in the string.
for character in english_string:
# If the character is even, increment the even count.
# Note that we use `ord()` to get the ASCII value of the characters;
# we subtract the ASCII value of 'a' to get the 0-based index of the character,
# meaning that 'a' would yield 0, 'b' would yield 1, and so on.
if (ord(character) - ord('a')) % 2 == 0:
odd_count += 1
# Otherwise, increment the odd count.
else:
even_count += 1
# Print out the counts of odd and even characters!
print(odd_count, even_count)
Comments