Trying to complete one of my uni I got stuck with one of the exercises, which is counting the letters of each word from a text I have to receive until the summation of all the letters is >= 1000 but it never reaches 1000 even if the text has more than 1000.
First of all I thought it might have been because I´m not getting the full message but after looking and messing with different functions I created (none of them solved the issue) it seems like my counter function is not working. (this counter function returns an numeric array which later I convert into a string)
Is there something I´m not getting that is messing up my code?
def letterCounter(text):
counter = 0
indice = 0
lettTotal = 0
totalCount = []
while True:
if text[indice] == " ":
totalCount.append(counter)
counter = 0
indice = 1
else:
counter = 1
lettTotal = 1
indice = 1
if lettTotal >= 1000:
break
return totalCount
EDIT: -Sample imput:
authority stuff Design reach speak professor worker Executive future factor police spring live seek specific Easy What lot continue chair area military may matter Mention act bed Government all positive reflect Product plan Check Painting drop bar general weight anything to policy Control series over gas blue trip sure form care commercial hand term offer determine structure score Produce Person whatever someone among hundred teacher act garden hundred red appear With stock Citizen school answer stock as start number arm real himself Rate notice Under hair low Form avoid task get baby card anyone deep suggest adult Game order road able heavy Police culture reduce teacher third such he She Just your with ahead pick report camera sure choose wife general Hundred present land will Guess within skin Line never friend from Contain edge accept as list Civil drop cost newspaper anything section activity New no arm individual Statement base Build per figure main stay Eat See throw Authority difference last see night across baby firm Company So source treat computer plant how across news just her me hope without include news seem difficult after low yet style parent picture clear subject during true office owner but turn can war never see long cell so list current top her end May interest course list figure start least risk need network mention Floor camera inside he Himself how under Girl Behavior Through Practice analysis enter region very next Behavior under central cause hand Whole situation detail knowledge Successful institution task lay attack Prove ten participant Physical woman religious Her should friend Blue though throw claim deep item Ask who Several finally national candidate serve officer East month operation peace Ten street loss kind prepare attack against experience Institution toward course Dinner build impact final Rise find arrive hold Understand Others story close citizen main six region tree sometimes choice apply data student trial get food discuss production country husband quality finally eye in beautiful lose lay Sister whom thus between still Top management Fact color such
Output:
[9, 5, 6, 5, 5, 9, 6, 9, 6, 6, 6, 6, 4, 4, 8, 4, 4, 3, 8, 5, 4, 8, 3, 6, 7, 3, 3, 10, 3, 8, 7, 7, 4, 5, 8, 4, 3, 7, 6, 8, 2, 6, 7, 6, 4, 3, 4, 4, 4, 4, 4, 10, 4, 4, 5, 9, 9, 5, 7, 6, 8, 7, 5, 7, 7, 3, 6, 7, 3, 6, 4, 5, 7, 6, 6, 5, 2, 5, 6, 3, 4, 7, 4, 6, 5, 4, 3, 4, 5, 4, 3, 4, 4, 6, 4, 7, 5, 4, 5, 4, 4, 5, 6, 7, 6, 7, 5, 4, 2, 3, 4, 4, 4, 5, 4, 6, 6, 4, 6, 4, 7, 7, 7, 4, 4, 5, 6, 4, 4, 5, 6, 4, 7, 4, 6, 2, 4, 5, 4, 4, 9, 8, 7, 8, 3, 2, 3, 10, 9, 4, 5, 3, 6, 4, 4, 3, 3, 5, 9, 10, 4, 3, 5, 6, 4, 4, 7, 2, 6, 5, 8, 5, 3, 6, 4, 4, 3, 2, 4, 7, 7, 4, 4, 9, 5, 3, 3, 5, 6, 7, 5]
Summation = 999, doesnt reach 1000 as it should
CodePudding user response:
If the text parameter your using has words separated by spaces you can do like below
def countLetters(sentence):#split your sentence to get the individual words
words=sentence.split(" ")
store=[]#declare an array to hold the length of each word
for word in words:
store.append(len(word))
for i in range(len(words)):#print the words alongside the length of each
print(words[i],store[i])
CodePudding user response:
You could use a list comprehension
to get your goal:
[len(w) for w in s.split()]
To get its sum()
:
sum([len(w) for w in s.split()])
Example
s = 'authority stuff Design reach speak professor worker Executive future factor police spring live seek specific Easy What lot continue chair area military may matter Mention act bed Government all positive reflect Product plan Check Painting drop bar general weight anything to policy Control series over gas blue trip sure form care commercial hand term offer determine structure score Produce Person whatever someone among hundred teacher act garden hundred red appear With stock Citizen school answer stock as start number arm real himself Rate notice Under hair low Form avoid task get baby card anyone deep suggest adult Game order road able heavy Police culture reduce teacher third such he She Just your with ahead pick report camera sure choose wife general Hundred present land will Guess within skin Line never friend from Contain edge accept as list Civil drop cost newspaper anything section activity New no arm individual Statement base Build per figure main stay Eat See throw Authority difference last see night across baby firm Company So source treat computer plant how across news just her me hope without include news seem difficult after low yet style parent picture clear subject during true office owner but turn can war never see long cell so list current top her end May interest course list figure start least risk need network mention Floor camera inside he Himself how under Girl Behavior Through Practice analysis enter region very next Behavior under central cause hand Whole situation detail knowledge Successful institution task lay attack Prove ten participant Physical woman religious Her should friend Blue though throw claim deep item Ask who Several finally national candidate serve officer East month operation peace Ten street loss kind prepare attack against experience Institution toward course Dinner build impact final Rise find arrive hold Understand Others story close citizen main six region tree sometimes choice apply data student trial get food discuss production country husband quality finally eye in beautiful lose lay Sister whom thus between still Top management Fact color such'
sum([len(w) for w in s.split()])
Output
1794
CodePudding user response:
The other solutions here may solve your homework, but let's look at why it was failing in the first place.
Notice that in the first part of the loop you are keying off the space character to append the current count. However while counting the last word you will always hit your letter total limit. Once you break
from the loop you can't add the last word (the one that was being counted).
If you rearrange your function to check for letter total after adding the word to the count, then you won't lose that last word.
while True:
if text[index] == " ":
total_counts.append(counter)
counter = 0
index = 1
if total_letters >= 1000:
break
else:
counter = 1
total_letters = 1
index = 1
Then the word count will be appended prior to breaking out of the loop.