Editorial for Odd Even Characters


Author:kahootist

Code 1
# 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)

Comments0


No comments yet

Be the first to comment.

New comment


Log in to join the discussion.