631. Design Excel Sum Formula #389
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Resolves: #242
Algorithm:
When calculating the value of each cell, there are 2 possibilities:
set()method updates the value of some cell to an integer.sum()method updates the value of some cell to a collection of cell names.In order to get the value of summed cells, 2. type of cells, we try to calculate the value of each cell/rectangle they point at.
Let's say:
D4 = [B1:C2, A2]. In order to calculateD4, we need to calculate The rangeB1:C2. This range might also contain references to other ranges, for example a case might be:B2 = [A1:A3]. For calculating this, we also need to sum this range. This yields a recursive method where we calculate the value of each cell each time by calculating the ranges they point to. When values are updated, the structure recalculates such values each time again, so we don't have to worry about updating something like the prefix sum etc.In implementation, cells are
Objects that are eitherInteger, pure value, orString[], reference to other cells by name, which gets parsed by thecalculate()method.