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

Using python and while loop only, Write code that takes a string and a list of s

ID: 3725201 • Letter: U

Question

Using python and while loop only,

Write code that takes a string and a list of substrings, each with a length of three. Return True or False depending on whether the strings contains any of the substrings. For example, if the string was happybirthday, and the list of substrings was ['hay', 'pyr', 'bir', 'hdy'], your code would return True. Do not use the in keyword.
You may not use any built-in functions/methods besides len() and .append().

Template:

def inside(string, list):

    string = string
    list = list
    
    
    #YOUR CODE GOES HERE (indented)

    return False
    #END YOUR CODE

Explanation / Answer

def inside(string, list):
flag = True
# this is to mark is output is True/False
for i in list:
# interate over the list
for j in i:
# interate over the characters of string
if j not in string:
#check for presence of each string in string
flag = False
  
return flag

if __name__ == "__main__":
str = 'happybirthday'
  
#False case as xyz is not substring of str
list = ['hay', 'pyr', 'xyz', 'hdy']
print(inside(str,list))
  
#True case as all are substring of str
list = ['hay', 'pyr', 'bir', 'hdy']
print(inside(str,list))

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