This article discusses and contains the Python code solution for the American Computer Science League (ACSL) Junior Division 2002-2003 Contest 3 Programming Problem Strings and Things.
ACSL Junior 2002-2003 Contest 3 Strings and Things problem
Strings and Things
The ACSL Strings and Things problem problem was the programming problem in ACSL Junior Division 2002-2003 Contest 3.
First, read the Strings and Things problem. Make sure you understand the problem description. Read it many times, if necessary. Then, write your algorithm on paper before coding anything. For your algorithm, you can write either pseudocode or a diagrammatical flowchart.
Input and Output
The questions are straightforward. There are 5 questions, and for each, the output is in a separate line.
Sample Input
The input is a string of characters, all in uppercase.
IN A RIGHT TRIANGLE THE SQUARE OF THE HYPOTENUSE EQUALS THE SUM OF THE
SQUARES OF THE LEGS
Sample Output
The output of the above input will be this:
18
HYPOTENUSE
17
E
O
Programming Solution Logic
Take a look at all the five questions.
- How many words are in the string?
- What is the longest word? If there is a tie, print all.
- How many different letters are in the string.
- What is the most frequent letter?
- What is the most frequently used word other than the word "THE"?
How to proceed
We will write code for each of these questions as a separate function, just so that debugging will be easy at a later stage. Before we start writing code for each individual question, we will chop and dice the sentence to suit our expected output.
We will split the sentence into a list of words. There is no extra punctuation, so we can split based on single whitespace character.
After that, we will create a dictionary with each unique word as the key and the number of occurrences as value.
For finding the number of different letters in the string question, we can craete one giant string without any spaces and convert it to a set, and count the letters in the set.
For finding the other four answers, we will use various list, dictionary and set functions.
Algorithm
- Read input and store the letter in variable s.
- Create a list
words
by splitting the strings
on single space. - Call function
print_num_words(words)
. - Call function
print_longest_word(words)
and print the result. - Call function
print_num_different_letters(s)
and print the result. - Call function
print_most_frequent_letter(s)
and print the result. - Call function
print_most_frequently_used_word_minus_the(words)
and print the result.
Algorithm for print_num_words(words)
- Print length of list
words
Algorithm for print_longest_word(words)
- There can be more than one longest word, so we will create an empty list named
longest_words
. - Create an int variable
longest_word_length
and assign it to 0. - Iterate through each word in the list, represent it with
current_word
. - If length of
current_word
is greater thanlongest_word_length
, then
longest_word_length = length ofcurrent_word
Create a new listlongest_words
with one item, the value ofcurrent_word
- Else If length of
current_word
is equal tolongest_word_length
, then
Append the value ofcurrent_word
to the existing listlongest_words
. - If there are still words in the list, continue iterating the words by going to Step (4).
- At this point, you will have list
longest_words
with one or more values. Join the value[s], with comma, for multiple values, and print.
Algorithm for print_num_different_letters(s)
- We will use the original list
s
. Remove all spaces from it. - Create a set out of the string created above.
- Print the length of the set.
Algorithm for print_most_frequent_letter(s)
- Using the string
s
, create a dictionary with the letter as key, and value 0 as the initial count of occurrences. - Iterate through every letter in the huge string.
- Update the dictionary value based on the current letter.
- Continue iterating by going to Step (2).
- Iterate through the dictionary key-value pairs and find the key corresponding to the maximum value.
Algorithm for print_most_frequently_used_word_minus_the(s)
- This algorithm will be similar to
print_most_frequent_letter()
. - Iterate through the word as key and count of occurrences as value.
- If the current word is not "the", then increase the number of occurrences for that word.
- Continue the iteration until all words are exhausted.
- Print the key with the highest occurrences.
My Python solution
This is my Python solution:
'''
ACSL Junior 2002-2003 Contest 3 STRINGS AND THINGS
Blog Post: https://aruljohn.com/blog/acsl-strings-and-things/
'''
def print_num_words(words):
print(len(words))
def print_longest_word(words):
longest_words = []
longest_word_length = 0
for current_word in words:
if len(current_word) > longest_word_length:
longest_word_length = len(current_word)
longest_words = [current_word]
elif len(current_word) == longest_word_length:
longest_words.append(current_word)
print(', '.join(longest_words))
def print_num_different_letters(s):
s = s.replace(' ', '')
s = set(s)
print(len(s))
def print_most_frequent_letter(s):
s = s.replace(' ', '')
letter_count = dict.fromkeys(s, 0)
for letter in s:
letter_count[letter] += 1
most_frequent_letter = ''
most_frequent_letter_count = 0
for letter, lcount in letter_count.items():
if lcount > most_frequent_letter_count:
most_frequent_letter = letter
most_frequent_letter_count = lcount
print(most_frequent_letter)
def print_most_frequently_used_word_minus_the(words):
word_count = dict.fromkeys(words, 0)
for word in words:
if word != 'THE':
word_count[word] += 1
most_frequent_word = ''
most_frequent_word_count = 0
for word, wcount in word_count.items():
if wcount > most_frequent_word_count:
most_frequent_word = word
most_frequent_word_count = wcount
print(most_frequent_word)
'''
=============
SAMPLE INPUT
=============
IN A RIGHT TRIANGLE THE SQUARE OF THE HYPOTENUSE EQUALS THE SUM OF THE SQUARES OF THE LEGS
=============
SAMPLE OUTPUT
=============
18
HYPOTENUSE
17
E
OF
'''
# Main function
if __name__ == '__main__':
s = 'IN A RIGHT TRIANGLE THE SQUARE OF THE HYPOTENUSE EQUALS THE SUM OF THE SQUARES OF THE LEGS'
words = s.split()
print_num_words(words)
print_longest_word(words)
print_num_different_letters(s)
print_most_frequent_letter(s)
print_most_frequently_used_word_minus_the(words)
Output
18 HYPOTENUSE 17 E OF
Useful Python code snippets
There are many code snippets that can be used in competitive programming contests. These are a few, relevant to this problem. We will use the Python shell to run these individual snippets.
Convert string to list based on single-space
We use this code to create a list from a string hello how are you
that has text separated by single space.
>>> s = 'hello how are you'
>>> s.split()
['hello', 'how', 'are', 'you']
Convert string to set
To convert string hello
to a set:
>>> s = 'hello'
>>> set(s)
{'h', 'e', 'o', 'l'}
Convert string to dictionary
To convert string hello
to a dictionary:
>>> s = 'hello'
>>> dict.fromkeys(s)
{'h': None, 'e': None, 'l': None, 'o': None}
Iterate through dictionary
To iterate through a dictionary and get keys and values with each iteration:
>>> s = 'hello'
>>> d = dict.fromkeys(s, 0)
>>> for k, v in d.items():
... print(k, v)
...
h 0
e 0
l 0
o 0
Use the Python shell
If you want to debug and see what string or numerical functions work, just run them in the Python shell.
Combine common code and DRY
Wherever possible, see if you can combine common code that repeats itself. DRY stands for don't repeat yourself. You can either use repeating code in a function with appropriate parameters. If you don't have time, or it's too late to make a clean fix, just leave it as it is, as long as it is working properly.
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.
When the program runs, this will be the output:
$ python strings_and_things.py
18
HYPOTENUSE
17
E
OF
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.