In: Computer Science
Write the simplest program possible in your language of choice containing your 'main' and any other functions you may need that will:
A. Read a sequence of words up to a maximum of 64 words from std input. Before reading the input, prompt the user to enter the words.
B. Check to see if there any duplicate words in the input read.
C. If there are no duplicates, print out to std output the message 'No Duplicates'.
D. If there are duplicates, print out on separate lines each word that is duplicated and the number of instances of that word. The results should be sorted in ascending
E. If you write helper functions to your main, ensure that all input and output happens only in main.
F. Do not use Vectors, Collections, or any other data structures provided by the language platform or any oft-used third party libraries like STL.
G. Take screenshot of your program execution with the following samples.
H. Your program will be tested with my own input.
Sample Inputs | Sample Outputs |
THE RAIN IN MAINE. | No Duplicates |
THE RAIN IN MAINE IN THE MONTH OF MAY. |
IN 2 THE 2 |
The rain in Maine in the month of May. | in 2 |
The rain in Maine in the month of May makes May the rainiest month for Maine in the year. |
Maine 2 May 2 in 3 month 2 the 3 |
Please do it in Python
Hi,
Hope you are dong fine. I have coded the above question in python as per your requirements. I have tried to keep it as simple as possible. I have not used or imported the collections module or any other external modules to write the code. The code has been clearly explained using comments that have been highlighted in bold. Also have a look athe snippets from the editor for correct indentation.
Program:
#Taking input from user. It is stored as a
string
string=input()
#removing the fullstop at the end because, is a duplicate
is present at the end, then the full stop along with it will be
difficult for comparison
string=string.strip(".")
#split the words seperated by space and store then in a
list called words
words=string.split(" ")
#if the number of words are more than 64
then
if len(words)>64:
#reduce the size of the words list upto the first 64
words
words=words[:64]
#count is an empty dictionary
count={}
#flag is used to check if there are any duplicates present.
It is set to one if atleast one duplicate is found
flag=0
#dupes stores the list of duplicates
dupes=[]
#taking x as key and assigning value 0 to every element of
count from words
for x in words:
count[x]=0
#traversing the list of words
for x in words:
#increment count in dictionary for each word
count[x]+=1
#traversing the dictionary to check the values of each key
i.e word
for x in count:
#if count is more than 1, it means that it is a
duplicate
if count[x]>1:
#append it to the list of dupes
dupes.append(x)
#set flag to 1
flag=1
#if flag is 0, then it means that there are no
duplicates
if flag==0:
print("No Duplicates")
else:
#sort the duplicates in ascending order
dupes.sort()
#traversing and printing the duplicates ine by
line
for x in dupes:
print("{} {}".format(x,count[x]))
Executable code snippet:
Sample outputs: Note that first line is the input.