← 返回 ramp 的题目列表Implement a Basic Spreadsheet
类型:online_judge
Implement a basic Spreadsheet class.
The spreadsheet is initialized with 26 columns and 100 rows. Columns are named A through Z, and rows are numbered 1 through 100; therefore, valid cell references include A1, B75, and Z100.
Each cell may contain one of the following:
A string, such as "Name" or "John Doe".
A number, such as 263 or 75.
A formula string beginning with =. A formula references two cells and applies one basic arithmetic operator, for example:
=B1+B2
=B1-B2
=B1*B2
=B1/B2
Implement a Spreadsheet class with at least the following methods:
setCell(cell: str, value: str | int | float) -> None
getCell(cell: str) -> str | int | float
Requirements:
setCell(cell, value) stores a plain string, a number, or a formula in the specified cell.
getCell(cell) returns the cell's current value:
Return plain strings and numbers directly.
Evaluate and return the result for formula cells.
Formula cells depend on their referenced cells. If a referenced cell is later changed using setCell, a subsequent getCell on the dependent formula must reflect the updated value.
All input is valid. You do not need to handle invalid references, invalid formulas, circular dependencies, division by zero, or arithmetic on non-numeric values.
A formula contains exactly two cell references and one operator. Constants, parentheses, functions, and more complex expressions are not required.
Example:
sheet = Spreadsheet()
sheet.setCell("C1", 10)
sheet.setCell("C2", 5)
sheet.setCell("D2", "=C1*C2")
sheet.getCell("D2") # 50
sheet.setCell("C1", 7)
sheet.getCell("D2") # 35
Constraints: there are at most 2,600 cells, and every formula directly references exactly two cells.
Example
Input
7
SET C1 10
SET C2 5
SET D2 =C1*C2
GET D2
SET C1 7
GET D2
GET C2
Output
50
35
5