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 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