Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Introduction to Obiect-oriented Analvsis and Desion 4. (10 points) Special methods and syntax sugar. Check whether or not a number is prime using the in

Introduction to Obiect-oriented Analvsis and Desion

image text in transcribed
image text in transcribed
image text in transcribed
4. (10 points) Special methods and syntax sugar. Check whether or not a number is prime using the in operator. A prime number is a natural number greater than 1 and is divisible only by 1 and itself. (a) (2 points) Complete the definition of _contains_ in class PrizeVueber. Hint: Use the helper function isprime. (b) (2 points) Now, suppose you change the method name from _contains_- to contains, as follows. elasaPrimeNumberfdefEontains(nelf+x)4pass Can you still make an object from class PrimeNumber? Answer Yes or No. (c) (2 points) After changing -contains.. to contains, what is the output of the following statement: print(issubclass (PrimeNumber, Container))? Answer True or False. (d) (2 points) Now, suppose class PrimeNumber inherits from the abstract base class Container, as follows. elnanPrimeXumber(Container):defContaina(tolf,x)pasa (a) (2 points) In the above code, we imported three classes, i.e., Iterable, Iterator and Generator. All of them are Abstract Base Class. Answer True or False. (b) (2 points) Complete the definition of property fiveA such that it allows the following usage Note: You must use the given class method filter to avoid code duplication. (c) (4 points) Complete the initialization method _ init_. in class AttractionIterator to make next_. work. The special method next_. returns the next A.A.A.A attraction (if any). (d) (3 points) What is the output of the following statements in the above code? Answer True or False for each statement. print(ininatance(attraction=.Iterator))print(iainatance(attractionn.Iteratule))print(isinstanee(attractions.Geverator)) (e) (1 point) Select one option below that can best explain the phenomenon sern in question d. A. Mixin B. Duck Typing C. Simple inheritance D. Multiple inheritance (f) (2 points) What is the output of the following statements in the above code? atteractions = Attractionlietater(lat) for attraction In attractionat porint(attraction) forttraction in attractionat print(attraction) (g) (4 points) What is the output of the following statements in the above code? \begin{tabular}{l} attractions2 = Attractionlterable(1:t) \\ for attraction in attractions2: \\ print(attraction) \\ for attraction in attraction:2: \\ print(attraction) \\ \hline \end{tabular} (h) (2 points) It is quite tedious to define the special method next_. in an Iterator class. We could make the job easier by using the yield keyword. Please complete the following functionstyle definition for five_a_attractions that generates the same result as in question ff 4. (10 points) Special methods and syntax sugar. Check whether or not a number is prime using the in operator. A prime number is a natural number greater than 1 and is divisible only by 1 and itself. (a) (2 points) Complete the definition of _contains_ in class PrizeVueber. Hint: Use the helper function isprime. (b) (2 points) Now, suppose you change the method name from _contains_- to contains, as follows. elasaPrimeNumberfdefEontains(nelf+x)4pass Can you still make an object from class PrimeNumber? Answer Yes or No. (c) (2 points) After changing -contains.. to contains, what is the output of the following statement: print(issubclass (PrimeNumber, Container))? Answer True or False. (d) (2 points) Now, suppose class PrimeNumber inherits from the abstract base class Container, as follows. elnanPrimeXumber(Container):defContaina(tolf,x)pasa (a) (2 points) In the above code, we imported three classes, i.e., Iterable, Iterator and Generator. All of them are Abstract Base Class. Answer True or False. (b) (2 points) Complete the definition of property fiveA such that it allows the following usage Note: You must use the given class method filter to avoid code duplication. (c) (4 points) Complete the initialization method _ init_. in class AttractionIterator to make next_. work. The special method next_. returns the next A.A.A.A attraction (if any). (d) (3 points) What is the output of the following statements in the above code? Answer True or False for each statement. print(ininatance(attraction=.Iterator))print(iainatance(attractionn.Iteratule))print(isinstanee(attractions.Geverator)) (e) (1 point) Select one option below that can best explain the phenomenon sern in question d. A. Mixin B. Duck Typing C. Simple inheritance D. Multiple inheritance (f) (2 points) What is the output of the following statements in the above code? atteractions = Attractionlietater(lat) for attraction In attractionat porint(attraction) forttraction in attractionat print(attraction) (g) (4 points) What is the output of the following statements in the above code? \begin{tabular}{l} attractions2 = Attractionlterable(1:t) \\ for attraction in attractions2: \\ print(attraction) \\ for attraction in attraction:2: \\ print(attraction) \\ \hline \end{tabular} (h) (2 points) It is quite tedious to define the special method next_. in an Iterator class. We could make the job easier by using the yield keyword. Please complete the following functionstyle definition for five_a_attractions that generates the same result as in question ff

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_2

Step: 3

blur-text-image_3

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

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2010 Barcelona Spain September 2010 Proceedings Part 3 Lnai 6323

Authors: Jose L. Balcazar ,Francesco Bonchi ,Aristides Gionis ,Michele Sebag

2010th Edition

3642159389, 978-3642159381

More Books

Students also viewed these Databases questions

Question

d. Present your teams conclusions to the class.

Answered: 1 week ago

Question

List out some inventory management techniques.

Answered: 1 week ago

Question

Understand the different approaches to job design. page 167

Answered: 1 week ago