Homework05

A MathWikiből
(Változatok közti eltérés)
(Új oldal, tartalma: „== Problem 1 : range == Write an iterable class like '''range''', but without returning a whole list, but storing only the actual element. <python> class Range: d…”)
 
 
(egy szerkesztő egy közbeeső változata nincs mutatva)
32. sor: 32. sor:
 
     ((x-1)/2)^2 + ((y-2)/3)^2 = 1
 
     ((x-1)/2)^2 + ((y-2)/3)^2 = 1
  
== Problem 03Chess==
+
==Problem 3moduloz_init==
Define a class called '''Piece'''. This will represent a chess piece (a figure on the chessboard).
+
Store its position and its color (Black/White) and its '''__str__''' function should write the position like A2, G3 etc.
+
Store the position as integers.
+
  
* Define child classes '''King''' and '''Pawn'''!
+
Define a class called Moduloz representing modulo n numbers (integers).
* Implement their '''__str__''' method which also prints the type of the figure. (You can implement it in the parent class, too)
+
For example in modulo 5:  
** Look the unicode characters of the figures: [https://en.wikipedia.org/wiki/Chess_symbols_in_Unicode]
+
4 + 3 = 2 (because 7 % 5 = 2)  
** Or their [https://en.wikipedia.org/w/index.php?title=Algebraic_notation_(chess)#Piece_names_in_various_languages one letter names]
+
2 - 3 = 4 (because -1 % 5 = 4)  
* Every child class should have a '''.move(pos)''' method, where '''pos''' is a string like A3, G2 etc. Move the piece to that position if it is accepted by the chess rules. If the move was successful return '''True''' otherwise '''False'''!
+
4 * 3 = 2 (because 12 % 5 = 2)
* Define a '''PieceMoveError''' exception. If the move is not allowed, then raise this exception!
+
  
Implement the '''Board''' class. Store the list of figures.
+
You don't have to implement the operations yet, just define the __init__ and the __str__ methods.  
* Make an '''add''' method which puts a figure on the table.
+
* Implement a '''move(pos1, pos2)''' method in '''Board''' which moves a figure from one position to the other, if the move is allowed (handle the '''PieceMoveError''' exception).
+
** Mind that there may be an other figure on the '''pos2''' position. If the target position is occupied with the same player's figure, then the move is not allowed. If the target is the other player's figure, then remove their figure and replace it with the new figure (its a ''capture'').
+
** Its easier if you implement an '''.occupied(pos)''' method first, which tells whether a given position is occupied or not.
+
* Implement the other pieces: '''Knight, Rook, Bishop''' and '''Queen'''!
+
** Start with the '''Rook''', its move is the simplest. Mind that the figure cannot pass through existing figures!
+
** Implement the '''Knight''' class! The Knight and Queen can jump through other figures, so its not a problem for them.
+
** After the bishop its not hard to implement '''Queen'''.
+
* Define a '''check''' method in Board. Return the color of the player who's King is in a chess position, or empty string of there is no chess position!
+
** Modify the Board's move method not to allow self-checks (when the active player makes its own King into a chess)!
+
* Implement the '''__str__''' method of '''Board'''!
+
  
After all these, implement the '''start''' method which assigns the board to a starting position.
+
In the constructor you will have two parameters, except self. The first one is the base of the modulo, the second one is the actual number.  
  
==Note :==
+
The base will be a positive integer, the value will be an integer.
Problem 1: 1 point
+
  
Problem 2: 1 point
+
The __str__ should return a string, containing the value.
  
Problem 3: 2 points
+
For example:
 +
a = ModuloZ(5, 7)
 +
print a
 +
 
 +
Should print:
 +
2
 +
==Problem 4: moduloz_operations==
 +
 
 +
Implement the __add__, __sub__, __mul__ methods for the previous Moduloz class!
 +
 
 +
For example in modulo 5:
 +
4 + 3 = 2 (because 7 % 5 = 2)
 +
2 - 3 = 4 (because -1 % 5 = 4)
 +
4 * 3 = 2 (because 12 % 5 = 2)
 +
 +
Mind that the operations should return an object of class Moduloz, not an integer (int)!
 +
For example:  
 +
a = Moduloz(7, 9) b = Moduloz(7, 12)
 +
print a + b print a - b print a * b
 +
 
 +
should print:
 +
0 4 3
 +
In the test outputs you can see the sum, difference and the product of the two input numbers.
 +
 +
Hint
 +
 
 +
Use the previous exercise as a starting point.
 +
 
 +
==Problem 5: matrix_init===
 +
 
 +
Define a class called Matrix for representing matrices.
 +
 
 +
You have to implement the __init__ and __str__ methods.
 +
The constructor has one parameter (except self), a list of list of numbers. The elements of the matrix.
 +
The __str__ should return a multi-line string, containing the matrix in a tabular-like format.
 +
For example:
 +
m = Matrix([[1, 2], [13, 4], [5, 6]])  print m
 +
should print this:
 +
  1  2 
 +
13  4 
 +
  5  6
 +
The numbers are padded to the right in 4 characters width. There are 3 spaces before each element, except the 13 because there are 2 spaces there.
 +
 
 +
==Problem 6: matrix_operations==
 +
 
 +
Implement the __add__, __sub__, __mul__ methods for the previous Matrix class.
 +
 
 +
The matrices will be square shaped, so every operation is compatible.
 +
 
 +
For example:
 +
m1 = Matrix([[1, 2], [3, 4]]) m2 = Matrix([[1, 0], [0, 2]])  print a + b
 +
 
 +
should print this:
 +
    2  2 
 +
    3  6 
 +
 
 +
 
 +
In the test you can see the sum, difference and the dot product of the two input matrices.
 +
Hint
 +
 
 +
Use the previous exercise as a starting point.

A lap jelenlegi, 2021. május 25., 17:34-kori változata

Tartalomjegyzék

Problem 1 : 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. It 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)!


Problem 2: 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

Problem 3: moduloz_init

Define a class called Moduloz representing modulo n numbers (integers). For example in modulo 5:

4 + 3 = 2 (because 7 % 5 = 2) 
2 - 3 = 4 (because -1 % 5 = 4) 
4 * 3 = 2 (because 12 % 5 = 2) 

You don't have to implement the operations yet, just define the __init__ and the __str__ methods.

In the constructor you will have two parameters, except self. The first one is the base of the modulo, the second one is the actual number.

The base will be a positive integer, the value will be an integer.

The __str__ should return a string, containing the value.

For example:

a = ModuloZ(5, 7) 
print a 

Should print:

2 

Problem 4: moduloz_operations

Implement the __add__, __sub__, __mul__ methods for the previous Moduloz class!

For example in modulo 5:

4 + 3 = 2 (because 7 % 5 = 2) 
2 - 3 = 4 (because -1 % 5 = 4) 
4 * 3 = 2 (because 12 % 5 = 2) 

Mind that the operations should return an object of class Moduloz, not an integer (int)! For example:

a = Moduloz(7, 9) b = Moduloz(7, 12) 
print a + b print a - b print a * b 

should print:

0 4 3 

In the test outputs you can see the sum, difference and the product of the two input numbers.

Hint

Use the previous exercise as a starting point.

Problem 5: matrix_init=

Define a class called Matrix for representing matrices.

You have to implement the __init__ and __str__ methods. The constructor has one parameter (except self), a list of list of numbers. The elements of the matrix. The __str__ should return a multi-line string, containing the matrix in a tabular-like format. For example:

m = Matrix([[1, 2], [13, 4], [5, 6]])  print m 

should print this:

 1   2  
13   4  
 5   6 

The numbers are padded to the right in 4 characters width. There are 3 spaces before each element, except the 13 because there are 2 spaces there.

Problem 6: matrix_operations

Implement the __add__, __sub__, __mul__ methods for the previous Matrix class.

The matrices will be square shaped, so every operation is compatible.

For example:

m1 = Matrix([[1, 2], [3, 4]]) m2 = Matrix([[1, 0], [0, 2]])  print a + b 

should print this:

   2   2  
   3   6   


In the test you can see the sum, difference and the dot product of the two input matrices. Hint

Use the previous exercise as a starting point.

Személyes eszközök