Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In python : Given a string s = \"011110010000000100010111\", the length of the l

ID: 3828390 • Letter: I

Question

In python : Given a string s = "011110010000000100010111", the length of the longest substring of s which contains consecutive "1"s would be 4. Write a function named longest_one, which accepts one input string consisting only of ‘0’ and ‘1’ characters. The function should return the size of the longest substring of consecutive ‘1’s. Note: your are not allowed to use string functions. You should use the programming methos (loops, conditionals). The function should return the desired output regardless of the input string size .

Explanation / Answer


#Function which returns the maximum length of consecutive 1's in given string
def str_len(s):
   m=0
   count=0
   for c in s:
       if c=='1':
           count+=1
       elif m<count:
           m=count
           count=0
       else:
           count=0
   if count>m:
       m=count
   return m
#Testing of function with given string
s = "011110010000000100010111"
print str_len(s)

Output :

4

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote