Question

In: Computer Science

Make sure to include comments that explain all your steps (starts with #) Make sure to...

Make sure to include comments that explain all your steps (starts with #)

Make sure to include comments that explain all your steps (starts with #)

Write a program that prompts the user for a string (a sentence, a word list, single words etc.), counts the number of times each word appears and outputs the total word count and unique word count in a sorted order from high to low. The program should: Display a message stating its goal Prompt the user to enter a string Count the number of times each word appears in the string, regardless if in lowercase or uppercase (hint: use dictionaries and the lower() function) Display the total number of words in the string Display the unique word count in the string in order from high to low Bonus: if a few words have the same count, sort the display in an alphabetic order For example, in the string: “Hello hello my dear dear friend!” the output should be: Total words: ========= 6 Word count: ========= dear - 2 hello - 2 friend - 1 my - 1

Solutions

Expert Solution

text=input('Enter text: ')
total=0
d={}#dictionary to store frequency
for i in text.split():#split by space, and iterate
word=i.lower().strip(";:,.?!")#convert to lowercase, remove common punctuations from start and end
if word in d:
d[word]+=1#if word is in dictionary, add 1 to count
else:
d[word]=1#if not, add to dictionary with count 1
total+=1#increment total
print('Total words:',total)
print("="*10)
print('Word count')
print("="*10)
for i in sorted(d,key=lambda x: (-d[x],x)):#sort dictionary keys, first by the dicitonary value, then the key itself
#the key above specified wat is to be compared
print(i,"-",d[i])


Related Solutions

Do a hypothesis for the following, make sure to include and label all five steps:    ...
Do a hypothesis for the following, make sure to include and label all five steps:     Test the claim that the proportion of wins is the same whether the wear a shirt     and tie or jeans and a t-shirt. Use a .05 significance level.             Win Loss Suit and Tie 23 38 Jeans and T-shirt 28 24
In detail, compare and contrast mitosis and meiosis. Make sure to include the steps of each....
In detail, compare and contrast mitosis and meiosis. Make sure to include the steps of each. 500 word minimum
Discuss the steps in generating an action potential in a neuron. (Make sure to include which...
Discuss the steps in generating an action potential in a neuron. (Make sure to include which ion channels are activated and how, changes in membrane potential as a result of the movement of those ions, the different phases seen in an action potential, and the movement of the action potential in relation to the anatomy of a neuron.).
For all hypothesis tests, be sure to include information for all 7 steps. For any two-sample...
For all hypothesis tests, be sure to include information for all 7 steps. For any two-sample or paired t-tests that you perform, in Step 6, show the decision rule based on BOTH the critical value and the p-value. Use the online app for the t-distribution to compute your p-values. The Bayley Scales of Infant Development yield scores on two indices – the Psychomotor Development Index (PDI) and the Mental Development Index (MDI) – that can be used to assess a...
Please show all steps and write neatly. Make sure the proving is with letters and not...
Please show all steps and write neatly. Make sure the proving is with letters and not with numbers. Please!!!! Thank you Let A, B, C be 3 x 3 matrices. Show (AB)C= A(BC)
Make sure to show all steps to receive full credit, that is: a) Identify the claim:...
Make sure to show all steps to receive full credit, that is: a) Identify the claim: state the null and alternative hypotheses. b) Determine the test: left-tailed, right-tailed, or two-tailed. c) Graph your bell-shaped curve and label your levels of significance or critical value. d) Find your standardized test statistic ? and label it on your graph. e) Decide whether to reject or fail to reject the null hypothesis. f) Interpret your result. 1) A medical researcher suspects that the...
Please show steps in the calculation. Please make sure you include checking conditions for using the...
Please show steps in the calculation. Please make sure you include checking conditions for using the CLT. Only Answer Question 3 and 4 The Human Resources (HR) Department of a certain college has asked all employees who were employed in 2018 to fill out a survey in December 2018. Three items on the survey were: “Your dental expense in 2018”, “Are you in a family with at least three other family members?”, and “Your medical expenses in 2018”. The manager...
Please show steps in the calculation. Please make sure you include checking conditions for using the...
Please show steps in the calculation. Please make sure you include checking conditions for using the CLT. The Human Resources (HR) Department of a certain college has asked all employees who were employed in 2018 to fill out a survey in December 2018. Three items on the survey were: “Your dental expense in 2018”, “Are you in a family with at least three other family members?”, and “Your medical expenses in 2018”. The manager of HR has randomly selected a...
Draw a diagram that illustrates lipid digestion and absorption, make sure you include where these steps...
Draw a diagram that illustrates lipid digestion and absorption, make sure you include where these steps are occurring and the important role of bile! Make sure it is clearly labeled and include a brief description for any sequences and processes.
Code the following in C++ and make sure to include all three files required at the...
Code the following in C++ and make sure to include all three files required at the end. Hotdogs – The World’s Greatest Cost Effective Wholesome Nutritious Food Due to world food supply scarcity and the burgeoning populations of the world’s countries, your hot stand business is globalizing to satisfy the world’s desire for a cost effective wholesome nutritious food source. Market studies have shown that tens of billions of people are craving for the eponymous hotdog which will result in...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT