In: Computer Science
Python
English algorithm explanation
Write a program that asks the user for the name of a file in the
current directory. Then, open the
file and process the content of the file.
1)If the file contains words that appear more than once, print “We
found duplicates.”
2)If the file does not contain duplicate words, print “There are no
duplicates.”
Code:
fi=open("duplicate.txt","r")
#Here we open the file in read mode
data=fi.read()
#Here we read the data from the file
words=data.split()
#Here we convert the data into the list
if(len(words)!=len(set(words))):
#Here we find the lenght of the list
#and compare it with the length of the set of same list
#Set removes all duplicate elements of the list
#If boath the lenghts are not equal then duplictes are
present
print("We found duplicates")
else:
# if boath the lengths are equal Duplicates are not present
print("There are no duplicates")
Text file data case 1:
Output:
Text file data case 2:
Output:
Indentation: