The Python-code can be retrieved on my Github repository regarding these assignments, linked below.

Dr. Eureka
Dr. Eureka is a game where players take on the role of apprentice scientists. They race to be the first to realize the correct combination of the colored balls in the test tubes, in the order prescribed on the current challenge card. In order to achieve this, players must transfer the balls from one tube to the other, without touching them with their hands or letting them fall from the tubes.
When all players are ready to get started, the top challenge card is turned face up. All at the same time, players race to complete the challenge by transferring their balls from one tube to another, trying to arrange their balls in the order shown on the current challenge card. Players must obey the following rules:
     - a combination can be completed with all tubes right side up (A)
     - a combination can be completed with one of the tubes flipped upside down (B)
The order of the test tubes (from left to right) doesn't matter in realizing a combination. Only the order of the colored balls in the test tubes (from top to bottom) matters. Players are allowed to swap the position of their tubes if that may help them.

If a player touches or drops one of his balls, or incorrectly announces that he has completed the challenge, he is immediately removed from the rest of the round (until the current challenge card is completed).
The game is played with a fixed number of test tubes. These tubes have a maximal capacity, which indicates the maximum number of balls that fit in a test tube.

The first player who arranges his balls in the correct combination, calls out "Eureka!". If all the players agree that his combination is correct, he scores the current challenge card as 1 point.

Players leave their balls where they were at the end of the previous challenge. They are not returned to a particular starting position. However, all tubes must be turned right side up before beginning a new round. When all player are ready to start again, a new round begins by drawing the next challenge card face up. The first player to 5 points (5 challenge cards) wins the game.

Assignment
Define a class Eureka that can be used to simulate how the players of Dr. Eureka can arrange the colored balls into their test tubes. To refer to the individual test tubes, the tubes are numbered from left to right, starting from 0.

When creating a simulation (Eureka), a string (str) must be passed that describes the configuration of the colored balls in the different test tubes. Each ball is represented as an uppercase letter that indicates the color of the ball (the same letters represent balls of the same color). Each test tube is described by the sequence of balls it contains, listed from bottom to top. The successive test tubes are separated by commas (,). For example, PG,GR,PR describes the configuration of three test tubes that can be seen in the first image of the introduction, where the uppercase letter P represents a purple ball, G a green ball and R a red ball.

The maximum capacity (int) of the test tubes can be passed to the optional parameter maximum (default value: 4). If one of the test tubes in the given configuration exceeds the maximum capacity, an AssertionError must be raised with the message invalid configuration.

If a test tube is flipped upside down, its number (int) can be passed to the optional parameter flipped. All test tubes are turned right side up by default. If an invalid number is passed, an AssertionError must be raised with the message invalid configuration.
If a simulation (Eureka) is passed to the built-in function repr, a string representation (str) must be returned that reads as a Python expression to create a new simulation with the same configuration as the object passed to the function repr. The maximum capacity of the test tubes must always be passed explicitly to the parameter maximum. Only if one of the test tubes is flipped upside down, its number must be passed to the parameter flipped.

If a simulation (Eureka) is passed to the built-in function str, a string representation (str) of the test tube configuration must be returned. The format of this representation can be derived from the examples below. Note that the top (= =) and bottom (===) of each test tube indicate whether the tube is upright or upside down. All tubes are separated from each other by a single space in the string representation.

In addition, the class Eureka must support at least the following methods:

     - A method flip that takes the number (int) of a test tube. The method must flip the given tube upside down, with the condition that at most one tube may be flipped at each point in time. It is therefore only allowed to flip a tube if all tubes are upright, or to flip the tube that was upside down back right side up. The method must return a reference to the object on which it was called. If an invalid test tube number is passed to the method or if the given test tube cannot be flipped, an AssertionError must be raised with the message invalid move.

     - A method transfer that takes the numbers (int) of two different test tubes: i) the source tube and ii) the destination tube for transferring balls. The method also has a third optional parameter that may take the number of balls (int) that must be transferred. It is not allowed to transfer balls if any of the tubes is flipped upside down, and the maximum capacity of the tubes cannot be exceeded in transferring balls. The method must return a reference to the object on which it was called. If an invalid test tube number is passed to the method, if there are less balls in the tube then must be transferred, or if the transfer is not allowed, an AssertionError must be raised with the message invalid move. The configuration of the colored balls may not be modified in that case.

