Question: This is a python code and then they ask to improve it . so the code will be first then the question. from os import
This is a python code and then they ask to improve it so the code will be first then the question. from os import strerror
# Initialize counters for each Latin letter.
# Note: we've used a comprehension to do that.
counters chrch: for ch in rangeorda ordz
filename inputEnter the name of the file to analyze:
try:
file openfilename, rt
for line in file:
for char in line:
# If it is a letter...
if char.isalpha:
# we'll treat it as lowercase and update the appropriate counter.
counterscharlower
file.close
# Let's output the counters.
for char in counters.keys:
c counterschar
if c :
printchar c
except IOError as e:
printIO error occurred: strerroreerrno The previous code needs to be improved. It's okay, but it has to be better.
Your task is to make some amendments, which generate the following results:
the output histogram will be sorted based on the characters' frequency the bigger counter should be presented first
the histogram should be sent to a file with the same name as the input one, but with the suffix hist' it should be concatenated to the original name
Assuming that the input file contains just one line filled with:
cBabAa
samplefile.txt
the expected output should look as follows:
a
b
c
Output
Tip: Use a lambda to change the sort order.
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
