Write a program to build a histogram using dictionary to count the number oftimes each character appears in words starting with a certain character.The program• asks the user to input a filename and a character• builds a histogram using dictionary to count the number of times eachcharacter appears in those words in the file starting with the input character• prints the characters in alphabetical order and in lower case and itsfrequency, separated by a single space, one row per character ended with anewline• if no word starts with the input character, print “Not found
Question
Write a program to build a histogram using dictionary to count the number oftimes each character appears in words starting with a certain character.The program• asks the user to input a filename and a character• builds a histogram using dictionary to count the number of times eachcharacter appears in those words in the file starting with the input character• prints the characters in alphabetical order and in lower case and itsfrequency, separated by a single space, one row per character ended with anewline• if no word starts with the input character, print “Not found
Solution
Sure, here is a Python program that fulfills your requirements:
def build_histogram(filename, start_char):
histogram = {}
with open(filename, 'r') as file:
for line in file:
words = line.split()
for word in words:
if word[0].lower() == start_char.lower():
for char in word:
char = char.lower()
if char in histogram:
histogram[char] += 1
else:
histogram[char] = 1
if not histogram:
print("Not found")
else:
for key in sorted(histogram.keys()):
print(f"{key} {histogram[key]}")
filename = input("Enter a filename: ")
start_char = input("Enter a character: ")
build_histogram(filename, start_char)
This program works as follows:
- It asks the user to input a filename and a character.
- It reads the file line by line, and for each line, it splits the line into words.
- For each word, it checks if the first character of the word (in lower case) is the same as the input character (also in lower case).
- If the first character of the word matches the input character, it goes through each character in the word. For each character, it checks if the character is already in the histogram dictionary. If it is, it increments the count of that character in the dictionary. If it's not, it adds the character to the dictionary with a count of 1.
- If the histogram dictionary is empty after going through the entire file (i.e., no word starts with the input character), it prints "Not found".
- Otherwise, it prints the characters and their frequencies in alphabetical order. Each character and its frequency are printed on a new line, separated by a single space.
Similar Questions
Find the count of letters and words in a file.
Write a python program to count the number of characters of alphabets 'O' and 'i' from the given file
Count the sentences, words and characters in a fileWrite a Python program that reads a file and prints the number of lines, words, and characters in the file.Input Format:The input will be a single string, which is the name of the file to be read.Output Format:The output will be three separate lines, each containing a count:The first line will contain the number of lines in the file, prefixed with "Sentences: ".The second line will contain the number of words in the file, prefixed with "Words: ".The third line will contain the number of characters in the file, prefixed with "Chars: ".Sample Test CasesTest Case 1:Expected Output:file·name:input1.txtSentences:·8Words:·34Chars:·228Test Case 2:Expected Output:file·name:input2.txtSentences:·7Words:·51Chars:·306
Print the number of lines, words, and characters in a file.Write a program to print the number of lines, words, and characters in a file.Note: Take the input file name from the user.Sample Test CasesTest Case 1:Expected Output:Enter·file·name:·InputData1.txtLine·count·=·7Word·count·=·51Char·count·=·306
Write a Python program to find the occurrences of a character in a given word
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.