1

I was trying a leetcode, where we have to calculate the number of words in a sentence. The code I wrote calculates the number of words but for some reason it just appends 1 to the max_words list each time, regardless of the number of words.

def mostWordsFound(sentences):
    max_words=[]
    for i in range(0,len(sentences)):
        num_of_words=1
        for j in range(0,len(sentences[i])):
            if sentences[i][j]==" ":
                num_of_words+=1
                print(num_of_words)
            if j==len(sentences[i][j])-1:
                print("reached here")
                max_words.append(num_of_words)
            j+=1
        i+=1
    print(max_words)
                    
            
            
sentences = ["please wait.", "continue to fight.", "continue to win."]
mostWordsFound(sentences)
Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
Mustafa
  • 13
  • 2
  • 1
    `len(sentences[i][j])` is always going to be 1. `sentences[i]` is one of your three strings, `sentences[i][j]` is a *single character* from that string. – jasonharper Mar 30 '22 at 13:39
  • 1
    This is exactly the kind of problem debuggers were made to help solve. – Scott Hunter Mar 30 '22 at 13:39
  • 1
    If you are using an IDE **now** is a good time to learn its debugging features. If not using an IDE learn the built-in [Python debugger](https://docs.python.org/3/library/pdb.html). Printing *stuff* at strategic points in your program can help you trace what is or isn't happening. [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – wwii Mar 30 '22 at 14:15

1 Answers1

0

You were using len(sentences[i][j]) which will always be 1 because it represents a letter/char. You need to use len(sentences[i]) to get the length of the sentence. Also there's no need to use i += 1 or j += 1 as its value will automatically increment in for loop.

Code after modifications:

def mostWordsFound(sentences):
    max_words=[]
    for i in range(0,len(sentences)):
        num_of_words=1
        for j in range(0,len(sentences[i])):
            if sentences[i][j]==" ":
                num_of_words+=1
                print(num_of_words)
            if j==len(sentences[i])-1: 
                print("reached here")
                max_words.append(num_of_words)
    print(max_words)
                    
            
            
sentences = ["please wait.", "continue to fight.", "continue to win."]
mostWordsFound(sentences)
 

However, if you are interested here's a simple implementation:

def mostWordsFound(sentences):
    max_words= [sentence.split() for sentence in sentences] # This line splits the word on the basis of a space and creates a list.
    n_of_words = [len(word) for word in max_words] # This line counts the number of all words in split sentence list.
    print(n_of_words)     
            
sentences = ["please wait.", "continue to fight.", "continue to win."]
mostWordsFound(sentences)
Abhyuday Vaish
  • 2,357
  • 5
  • 11
  • 27