In: Computer Science
Let us define the overlap between two words as the count of unique letters they have in common. Thus, the overlap between JANE and MICK is 0. Here are some more examples: - The overlap between JANE and MIKE is 1 (E is common) - The overlap between JANE and MEEK is 1 (E is common; we do not double count a letter) - The overlap between JANE and JEDI is 2 (J and E are common) - The overlap between JANE and PANEL is 3 (A, N, and E are common) - The overlap between JANE and ANKLE is 3 ( A, N, and E are common) - The overlap between JANE and JAUNDICE is 4 (J, A, N, and E are common) Let us define the total overlap between a word W and a collection of words L as the sum of the overlap between W and each of the words contained in L. Thus the total overlap between the word JANE and the collection of words: [MIKE, MEEK, JEDI, PANEL, ANKLE, JAUNDICE] = 1+1+2+3+3+4 = 14 For Project 5, you will be provided with a collection of 5,163 names (in all caps) inside a text file. Each line in the text file contains a single name. Write a program that finds the total overlap between your first name (in all caps) and this collection. If you are on Windows, the text file you should use is “names_win.txt”. If you are using a Mac to write your programs, the text file to use is “names_mac.txt”. Python solution
Code screenshot :
Sample input :
Sample output:
Copyable code:
input_filename = input("Enter the name of the input file: ")
f1=open(input_filename)#opening the input file
word=f1.readline().strip()#reading the first line which is our
word
c=0
for line in f1: #reading contents of file
line=line.strip()#to remove leading spaces
word1=word
#code to check overlap characters in the strings
for i in word1:
for k in line:
if (i==k):
c=c+1 #counting the total number of overlaps
k='*' #to avoid duplication check
i='*' #to avoid duplication check
print("The Total number of overlaps for string "+word+" with other
words in file are: "+str(c))#printing the output