Two simulations r and s (Eureka) are the same (r == s) if the colored balls are arranged in the same order (from bottom to top) in the test tubes. The order of the test tubes (from left to right) doesn't matter. Also the position of the test tubes (upright or upside down) or the maximum capacity of the tubes isn't taken into account.

Solution

Racetrack Playa
The Racetrack Playa is a scenic dry lake that is located above the northwestern side of Death Valley, in Death Valley National Park, California, U.S.. The playa is exceptionally flat and level, elevates 1132 m above sea level, with the northern end being only 4 cm higher than the southern end. During periods of heavy rain, water washes down from the Racetrack mountain area draining into the playa, forming a shallow, short-lived endorheic lake. Under the desert sun, the thin veneer of water quickly evaporates, leaving behind a surface layer of soft slick mud. As the mud dries, it shrinks and cracks into a mosaic pattern of interlocking polygons.

The lake is famous for a mysterious geological phenomenon of "sailing stones" that move and inscribe long tracks along the smooth valley floor without human or animal intervention. Stones with rough bottoms leave straight striated tracks, while those with smooth bottoms tend to wander. Stones sometimes turn over, exposing another edge to the ground and leaving a different track in the stone's wake. Trails differ in both direction and length. Rocks that start next to each other may travel parallel for a time, before one abruptly changes direction to the left, right, or even back to the direction from which it came. Trail length also varies – two similarly sized and shaped rocks may travel uniformly, then one could move ahead or stop in its track.

Assignment
We represent rocks as cuboid objects. A block always rests on the horizontal base (XY-plane) of a three-dimensional space with a fixed coordinate system as indicated in the figure below. The block is always viewed from a position along the positive X-axis in the direction of the YZ-plane. The front face of the block is always parallel with the YZ-plane. The dimensions of the object are described by its width W (measured along the X-axis), length L (measured along the Y-axis) and height H (measures along the Z-axis). The position of the object is described by the position of the corner point (x,y) at the bottom left of the front face of the object, as indicated by a black dot in the figure below, where x,y ∈ Z.
The surface area A of the block is given by:
A=2(L⋅W+L⋅H+H⋅W)
The volume V is given by:​​​​​​​
V=L⋅H⋅B
The length of the space diagonal d is given by:​​​​​​​
d=L2+H2+W2​​​​​​​
There are two ways the block can move: slide or tilt. 

The block may slide over its entire length to the left or to the right, or it may slide forward or backward over its entire width. This is illustrated in the figures below.​​​​​​​
The block may also tilt to the left, to the right, forward or backward. This is illustrated in the figures below.
Write a class Block that represents blocks having dimensions and a position as described above, who can move in the two given ways. This class should support at least the following methods:

      - An initialization method __init__ having three parameters length, height, width that take integer values (int) that respectively represent the length, the height and the width of the block. The initialization method also has an optional fourth parameter position that takes a sequence (list or tuple) containing two integers (int) that indicates the position of the corner point at the bottom left of the front face. In case no value is passed explicitly to the position, this corner point it located in the origin (0,0) of the base plane.

     - A method area (without arguments) that returns the surface area (float) of the block.

     - A method volume (without arguments) that returns the volume (float) of the block.

     - A method diagonal (without arguments) that returns the length (float) of the space diagonal of the block.

     - A method __repr__ (without arguments) that returns a string representation (str) of the current dimensions and position of the block. This string must read as a Python expression that creates a new object of the class Block that has the same dimensions and position as the current dimensions and position of the block on which the method is called. The position must always be represented as a tuple containing two integers (int). Derive the correct formatting of the string representation from the examples given below.

     - A method slide that takes a string argument (str) containing a single letter. This letter indicates the direction in which the method should slide the block: to the left (L), to the right (R), forward (F) or backward (B). The method must return a reference to the object (Block) on which the method was called, such that method calls can be chained. In case no uppercase letter was passed to the method that corresponds to one of the four directions, the method must raise an AssertionError with the message invalid direction.

     -A method tilt that takes a string argument (str) containing a single letter. This letter indicates the direction in which the method should tilt the block: to the left (L), to the right (R), forward (F) or backward (B). The method must return a reference to the object (Block) on which the method was called, such that method calls can be chained. In case no uppercase letter was passed to the method that corresponds to one of the four directions, the method must raise an AssertionError with the message invalid direction.

     - A method sail that takes a string argument (str) with an even length. The characters of this string must alternate between letters that indicate a type of movement (S for slide and T for tilt) and letters that indicate a direction (L for left, R for right, F for forward and B for backward). For example SB means slide backward, TR means tilt to the right, and SBTR means first slide backward and then tilt to the right. The method must execute the described movements one after the other and return a reference to the object (Block) on which the method was called, such that method calls can be chained. In case the given string contains a character at an even position that is no uppercase letter representing one of the two types of movement, the method must raise an AssertionError with the message invalid movement. In case the given string contains a character at an odd position that is no uppercase letter representing one of the four directions, the method must raise an AssertionError with the message invalid direction.

Solution

Grille
Assignment
In order to be able to refer to the cells of a grid (both for a grid with characters in which a secret message is hidden and for a grid with the apertures of a grille), the rows are numbered from top to bottom, and the columns from left to right, starting at zero in both cases. As a result, the position of a cell can be represented as a tuple (r,c), with r∈N (int) indicating the row index and c∈N (int) indicating the column index.

An n×n (n∈N0) grid that hides a secret message is stored in a text file. Each row of the grid is on a separate line, so that the file has n lines that each contain n characters. For example, the file containing the 5×5 grid that hides the secret message from the introduction, looks like this:

E hWe 
aosbt 
seVts 
m,e,' 
t.    s

Define a class Grille to represent grilles that can be used to decode secret messages. Two arguments must be passed when creating a grille (Grille): i) the number of rows/columns (int) of the square grid on the grille and ii) a collection (list, tuple or set) with the positions of the apertures in the grille. Each grille (Grille) must at least have the following properties:

     - dimension: number of rows/columns (int) of the square grid on the grille
     - apertures: set with the positions of the apertures in the grille

In addition, grilles (Grille) must at least support the following methods:

     - A method decode that takes the location (str) of a text file containing a square grid that hides a secret message. The method must return the fragment of the secret message (str) that can be read through the apertures (from left to right, and from top to bottom) when putting the grille on top of the grid.
     - A method rotate with an optional parameter clockwise that may take a Boolean value (bool). Based on the value of the parameter clockwise, the method must rotate the grille 90° clockwise (True, default value) or counterclockwise (False). The method must return a reference to the object it was called on.

If a grille (Grille) is passed to the built-in function str, a string representation (str) of its grid must be returned. Each row of the grid is on a separate line, cells with apertures are represented by the capital O, and cells without apertures are represented by a hash symbol (#).

Two grilles r and s (Grille) are equal (r == s) if they have the same number of rows/columns and if they can be rotated (multiples of 90°) such that all their apertures are at the same positions.

The addition (r + s) of two grilles r and s (Grille) produces a new grille t (Grille) that looks as if both grilles r and s are put on top of each other. In other words, the apertures in t are at positions where there are apertures in both r and s. The addition is only defined for grilles r and s that have the same number of rows/columns. If this is not the case, an AssertionError must be raised with the message invalid operation.

In the following excercise it is assumed that the text file message.txt is located in the correct directory.

Solution

Cover Drawing


All assignments and their complementary photographs were provided by Peter D. via the University of Ghent through a course that I took during my Master. All code is written by me, Arthur Mestdagh, in order to complete the course.​​​​​​​
Python excercises
Published:

Owner

Python excercises

Published: