Midterm2
(Új oldal, tartalma: „=Programming Problems:= ==Problem 1 (2 points)== Write a variadic function, which can have an arbitrary number of keyword arguments (**kwargs). The inputs are names an…”) |
|||
12. sor: | 12. sor: | ||
Write a Polynomial class, whose input is a dictionary. In the dictionary the keys are the degrees, and the values are | Write a Polynomial class, whose input is a dictionary. In the dictionary the keys are the degrees, and the values are | ||
the corresponding coefficients. | the corresponding coefficients. | ||
− | a | + | a. Write the __init__ method. (0.5 point) |
− | b | + | b. Write the __str__ method, which can print a polynomial in an aesthetic form. (1.5 point) |
1. Remark: Be careful not to print the parts with zero coefficients, and if the coefficient is 1 or -1 then only show | 1. Remark: Be careful not to print the parts with zero coefficients, and if the coefficient is 1 or -1 then only show | ||
it with a + or - sign before that part. | it with a + or - sign before that part. | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | 2. Remark: We can assume that the keys (degrees) in the dictionary are given in a monotone decreasing order. | |
− | the | + | c. Write an __add__ method for the Polynomial class. It might be useful to write a degree() method, which returns the degree of the polynomial. (3 point) |
− | + | Example: | |
− | + | Let p = Polynomial({24:1, 12:-1, 2:0, 1:0, 0:2}). | |
− | + | Then print(p) returns the following: | |
− | + | x^24 - x^12 + 2 | |
− | + | ==Problem 4 (6 points)== | |
+ | Write an iterable Primes class, which input is an n natural number, which is the upper bound of the primes. | ||
+ | a. Write the __init__ method. You should also test whether the input n is a natural number. If it isn't then make a ValueError exception. (1 point) | ||
+ | b. Write the __next__ and __iter__ methods. (Make the instances of the Primes class iterable, | ||
+ | you should be able to iterate on the primes from 2 to n.) (3 point) | ||
+ | c. Write the Mersenne_primes child class of the Primes class, which has a power() method which returns | ||
+ | for which n is it true that the prime = 2^n - 1 (2 point) |
A lap 2021. április 16., 00:04-kori változata
Tartalomjegyzék |
Programming Problems:
Problem 1 (2 points)
Write a variadic function, which can have an arbitrary number of keyword arguments (**kwargs). The inputs are names and the age of the person. Return the names of the persons whose age is above 50.
Problem 2 (3 points)
Write a variadic function, whose inputs are real numbers, and the function returns whether the given numbers form an arithmetic or geometric sequence or both or none of them.
Problem 3 (5 points)
Write a Polynomial class, whose input is a dictionary. In the dictionary the keys are the degrees, and the values are the corresponding coefficients.
a. Write the __init__ method. (0.5 point) b. Write the __str__ method, which can print a polynomial in an aesthetic form. (1.5 point)
1. Remark: Be careful not to print the parts with zero coefficients, and if the coefficient is 1 or -1 then only show it with a + or - sign before that part.
2. Remark: We can assume that the keys (degrees) in the dictionary are given in a monotone decreasing order.
c. Write an __add__ method for the Polynomial class. It might be useful to write a degree() method, which returns the degree of the polynomial. (3 point)
Example:
Let p = Polynomial({24:1, 12:-1, 2:0, 1:0, 0:2}). Then print(p) returns the following: x^24 - x^12 + 2
Problem 4 (6 points)
Write an iterable Primes class, which input is an n natural number, which is the upper bound of the primes.
a. Write the __init__ method. You should also test whether the input n is a natural number. If it isn't then make a ValueError exception. (1 point) b. Write the __next__ and __iter__ methods. (Make the instances of the Primes class iterable, you should be able to iterate on the primes from 2 to n.) (3 point) c. Write the Mersenne_primes child class of the Primes class, which has a power() method which returns for which n is it true that the prime = 2^n - 1 (2 point)