0

Problem Detail

I am stuck in this... I can calculate the number of times "the" and "to" will occur by using the count function but it will also include the occurrence in these and top which is not needed so how should I approach this question? I only know basic operators, Loops, Str Functions, etc

What I Tried

I thought that the exact occurrence would be having " " in its T[EndChar+1] But how will I get the index of ending char where it is occurring

 N=input("")
print(N.count("the"))
# Input: these are the boxes
# Output: 2
# Expected Output: 1
# As I want the occurrence of "the" not "the" in "these"
    
Bill Hileman
  • 2,798
  • 2
  • 17
  • 24

1 Answers1

1

You want the number of occurrences of 'the' and 'to' in a string if I understood correctly.


#input
s = "These To The Top"

#split the string when space occurs -> s = ["These","To","The","Top"]

t = list(s.split(" "))

#filter when it equals to "The" or "To"
q = filter(lambda x:x == "The" or x == "To",t)

#join list as string
a = list(q).join(' ')

a.count("The")
a.count("To")

Or you can use regex

Aarav Shah
  • 55
  • 1
  • 7