#Python
>>> animalsOnFarm = ['cow', 'cow', 'cow', 'dog', 'cat', 'bird']
>>> farmAnimals(animalsOnFarm)
['cow']
>>> animalsOnFarm = ['pig', 'sheep', 'cow', 'cow', 'cow', 'dog', 'cat', 'bird']
>>> farmAnimals(animalsOnFarm)
['pig', 'sheep', 'cow']
>>>
I have written the program using PYTHON PROGRAMMING LANGUAGE.
OUTPUT :
CODE :
#function definition for farmAnimals
def farmAnimals(animalsOnFarm):
farmanimals = ['cow','chicken','goat','pig','sheep']
output = []#declared the output variable
if('list' in str(type(animalsOnFarm)) and len(animalsOnFarm)!=0):
#iterating over the animalsOnFarm list
for animal in animalsOnFarm:
if(animal not in output and animal in farmanimals):
#adding to the output variable if animal is not existing in it before
output.append(animal)
else:
continue
return output#return output
else:
#in case if the length of input list is zero
return "Invalid Input!!!"
#defind the input list
input_list = ['cow', 'cow', 'cow', 'dog', 'cat', 'bird']
#printing the input list and output list on the console
print("\nINPUT :\n"+str(input_list))
print("\nOUTPUT : \n"+str(farmAnimals(input_list)))
Thanks..
Get Answers For Free
Most questions answered within 1 hours.