ACSL String Stats Problem

Published on January 28, 2024

This article discusses and contains the Python code solution for the American Computer Science League (ACSL) Junior Division 2018-2019 Programming Problem. More about the ACSL here.

ACSL String Stats problem ACSL String Stats problem

String Stats problem

The String Stats problem was the programming problem in the ACSL Junior Division Contest 2.

You can read the String Stats problem. Make sure you understand the problem first. Read it many times, if necessary. Then, write the algorithm on paper before coding anything. For your algorithm, you can write either pseudocode or a diagrammatical flowchart.

I first wrote the solution in 2021 and published it on GitHub, and am publishing it now as a blog post with explanations.

Programming Solution Logic

The input is a line of data. The output is five lines.

The first line is the number of different letters. For this, use set() to get a list of unique characters. In the resulting set, eliminate all characters which do not lie between a and z. For this, use the condition if not 'a' <= x <= 'z'. In the end, you are left with only the letters of the alphabet. Print the number of letters.

The second line is the number of vowels, that is a, e, i, o, u. Iterate through the input string and count the vowels. For this, use x in 'aeiou' where x takes the value of every character in the input string.

The third line is the number of uppercase letters, that is the number of characters between A and Z. For this, use the condition if 'A' <= x <= 'Z'.

The fourth line is the number of times the most frequent letter appears, and this is case insensitive. For this, a dictionary is best used. The key is the letter and the value is the count of occurrences. You can also use the set used earlier to create the dictionary, and initialize the count to 0. You can either use the .count() method for each item in the set, or iterate through the input string and count the occurrences. After the count, find the highest occurrences using max() over the values with dict.values().

The fifth line is the longest word. If there is a tie, print the one alphabetically first and case-insensitive. For this, remove the non-letters with blanks from the input string, and split the updated input string into a list of words. Keep track of the largest word. Print it.

Test cases

First Test Input

The quick brown fox, named Roxanne, jumped over Bruno, a lazy dog.

OUTPUT:

25
19
3
6
Roxanne

Second Test Input

The 2019 All-Star Competition is at Wayne Hills HS in Wayne, New Jersey.

OUTPUT:

16
19
11
7
Competition

My Python solution

This is my Python solution:

#
# ACSL Junior Division - String Stats - 2018-2019
# Solution by Arul John
# Problem at http://www.datafiles.acsl.org/samples/contest2/c2-jr-prog.pdf
# Created: 2021-01-20
# Updated: <NEVER>
#

'''
Function to do the string statistics
'''
def string_stats(sentence):
    sentence_lower = sentence.lower() # lower case sentence; we will use it many times

    # Find the number of different letters
    number_of_different_letters = 0
    new_sentence = ''.join(set(sentence_lower))
    # new_sentence has lower case letters
    for x in new_sentence:
        if not 'a' <= x <= 'z': # if current letter x is NOT a letter between 'a' and 'z', remove it
            new_sentence = new_sentence.replace(x, '')
    number_of_different_letters = len(new_sentence)
    print(number_of_different_letters)

    # Find the number of vowels
    number_of_vowels = 0
    vowels_string = 'aeiou'
    for x in sentence_lower:
        if x in vowels_string: # if current letter x is a vowel
            number_of_vowels += 1
    print(number_of_vowels)

    # Find the number of uppercase letters
    number_of_uppercase_letters = 0
    for x in sentence:
        if 'A' <= x <= 'Z': # if current letter x is a letter between 'A' and 'Z'
            number_of_uppercase_letters += 1
    print(number_of_uppercase_letters)

    # Most frequent letter count
    letter_frequency = { }
    for x in sentence_lower:
        if 'a' <= x <= 'z': # if current letter x is a letter between 'a' and 'z'
            if x in letter_frequency:
                letter_frequency[x] += 1 # increase frequency count by 1
            else:
                letter_frequency[x] = 1  # first time, so initialize to 0
    # letter_frequency is a dictionary with key as letter and value as number of occurrences of that letter
    most_frequent_letter_count = max(letter_frequency.values()) # find highest count of occurrences
    print(most_frequent_letter_count)

    # Longest word
    longest_word = ''
    sentence_with_only_letters = sentence
    for letter in sentence_with_only_letters:
        # If x is not A-Z, a-z or space, then remove it
        # meaning, remove it if it's a - or . or comma
        if not ('a' <= letter <= 'z' or 'A' <= letter <= 'Z' or letter == ' '):
            sentence_with_only_letters = sentence_with_only_letters.replace(letter, '')
    # sentence_with_only_letters contains only words and spaces
    # We split it into a list called "words"
    words = sentence_with_only_letters.split(' ')
    # Loop through all words to find the longest word.
    for word in words:
        if len(word) > len(longest_word): # compare the current word with longest word
            longest_word = word           # If the word is longer than the longest word, make longest word take the value of 'word'
    print(longest_word)


# Main
if __name__ == "__main__":
    string_stats('The quick brown fox, named Roxanne, jumped over Bruno, a lazy dog.')
    string_stats('The 2019 All-Star Competition is at Wayne Hills HS in Wayne, New Jersey.')

Running the Python code

Run the Python code from your Terminal. You do not need to use any input file because I added the input as a string in the main function.

Conclusion

If there is anything you would like me to add to this article, feel free to comment below or contact me. Thanks.

Related Posts

If you have any questions, please contact me at arulbOsutkNiqlzziyties@gNqmaizl.bkcom. You can also post questions in our Facebook group. Thank you.

Disclaimer: Our website is supported by our users. We sometimes earn affiliate links when you click through the affiliate links on our website.

Published on January 28, 2024