186k views
5 votes
BlockPy: #35.2) Animal Splits Write a function all_cats that consumes a comma-separated string of animals and prints whether all of the animals have "cat" in their name. For example, the string "gerbil,catfish,dog,cat" would return False, but just "cat,catfish" would return True. If the function consumes an empty string, then it returns True. Call this function on a string of animals of your choice and print the result.

1 Answer

3 votes

Answer:

def all_cats(data):

dataArray = data.split(',')

if(data==""):

return True

for i in dataArray:

if('cat' not in i):

return False

return True

print(all_cats("gerbil,catfish,dog,cat"))

print(all_cats("cat,catfish"))

print(all_cats(""))

Step-by-step explanation:

Step 1: define de function and the parameters

def all_cats(data):

Step 2: split the data by ,

dataArray = data.split(',')

Step 3 : validate if is an empty data and return true

if(data==""):

return True

Step 4: Loop over de array data and validate if have cat in each data if not, then return false

for i in dataArray:

if('cat' not in i):

return False

Step 5 if have cat in each one return true

return True

Step 6 Validate with examples

print(all_cats("gerbil,catfish,dog,cat"))

print(all_cats("cat,catfish"))

print(all_cats(""))

User Estelita
by
6.0k points