Ruby In Depth - Review Test

What You Will Build

Matrix (1)

Create a new data type called "MyMatrix". This data type is your own custom matrix with specifications as follows:

1. To initialise a new matrix, you need to pass the value of its row and column. It will initialize a matrix with nil elements and dimension as specified by the value of row and column.

matrix = MyMatrix.new(2, 3)
# will initialize a matrix with elements [[nil, nil, nil], [nil, nil, nil]]

Matrix (2)

2. We can assign an element of a matrix like this:

matrix = MyMatrix.new(2, 2)
matrix[1, 1] = 1
matrix[1, 2] = "a"
matrix[2, 1] = 2
matrix[2, 2] = "b"
# will return a matrix with elements [[1, "a"], [2, "b"]]

and we can access an element of a matrix like this:

# ...continued from the code above

matrix[1, 2]
# will return "a"

Matrix (3)

3. A matrix can be printed. Any empty (nil) elements of a matrix will be printed as "-". 

matrix = MyMatrix.new(2, 2)
matrix[1, 1] = 1
matrix[1, 2] = 2
matrix[2, 1] = 3

puts matrix
# will return:
# | 1 2 |
# | 3 - |

Matrix (4)

4. We can do scalar multiplication to a matrix: 

matrix = MyMatrix.new(2, 2)
matrix[1, 1] = 1
matrix[1, 2] = 2
matrix[2, 1] = 3
matrix[2, 2] = 4

matrix * 2
# will return a matrix with elements [[2, 4], [6, 8]]

Remember that a matrix can do scalar multiplication only if all of its elements are number.

Matrix (5)

5. We can do additions between two matrixes, as long as they have the same dimension and their elements are all number.

matrix = MyMatrix.new(2, 2)
matrix[1, 1] = 1
matrix[1, 2] = 2
matrix[2, 1] = 3
matrix[2, 2] = 4

another_matrix = MyMatrix.new(2, 2)
matrix[1, 1] = 2
matrix[1, 2] = 3
matrix[2, 1] = 4
matrix[2, 2] = 5

matrix + another_matrix
# will return a matrix with elements [[3, 5], [7, 9]]

Car Wash (1)

You are tasked with developing an allocation system app for a car wash. Here are the specifications:

1. A Car Wash can have n x n parking spaces.

# Example of what kind of Car Wash your app will generate
# when user specify the space as 3 x 3
# | - - - |
# | - - - |
# | - - - |

Car Wash (2)

2. When a car comes, you need to park it in the nearest parking space available. You should try to park it from top (row-wise) and left (column-wise).

# Example of what how cars are parked

# First, come car "A"
# | A - - |
# | - - - |
# | - - - |

# Then, come car "B", "C", and "D"
# | A B C |
# | D - - |
# | - - - |

When the parking space is full, your app will refuse to add any more car. It will return a message, "parking space is full".

Car Wash (3)

3. When a car is washed, you have to make sure that your app picks the first car that comes.

# Example of how cars are washed in turns

# First, when asked to wash a car, your app will give user car "A"
# | - B C |
# | D - - |
# | - - - |

# Then, comes car "E"
# | E B C |
# | D - - |
# | - - - |

# Now, if asked to wash a car again,
# your app should give user car "B", not car "E"
# because car "B" is the car that comes earliest by now
# | E - C |
# | D - - |
# | - - - |

When the parking space is empty but the user still ask your app to pick a car to be washed, it will return a message, "parking space is empty".

Car Wash (4)

4. Your app should be able to display list of the order how cars in the parking space will be processed.

# Example:

# Cars come: "A", "B", "C", "D"
# | A B C |
# | D - - |
# | - - - |

# Then, wash car "A", "B"
# | - - C |
# | D - - |
# | - - - |

# Then comes car "E"
# | E - C |
# | D - - |
# | - - - |

# Your app can print the order of cars to be washed as follow:
"The order of cars to be washed next are: C, D, E"

Car Wash (5)

5. Your app should be able to display list of all the cars that have been washed.

# Example:

# Cars come: "A", "B", "C", "D"
# | A B C |
# | D - - |
# | - - - |

# Then, wash car "A", "B"
# | - - C |
# | D - - |
# | - - - |

# Then comes car "E"
# | E - C |
# | D - - |
# | - - - |

# Your app can print the history:
"These cars have been washed: A, B"

How to Build It

1. You should write your solution in TDD manner.

2. You should write your solution in OOP manner.

3. You should write MyMatrix without using Ruby's existing Matrix data type.

4. You should not use any gem other than RSpec.

5. You do not need to implement your solution in MVC framework.

6. You can create as many classes and methods as you like, as long as you can justify your decision to create those classes and methods. Just add a "main.rb" file in your application's root folder so you can run a simulation of how your solution works in this file.

Good Luck, Have Fun!

Ruby In Depth - Review Test

By qblfrb

Ruby In Depth - Review Test

  • 306