Skip to content

Instantly share code, notes, and snippets.

@internetsadboy
Last active August 29, 2015 14:08
Show Gist options
  • Select an option

  • Save internetsadboy/506f1342360357eb87c8 to your computer and use it in GitHub Desktop.

Select an option

Save internetsadboy/506f1342360357eb87c8 to your computer and use it in GitHub Desktop.
690 Midterm: use a 1D array to simulate a 2D array.
"""
Problem: use 1D array to simulate 2D array
"""
array_1D = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
array_2D = [[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11]]
"""
simulate_2D_array
Convert a 2D address (row index and column index) into a 1D single address (one index)
@param {integer} row the 2D row index value
@param {integer} col the 2D column index value
@param {integer} row_len the 2D number of columns
"""
def simulate_2D_array(row, col, row_len):
return row * row_len + col
"""
Example
Show that array_2D[1][2] = array_1D[6] = 6
"""
assert (array_2D[1][2] == array_1D[6]) # true
assert (array_2D[1][2] == array_1D[7]) # false
print "array_2D[1][2] = " + str(array_2D[1][2]) # 6
print "array_1D[6] = " + str(array_1D[6]) # 6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment