-1

I am trying to make a function that reads a document, and counts how many times a certain letter x appears in each row and inserts it into an array. After that, it will return whichever number appeared the most in the array. Here is what I have so far:

def LetterNumberCount(Letter):
    document = open ('magazine.in', 'r')
    letterOccurs = []
    for (Number of Lines): #Here is my problem
        line = document.readLine()
        letterOccurs.extend(line.count(Letter))
    return max(letterOccurs)

I need help finding the number of lines in the magazine. Is there a function to find the number of lines in something?

2 Answers2

0

In your for loop if line=null: break . Generally after the last line the document will return null.

0

the code below should work for you. It returns the line which includes given letter more than any other line.

If you want to return the count of letter in that line, simply return max_count.

def LetterNumberCount(letter):
    document = open('magazine.in', 'r')
    max_count = 0
    max_count_line = None
    for line in document.readlines():
       if line.count(letter) > max_count:
          max_count = line.count(letter)
          max_count_line = line
    return max_count_line
Ali Yılmaz
  • 1,463
  • 1
  • 9
  • 23