<jupyter_start><jupyter_text>Abstract Data Structures: Abstract Data Structures 5.1.4 Describe the characteristics of a two-dimensional array.A two-dimensional array is a useful data structure, and is very common, and is basically a spreadsheet. They can also be thought of a a matrix, with an `x` and `y` axis.Remember that we can hold the columns of a spreadsheet. In these two arrays, the **length** is `4`<jupyter_code>COLUMN_A = Array.from_list(["Given Name", "John",  "Peter", "Mary"])
COLUMN_B = Array.from_list(["Surname",    "Smith", "Jones", "Moore"])
COLUMN_C = Array.from_list(["Salary",     35000,   60000,   100000])<jupyter_output><empty_output><jupyter_text>Notice that the headers of this spreadsheet are the first element of each array? We could also write it by rows, and be exactly the same content<jupyter_code>ROW_1 = Array.from_list(["Given Name", "Surname", "Salary"])
ROW_2 = Array.from_list(["John",       "Smith",    35000])
ROW_3 = Array.from_list(["Peter",      "Jones",    60000])
ROW_4 = Array.from_list(["Mary",       "Moore",    100000])<jupyter_output><empty_output><jupyter_text>It makes a lot more sense to keep these arrays contained in one variable, which we can do, like this:<jupyter_code># Set up each row
ROW_1 = Array.from_list(["Given Name", "Surname", "Salary"])
ROW_2 = Array.from_list(["John",       "Smith",    35000])
ROW_3 = Array.from_list(["Peter",      "Jones",    60000])
ROW_4 = Array.from_list(["Mary",       "Moore",    100000])

# Set up the one variable
ROWS = Array.from_list([ROW_1, ROW_2, ROW_3, ROW_4])

# py:
def display_array(array):
    cell_size = 0
    for row in array:
        row_max = max([len(str(r)) for r in row])
        cell_size = max([cell_size, row_max])
    for row in [array[0]]:
        print("|| ", end="")
        print(" | ".join([f"{r:^{cell_size}}" for r in row]), end="")
        print(" ||")
    print("|| ", end="")
    print(" | ".join(['-' * (cell_size) for r in array[0]]), end="")
    print(" ||")

    for row in array[1:]:    
        print("|| " + " | ".join([f"{r:^{cell_size}}" for r in row]), end=" ||\n")

# pseudo:
display_array(ROWS)<jupyter_output>3|  [33m"""[39;49;00m
   4|  [33mThis is something Array[39;49;00m
   5|  [33m"""[39;49;00m
   6|  
   7|  [37m# Set up each row[39;49;00m
   8|  ROW_1 = Array.from_list([[33m"[39;49;00m[33mGiven Name[39;49;00m[33m"[39;49;00m, [33m"[39;49;00m[33mSurname[39;49;00m[33m"[39;49;00m, [33m"[39;49;00m[33mSalary[39;49;00m[33m"[39;49;00m])
   9|  ROW_2 = Array.from_list([[33m"[39;49;00m[33mJohn[39;49;00m[33m"[39;49;00m,       [33m"[39;49;00m[33mSmith[39;49;00m[33m"[39;49;00m,    [34m35000[39;49;00m])
  10|  ROW_3 = Array.from_list([[33m"[39;49;00m[33mPeter[39;49;00m[33m"[39;49;00m,      [33m"[39;49;00m[33mJones[39;49;00m[33m"[39;49;00m,    [34m60000[39;49;00m])
  11|  ROW_4 = Array.from_list([[33m"[39;49;00m[33mMary[39;49;00m[33m"[39;49;00m,       [33m"[39;49;00m[33mMoore[39;49;00m[33m"[39;49;00m,    [34m100000[39;49;00m])
  12|  
  13|  [37m# Set up the one variable[39;49;00m
  14|  ROWS = Array.from_list([ROW_1, ROW_2, R[...]<jupyter_text>More simply, multidimentional arrays are **zero-indexed**, and in many (if not most) programming languages, use square brackets (`[][]`) to express them.```python    R  CROWS[0][0]   the first row, first columnROWS[1][1]   the second row, second column```Multi-dimensional arrays, instead of length, have two properties: **width** and **height**. The width is the "length" of the x-axis, and the height is the "length" of the y-axis.If you wanted to **invert** the multi-dimensional array, so that the data is **transposing**:<jupyter_code>ROW_1 = Array.from_list(["Given Name", "Surname"])
ROW_2 = Array.from_list(["John", "Smith"])
ROW_3 = Array.from_list(["Peter", "Jones"])
ROW_4 = Array.from_list(["Mary", "Moore"])

// Set up the one variable
ROWS = Array.from_list([ROW_1, ROW_2, ROW_3, ROW_4])

def transpose(array):
    return [*zip(*array)]
    
// same as above, but modified to format
def display_array(array):
    cell_size = 0
    for row in array:
        row_max = max([len(r) for r in row])
        cell_size = max([cell_size, row_max])

    for row in array:    
        print("|| " + f"{row[0]:^{cell_size}}", end=" ")
        print(" | ", end="")
        print("   ".join([f"{r:^{cell_size}}" for r in row[1:]]), end="")
        print(" ||")
        
display_array(transpose(ROWS))<jupyter_output><empty_output><jupyter_text>5.1.5 Construct algorithms using two-dimensional arrays.<jupyter_code>MAZE = [
    [0, 0, 1, 0, 1, 3],
    [1, 1, 1, 1, 0, 1],
    [1, 1, 0, 1, 1, 1],
    [1, 1, 1, 0, 1, 1],
    [1, 0, 1, 0, 0, 0],
    [2, 0, 1, 1, 1, 1]
]
MAZE_WIDTH = 6
MAZE_HEIGHT = 6

// py:
def print_maze(maze):
    print("----" * MAZE_WIDTH)
    for row in maze:
        for column in row:
            if column == 0:
                print("|***", end="")
            elif column == 1:
                print("|   ", end="")
            elif column == 2 or column == -1:
                print("| 1 ", end="")
            elif column == 3:
                print("|===", end="")
        print("|")
        print("----" * MAZE_WIDTH)
            
// pseudo:

sub search(X, Y)

    # first validate parameters
    if X < 0 or Y < 0 then
        return false
    else if X >= MAZE_WIDTH OR Y >= MAZE_HEIGHT then
        return false
    end if

    if MAZE[X][Y] = 3
        MAZE[X][Y] = -1
        output "Completed!"
        print_maze(MAZE)
        return true
    else if MAZE[X][Y] = 0 then
        return false
    else if MAZE[X][Y] = -1 then
        return false
    end if

    MAZE[X][Y] = -1
    
    # explore neighbors
    return search(X+1, Y) OR search(X, Y-1) OR search(X-1, Y) OR search(X, Y+1)

end sub

output "Start:"
print_maze(MAZE)
print()
search(5, 0)<jupyter_output>[0;31mProgramming error on line 12:
	// py: (pseudocode)
	// py: (python)
SyntaxError: invalid syntax
[0m
{'fraction_non_alphanumeric': 0.18478260869565216, 'fraction_numerical': 0.0782608695652174, 'mean_word_length': 3.391336270190896, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '473505', 'license': 'MIT', 'n_tokens_mistral': 2651, 'n_tokens_neox': 2284, 'n_words': 584, 'path': 'Topic 5 Abstract data structures/2) Abstract Data Structures (5.1.4-5.1.10).ipynb', 'repo_name': 'classroomtechtools/ib_dp_course', 'size': '5980'}