Tetris is a famous puzzle video game. The objective of the game is to complete lines by moving different shaped pieces (tetromonioes) that falls onto the grid one by one. The scores are calculated by the number of lines completed before the playing grid is filled to the top. Given an n x m playing grid (n rows, m columns), your task is to count how many lines are already completed. Each cell is either occupied by a block or empty. A line is considered complete when there is no empty space in the line. For example, for the following 10 x 5 grid,
Tetris is a famous puzzle video game. The objective of the game is to complete lines by moving different shaped pieces (tetromonioes) that falls onto the grid one by one. The scores are calculated by the number of lines completed before the playing grid is filled to the top. Given an n x m playing grid (n rows, m columns), your task is to count how many lines are already completed. Each cell is either occupied by a block or empty. A line is considered complete when there is no empty space in the line. For example, for the following 10 x 5 grid,
Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
Related questions
Question
Python: Tetris Simulator
![Tetris is a famous puzzle video game. The objective of the game is to complete lines by moving different shaped
pieces (tetromonioes) that falls onto the grid one by one. The scores are calculated by the number of lines completed
before the playing grid is filled to the top.
Given an n x m playing grid (n rows, m columns), your task is to count how many lines are already completed. Each
cell is either occupied by a block or empty. A line is considered complete when there is no empty space in the line.
For example, for the following 10 x 5 grid,
there are 4 completed lines in the above example.
You are to write a function tetris(grid:list[list[str]]) -> int that returns the number of completed lines where
the grid is given as a list of rows and each row is a list of string. Each cell can only be either 'x' or
.However,
some input grids might be non-rectangle. In such case, the function must raise a ValueError with the message non-
rectangle grid is invalid .](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F48954d8a-c361-4523-b68b-ac12d6ced482%2F69837ef7-cc9a-4167-a7a5-122c0a6de538%2Fhw6rngj_processed.png&w=3840&q=75)
Transcribed Image Text:Tetris is a famous puzzle video game. The objective of the game is to complete lines by moving different shaped
pieces (tetromonioes) that falls onto the grid one by one. The scores are calculated by the number of lines completed
before the playing grid is filled to the top.
Given an n x m playing grid (n rows, m columns), your task is to count how many lines are already completed. Each
cell is either occupied by a block or empty. A line is considered complete when there is no empty space in the line.
For example, for the following 10 x 5 grid,
there are 4 completed lines in the above example.
You are to write a function tetris(grid:list[list[str]]) -> int that returns the number of completed lines where
the grid is given as a list of rows and each row is a list of string. Each cell can only be either 'x' or
.However,
some input grids might be non-rectangle. In such case, the function must raise a ValueError with the message non-
rectangle grid is invalid .
![def check_tetris(grid:list[list[str]], expected_output, exception:bool):
Verifies if `tetris` function works correctly
try:
assert tetris(grid)
expected_output
==
except ValueError:
return exception
if exception:
return False
return True
grid1
[
'),
'),
['
'x',' '],
['x'
'x'],
['x',
,'x'],
['x',
'x',' '],
['x','x','
,'x', 'x'],
['x','x','x','x','x'],
['x',' ','x','x',' '],
['x','x','x','x','x'],
check_tetris(grid1, 4, False) # rectangle grid, 4 completed lines
grid2
[
['
[
'],
['
'x',' '],
['x','x'
,'x', 'x'],
check_tetris(grid2, 1, True) # non-rectangle grid, expecting a ValueError](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F48954d8a-c361-4523-b68b-ac12d6ced482%2F69837ef7-cc9a-4167-a7a5-122c0a6de538%2F9v4x44i_processed.png&w=3840&q=75)
Transcribed Image Text:def check_tetris(grid:list[list[str]], expected_output, exception:bool):
Verifies if `tetris` function works correctly
try:
assert tetris(grid)
expected_output
==
except ValueError:
return exception
if exception:
return False
return True
grid1
[
'),
'),
['
'x',' '],
['x'
'x'],
['x',
,'x'],
['x',
'x',' '],
['x','x','
,'x', 'x'],
['x','x','x','x','x'],
['x',' ','x','x',' '],
['x','x','x','x','x'],
check_tetris(grid1, 4, False) # rectangle grid, 4 completed lines
grid2
[
['
[
'],
['
'x',' '],
['x','x'
,'x', 'x'],
check_tetris(grid2, 1, True) # non-rectangle grid, expecting a ValueError
Expert Solution

This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
This is a popular solution!
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 2 images

Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Recommended textbooks for you

Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education

Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON

Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON

Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education

Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON

Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON

C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON

Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning

Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education