Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

print(Part a) print(commonFactor(12,21)) print(commonFactor(60,336)) print(==========) print(Part b) print(primeFactor(24)) print(primeFactor(31)) print(primeFactor(420)) print(==========) print(Part c) print(LCM([3,4,6,10])) print(LCM([2,3,8,12,16,21])) print(==========) print(Part d) AnimalList = [dog,cat,whale,parrot,chicken,sparrow,fly,butterfly,bee] print(countAnimal(passage.txt)) print(==========) print(Part e)

image text in transcribed

print("Part a") print(commonFactor(12,21))

print(commonFactor(60,336)) print("==========")

print("Part b") print(primeFactor(24))

print(primeFactor(31))

print(primeFactor(420))

print("==========")

print("Part c")

print(LCM([3,4,6,10]))

print(LCM([2,3,8,12,16,21]))

print("==========")

print("Part d")

AnimalList = ["dog","cat","whale","parrot","chicken","sparrow","fly","butterfly","bee"]

print(countAnimal("passage.txt"))

print("==========")

print("Part e")

categoryDict =

{"mammal":["dog","cat","whale"],"bird":["parrot","chicken","sparrow"],"insect":["fly","butterfly","bee"]}

print(animalCategory("passage.txt",categoryDict))

3. 125 marks(General Python) You are to write simple Python functions to solve the following problems. Define auxiliary functions where appropriate. (a) (marks] Write a Python function common factor inl,n2) to find factors that are common to two numbers n, and n. For example, factors of 12 are 2. 3. 4.6. 12 and factors of 21 are 3. 7. 21. Thus the common one is only 3. def commonFactorinl,n2): A function to find common factors of nl and n2 Input: positive integer nl and n2 Output: return the list of common factors Sample testing: print (commonFactor (12,21)) print (common Factor (60, 336) > Sample output: 3, 4, 6, 12) (b) (5 marks] Write a Python function prime Factor in to find and print the factorization of a positive integer 2 x 2 x 2 x 3. def prime Factor (n): into prime factors. For example, 24- r A function to find and print the prime factors of n Input: positive integer n Output: return the list of prime factors eported erime perime factors Sample testing: print (Prime Factor (24) print (primeFactor (31) print (prime Factor (420) Sample output: 24 2 - 2 2 - 3 [2, 2, 2, 3) 31-31 (31) 420 - 2. 2.3.7 [2, 2, 3, 5, 7] (c) (5 marks) Based on (a), (b) or otherwise, write a Python function LCM (numbers) to find the Least Common Multiplier of a list of numbers. For example, the LCM of 3.4.6 and 10 is 60. def LCM (numbers): A function to find the LCM of a list of numbers Input: a list of positive integers Output: return the LCM of those numbers ) Sample testing: print (LCM [3,4,6,10))) print (LCM ([2, 3, 8, 12, 16, 21 Sample output: 60 336 (d) (5 marks] There is a text file containing sentences about animals. You are to count the number of times each animal is mentioned. For simplicity, all words in the file are in lower case and there are no punctuation marks. Write a Python function count Animal (filename) to read from a file and return a sorted list of tuples with the number of occurrences of each animal. The list of animals could be found in a variable called Animal List. def countAnimal (filename): A function to open and read a file and count the number of occurrences of animals Input: the name of a text file Output: return a sorted list of tuples of animals and counts Sample testing: AnimalList - ["dog","cat", "whale", "parrot", "chicken", "sparrow", "Fly", "butterfly", "bee"] print (countAnimal ("passage.txt") Sample output: ['bee', 1), ('butterfly'. 1), ('cat', 3), ('chicken', 2), ('dog', 3), (fly', 3), ('parrot', 0), sparrow', 2), (whale', 1)) (e) [5 marks] Based on (d). suppose that animals are classified into different categories, such as mammal. The category information is given as a dictionary. Making use of the function in (d), or otherwise, write a Python function animal category (filename, category) to read from a file and return a sorted list of categories with an aggregate count of those animals. def animal Category (filename, category) : A Function to read a file and count the number of occurrences of animals according to their category Input: the name of a text file and a dictionary of category Output: return a sorted list of tuples of animal categories and counts Sample testing: categoryDict - ("mammal":["dog","cat", "whale"],"bird": ["parrot", "chicken", "sparrow"],"insect":["fly", "butterfly", "bee"]} print (animal Category ("passage.txt", categoryDict)) Sample output: [('bird, 4), (insect, 5), (mammal', 7)) Content of the file passage.txt is shown below: there is a dog chasing a cat and watching the whale cat is avoiding dog and going for chicken and sparrow chicken tries to catch a fly and a bee fly wonders whyfly and butterfly look 30 different sparrow is also chased behind by cat and sometimes by dog Include the following statements in your .py file for testing. print("Parta) print (Common Factor (1221) print (common factor (60, 336) > print(" = = = print("Part b") print (prime Factor (24) print (primeFactor (31)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Logidata+ Deductive Databases With Complex Objects Lncs 701

Authors: Paolo Atzeni

1st Edition

354056974X, 978-3540569749

More Books

Students also viewed these Databases questions

Question

Do you think physicians should have unions? Why or why not?

Answered: 1 week ago