Informatics2-2021/Lab09
(→range) |
|||
(egy szerkesztő 2 közbeeső változata nincs mutatva) | |||
15. sor: | 15. sor: | ||
* Its constructor should have one parameter: a number or a string. The iteration should go up that number, from 0 with 1 steps. | * Its constructor should have one parameter: a number or a string. The iteration should go up that number, from 0 with 1 steps. | ||
* If the number is not positive, then the iteration should take 0 steps. | * If the number is not positive, then the iteration should take 0 steps. | ||
− | * If you get a string then try to convert it to a number. | + | * If you get a string then try to convert it to a number. If it cannot be converted, then raise a '''ValueError''' exception. |
** If it is a valid integer, then calculate with that. | ** If it is a valid integer, then calculate with that. | ||
* If you get the string '''"inf"''' then make the iteration go endless (infinite loop)! | * If you get the string '''"inf"''' then make the iteration go endless (infinite loop)! | ||
− | |||
== Shapes == | == Shapes == | ||
47. sor: | 46. sor: | ||
average(2, 3, 4, 5, 6) --> 4 | average(2, 3, 4, 5, 6) --> 4 | ||
average(2, 3, 4, 5, 6, ...) | average(2, 3, 4, 5, 6, ...) | ||
+ | |||
+ | ==Union Sets== | ||
+ | Use variadic function strategy to write a function which gives the union of any number of sets. For example: | ||
+ | union({1, 2, 3 }, {2, 3, 4}, {3, 4, 5}) --> {1, 2, 3, 4, 5} | ||
+ | |||
+ | Hint: use .update() method to add a new element to a set. |
A lap jelenlegi, 2021. április 14., 10:20-kori változata
Tartalomjegyzék |
Exercise
range
Write an iterable class like range, but without returning a whole list, but storing only the actual element.
class Range: def __init__( ... ): ... def __iter__( ... ): ... def __next__( ... ): ...
- Its constructor should have one parameter: a number or a string. The iteration should go up that number, from 0 with 1 steps.
- If the number is not positive, then the iteration should take 0 steps.
- If you get a string then try to convert it to a number. If it cannot be converted, then raise a ValueError exception.
- If it is a valid integer, then calculate with that.
- If you get the string "inf" then make the iteration go endless (infinite loop)!
Shapes
Write a class called Shape.
- Let it have two members: x and y, the coordinates of the shape on the plane (center of mass).
- Define a move method, with one parameter v: a list of length 2, a vector to translate the shape with. After this method the coordinates should be changed.
Define the following classes as children of Shape:
- Ellipse with additional parameters (except the (x, y) coordinates): a and b the x and y axes radii
- Rectangle with additional parameters (except the (x, y) coordinates) a and b the length of the sides
Write an area method for both, which calculates the area!
Define an equation method for printing the equation of the Ellipse! Something like:
((x-1)/2)^2 + ((y-2)/3)^2 = 1
Palindrome
Write a function, called Palindrome, with one parameter which is a string. The function should check whether the string is a palindrome or not. The function should return "True" if the string is a palindrome, otherwise It returns "False".
Mersenne Prime
Write a function, called Mersenne, with one parameter which is an integer. The function should check whether the number is a Mersenne Prime or not. The function should return "True" if the number is a Mersenne prime number, otherwise It returns "False".
Average
Use variadic function strategy to write a function average which gives the average of all numbers from the parameters. For example:
average(2, 3, 4) --> 3 average(2, 3, 4, 5, 6) --> 4 average(2, 3, 4, 5, 6, ...)
Union Sets
Use variadic function strategy to write a function which gives the union of any number of sets. For example:
union({1, 2, 3 }, {2, 3, 4}, {3, 4, 5}) --> {1, 2, 3, 4, 5}
Hint: use .update() method to add a new element to a set.