Question

In: Computer Science

4.31 Implement function duplicate() that takes as input the name (a string) of a file in...

4.31 Implement function duplicate() that takes as input the name (a string) of a file in the current directory and returns True if the file contains duplicate words and False otherwise. duplicate('Duplicates.txt') True duplicate('noDuplicates.txt') False

Please solve using Python Language and without using str.maketrans please. Just simple programming, Thank youuuuu!!!!!

Solutions

Expert Solution

# defining a function duplicate that will check if the file contains duplicates words or not

def duplicate(fileName):
    # opening the text file in read mode
    f = open(fileName, 'r')
    # create an empty dictionary
    d={}

    # loop through each line of the file
    for line in f:
        # take each word of the line
        for word in line.split(" "):
            # to find the frequency of each word in the dictionary
            d[word] = d.get(word,0)+1


    #loop through each value of dictionary d and if the value of any key is greater then 1 then return True
    for value in d.values():
        if value>1:
            return True
        
    #return False
    return False

# calling duplicate() function from 'main'
if __name__ == '__main__':
    
    if duplicate("Duplicates.txt"):
        print("Duplicates")
    else:
        print("Not Duplicates")


    if duplicate("noDuplicates.txt"):
        print("Duplicates")
    else:
        print("Not Duplicates")

python file

John David Rey Ronald Rey David
Have a great day

Duplicates.txt file

David John Rey Ronald Smith
Keep learning and growing

noDuplicates.txt

Sample Input and Output:

Duplicates
Not Duplicates

Code explainations:

Here I have implemented the duplicate() function that will accept a filename as an input and then it will open the file in read mode. Then it will create an empty dictionary to hold the key:value pair.

Now it will fetch each line from the input file and then from each line it will fetch each word.

Now it will find the frequency of each word in the dictionary as

d[word] = d.get(word,0)+1

Here get() method will return the value of given key if present and if not present it will return 0.

Simply we have added 1 to it to increment the count of each word in dictionary

Finally it will check for all the values of dictionary using d.values() method to check if any value in dictionary is greater then 1 that means that word is duplicate in the input file. So we returns True.

If there is not duplicates then simply we return False.

Code screenshot:


Related Solutions

5.16 Implement function indexes() that takes as input a word (as a string) and a onecharacter...
5.16 Implement function indexes() that takes as input a word (as a string) and a onecharacter letter (as a string) and returns a list of indexes at which the letter occurs in the word. >>> indexes('mississippi', 's') [2, 3, 5, 6] >>> indexes('mississippi', 'i') [1, 4, 7, 10] >>> indexes('mississippi', 'a') []
5.30 Develop the function many() that takes as input the name of a file in the...
5.30 Develop the function many() that takes as input the name of a file in the current directory (as a string) and outputs the number of words of length 1, 2, 3, and 4. Test your function on file sample.txt. >>> many('sample.txt') Words of length 1 : 2 Words of length 2 : 5 Words of length 3 : 1 Words of length 4 : 10 Coding language is python
Python Implement function noVowel() that takes a string s as input and returns True if no...
Python Implement function noVowel() that takes a string s as input and returns True if no char- acter in s is a vowel, and False otherwise (i.e., some character in s is a vowel). >>> noVowel('crypt') True >>> noVowel('cwm') True >>> noVowel('car') False
Implement function noVowel() that takes a string s as input and returns True if no char-...
Implement function noVowel() that takes a string s as input and returns True if no char- acter in s is a vowel, and False otherwise (i.e., some character in s is a vowel). >>> noVowel('crypt') True >>> noVowel('cwm') True >>> noVowel('car') False Implement function allEven() that takes a list of integers and returns True if all integers in the list are even, and False otherwise. >>> allEven([8, 0, -2, 4, -6, 10]) True >>> allEven([8, 0, -1, 4, -6, 10])...
4. Implement the function read_info_file that consumes a file name (string) as its parameter and returns...
4. Implement the function read_info_file that consumes a file name (string) as its parameter and returns a list of strings - one element for each line in the file. These lines should have all the whitespace removed from both ends of the line. a. See the formatting of the individual_info data file. Consider how a file can be read into the program. In Python language
Write a function redact() takes as input a file name. See secret.txt for the initial test....
Write a function redact() takes as input a file name. See secret.txt for the initial test. The function should print the contents of the file on the screen with this modification: Every occurrence of string 'secret' in the file should be replaced with string 'xxxxxx'. Every occurrence of “agent’ should be replaced with ‘yyyyyy’. Every occurrence of carrier should be replaced with ‘zzzzzz’. This function should catch exceptions. FOR PYTHON
Write a function that takes a C string as an input parameter and reverses the string.
in c++ Write a function that takes a C string as an input parameter and reverses the string. The function should use two pointers, front and rear. The front pointer should initially reference the first character in the string, and the rear pointer should initially reference the last character in the string. Reverse the string by swapping the characters referenced by front and rear, then increment front to point to the next character and decrement rear to point to the...
Write function words() that takes one input argument—a file name—and returns the list of actual words...
Write function words() that takes one input argument—a file name—and returns the list of actual words (without punctuation symbols !,.:;?) in the file. >>> words('example.txt') ['The', '3', 'lines', 'in', 'this', 'file', 'end', 'with', 'the', 'new', 'line', 'character', 'There', 'is', 'a', 'blank', 'line', 'above', 'this', 'line']
Write a parameterized function that takes in a file name as a parameter, reads the file,...
Write a parameterized function that takes in a file name as a parameter, reads the file, calculates the factorial of each number, and displays a formatted output as follows: Factorial of 10 = 3628800 Factorial of 5 = 120
C++ ONLY! Implement the find function for the List class. It takes a string as an...
C++ ONLY! Implement the find function for the List class. It takes a string as an argument and returns an iterator to a matching node. If no matching node, it returns a null iterator. #include <iostream> #include <cstddef> #include <string> using Item = std::string; class List { private: class ListNode { public: Item item; ListNode * next; ListNode(Item i, ListNode *n=nullptr) { item = i; next = n; } };    ListNode * head; ListNode * tail;    public: class...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT