Repeat-2

A MathWikiből

Tartalomjegyzék

Problem 1 (2.5 point):

Write a search() function, which first parameter is an x and after that it can have arbitrary many named parameters. (search(x, **kwargs)) The function should return True, if there exist a key in kwargs, for which the corresponding value is x. Otherwise it should return False.

Examples:

search(2, a=1, b=3) -> False
search(2, a=1, b=3, c=2) -> True

Problem 2 (3.5 point):

Write a variadic increasing() function, which inputs are arbitrary many positive natural numbers, and it returns True if the sequence of the numbers contains an increasing subsequence of length 3.

Examples:

increasing(1, 2, 1, 2, 4) -> True
increasing(1, 2, 1, 2, 1) -> False

Problem 3 (5 point):

Write a Vector class. It's parameter should be an arbitrary n dimensional vector. (A list of n real numbers.)

a) Write the __init__ and __str__ methods!(0.5 point)

b) Write the __mul__ method which calculates the dot product of two vectors. (1.5 points)

c) Write the norm method which calculates the length of the vector. (1 points)

d) Write a test method which returns True if the angle between the vector and the x-axis is between 0 and 45 degrees. It might be useful to use the norm method and to import the math package.(2 points)

Example:

v1 = Vector([1, 2, 3])
v2 = Vector([1, 0, 1])
print(v1) -> [1, 2, 3]
print(v1*v2) -> 4
print(v2.norm()) -> 1.41
print(v1.test()) -> False

Problem 4 (5 point):

Write an iterable Binomial class. It's parameter should be a positive natural number n. With an instance of this class we should be able to iterate the numbers (n choose k) from k=0 to k=n.

a) Write the __init__ method. You should also make an exception, raise an error if the input is not a positive natural number. (1 point)

b) Write the bin method, which input (apart from self) is a k natural number and it returns the value of (n choose k). (1 point)

c) Write the __iter__ method. (1 point)

d) Write the __next__ method. The iteration should be started with k=0. (2 points)

Example:

for j in Binom(3):
    print(j, end=' ')
Output: 1 3 3 1
Személyes eszközök