# Grid Reference

The Grid class in grid.py provides a simple 2-d storage utility. The values stored can be any Python value.

`Grid(width, height)` - constructs new grid. Initially all locations are None

`grid.width, grid.height` - width/height properties

`grid.get(x, y)` - returns contents at x,y (error if x,y out of bounds)

`grid.set(x, y, val)` - sets value into the grid at x,y (error if x,y out of bounds)

`grid.in_bounds(x, y)` - returns True if the x,y is in bounds of the grid. False otherwise.

`build(lst)` - constructs a new grid based on a nested-list format like this

``````# e.g. this makes a 3 by 2 grid:
grid = Grid.build([[1, 2, 3], [4, 5 6]])
# [1, 2, 3] is row y = 0
# [4, 5, 6] is row y = 1
``````

## Grid Demo

``````def grid_demo():
"""
Demonstrate use of the Grid class.
"""
# Create width 4 by height 2 grid, filled with None initially.
grid = Grid(4, 2)

# loop over contents in usual way,
# setting every location to 6
for y in range(grid.height):
for x in range(grid.width):
grid.set(x, y, 6)

# access 0,0
val = grid.get(0, 0)

# verify that 3,1 is in bounds
if grid.in_bounds(3, 1):
# set 3,1
grid.set(3, 1, 11)

print(grid)
# print uses nested-list format
# showing row-0, then row-1
# [[6, 6, 6, 6], [6, 6, 6, 11]]

# Grid.build() also uses nested-list format, allowing
# you to construct a grid on the fly.
grid2 = Grid.build([[6, 6, 6, 6], [6, 6, 6, 11]])
``````
{"ft_lang_label":"__label__en","ft_lang_prob":0.72215676,"math_prob":0.96158004,"size":1325,"snap":"2021-31-2021-39","text_gpt3_token_len":443,"char_repetition_ratio":0.11733535,"word_repetition_ratio":0.041666668,"special_character_ratio":0.36226416,"punctuation_ratio":0.2244898,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.95334303,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-07-29T12:06:24Z\",\"WARC-Record-ID\":\"<urn:uuid:dc9e499b-7ea9-4355-9af0-383ab5aaa9c4>\",\"Content-Length\":\"17579\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:c936de7d-562c-47cf-bc44-c76ade1ffda7>\",\"WARC-Concurrent-To\":\"<urn:uuid:84945f02-760a-4ca9-9eed-724720c9388d>\",\"WARC-IP-Address\":\"171.67.215.200\",\"WARC-Target-URI\":\"https://web.stanford.edu/class/cs106a/references/grid/grid-reference.html\",\"WARC-Payload-Digest\":\"sha1:BEC2OQDST35EDDEOMQMFMJEMW5UOM5ZS\",\"WARC-Block-Digest\":\"sha1:2I4S4GMXBKBC4CAGRTTRMBH5K7K5744E\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-31/CC-MAIN-2021-31_segments_1627046153857.70_warc_CC-MAIN-20210729105515-20210729135515-00473.warc.gz\"}"}