0

I have done this question before for my homework assignment using the Counter. Now, I am studying this same question for finals. I would like to memorize dictionaries and not Counters for this final. I tried solving this problem using a dictionary instead.

So the problem was to create function name repeatCount. The purpose of the function is to read each line of the input file, identify the number of words on the line that occur more than once, and write that number to a line in the output file.

The input file text is this:

Woke up this morning with an ache in my head
I splashed on my clothes as I spilled out of bed
I opened the window to listen to the news
But all I heard was the Establishment Blues

My output file should look like this:

0
2
3
2

The correct output is:

0
1
2 
0

So here is my code now. What particular part of my code causes Python to produce me the wrong answer?:

def repeatCount(inFile, outFile):
    inF = open(inFile, 'r')
    outF = open(outFile, 'w')

    d = {}
    for line in inF.readlines():
        count = 0
        words = line.split()
        for word in words:
            if word not in d:
                d[word] = 1
            elif word in d:
                d[word] += 1
            if d[word] > 1:
                count += 1
        outF.write(str(count) + "\n")

print(repeatCount('inputFile.txt', 'outputFile.txt'))
sandeep nagendra
  • 422
  • 5
  • 15
Jorgan
  • 49
  • 3
  • 10

2 Answers2

0

You program will start giving you correct output if you re-set your dict for each line. ie. move d = {} to inside of your outer for loop. It will then work for your current input. But your inner for loop is still buggy as it is not ignoring the already counted duplicate words. Try again and show us your next iteration!

gipsy
  • 3,859
  • 1
  • 13
  • 21
  • That helped! The person's answer above me did the same thing, but he/she modified my code a little more. I just followed what you said and it simply gave me the right answer! – Jorgan Dec 19 '16 at 16:26
0

As suggested by @gipsy

move declaring dictionary inside for loop. Also use list inbuilt function count to get the actual count of the word.

d = {}

Modified version of your code.

def repeatCount(inFile, outFile):
    inF = open(inFile, 'r')
    outF = open(outFile, 'w')
    for line in inF.readlines():
        d = {}
        count = 0
        words = line.split()
        for word in words:
            if word not in d:
                wc = words.count(word)
                d[word] = 1
                if wc > 1:
                    count += 1
        outF.write(str(count) + "\n")

print(repeatCount('inputFile.txt', 'outputFile.txt'))

You can also improve your code. Refer Python: count frequency of words in a list

Community
  • 1
  • 1
sandeep nagendra
  • 422
  • 5
  • 15
  • All I did was put the d = {} as you did, but I did not do the `wc = words.count(word)` and the program still worked. Thank you! – Jorgan Dec 19 '16 at 16:25