Midterm2
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 (5 points)
Write a Python program to create a list reflecting the run-length encoding from a given list of integers or a given list of characters.
Run-length encoding (RLE) is a form of lossless data compression in which runs of data (sequences in which the same data value occurs in many consecutive data elements) are stored as a single data value and count, rather than as the original run. For example:
'aaabaaa' --> [[3, 'a'], [1, 'b'], [3, 'a']] 'babaaaa' --> [[1, 'b'], [1, 'a'], [1, 'b'], [4, 'a']] 'bbbbbbbbbb' --> [ [10, 'b'] ]