0% found this document useful (0 votes)
44 views4 pages

Coding Challenge

Challenge Python

Uploaded by

karankumar71038
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views4 pages

Coding Challenge

Challenge Python

Uploaded by

karankumar71038
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Coding Challenge

You are to write a simplified Tetris engine.

The engine should model a grid that pieces enter from top and come to rest at the bo om, as if pulled
down by gravity. Each piece is made up of four unit squares. No two unit squares can occupy the same
space in the grid at the same me. The pieces are rigid, and come to rest as soon as any part of a piece
contacts the bo om of the grid or any res ng block. As in Tetris, whenever an en re row of the grid is
filled, it disappears, and any higher rows drop into the vacated space without any change to the internal
pa ern of blocks in any row.

Your program must process a text file of lines each represen ng a sequence of pieces entering the grid.
For each line of the input file, your program should output the resul ng height of the remaining blocks
within the grid.

The file denotes the different possible shapes by le er. The le ers used are Q, Z, S, T, I, L, and J. The
shapes of the pieces they represent are shown in the diagram below.

You do not have to account for shape rota on in your model. The pieces will always have the
orienta ons shown above.

Each line of the input file is a comma-separated list. Each entry in the list is a single le er (from the set
above) and a single-digit integer. The integer represents the le -most column of the grid that the shape
occupies, star ng from zero. The grid of the game space is 10 units wide. Your program need not detect
whether any sequence of pieces will exceed any par cular height, but you may assume that no test case
will result in a height of greater than 100. For each line of the file, the grid’s ini al state is empty.
For example, if the input file consisted of the line “Q0” the corresponding line in the output file would be
“2”, since the block will drop to the bo om of the ini ally empty grid and has height two.

An input file called "[Link]" has been provided. Your program should preferably be invoked from a
command line, taking its input from STDIN and wri ng its output to STDOUT. For instance:

$ ./tetris < [Link] > [Link]

Your program does not need to validate the file format and can assume that there will be no illegal
inputs in the file.

Example 1

A line in the input file contains “I0,I4,Q8” resul ng in the following configura on.

The filled bo om row then disappears.


Therefore, the output row for this sequence is “1”.

Example 2

A line in the input file contains “T1,Z3,I4”.

No rows are filled, so the output for this sequence is “4”.

Example 3

A line in the input file contains “Q0,I2,I6,I0,I6,I6,Q2,Q4”. A er the first three pieces drop, the result is as
follows:

The bo om line is cleared, and a er the next five pieces drop, here is the result:
The second line clears, and the final result is as follows:

Note that the rows drop as rows, and do not fill gaps in the rows below. So the final output for this test
case is “3”.

You might also like