Hi,I need help with problems in python programming. Would appreciate the help.Thank you
{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Part 1 Code\n", "import tkinter\n", "\n", "BLOCKED = 0 # site is blocked \n", "OPEN = 1 # site is open and empty\n", "FULL = 2 # site is open and full\n", "\n", "# drawing scale \n", "SCALE = 30 \n", "\n", "\"\"\"Draws the initial configuration of the grid.\n", " (You do not need to call this function in any new places)\"\"\"\n", "def drawGrid(grid, canvas): \n", " rows = len(grid)\n", " columns = len(grid[0])\n", " for row in range(rows):\n", " for col in range(columns):\n", " if grid[row][col] == BLOCKED:\n", " drawSquare((row, col), 'black', grid, canvas)\n", " else:\n", " drawSquare((row, col), 'white', grid, canvas)\n", " \n", "\"\"\" Draws one square in the given color at the\n", " given position in the grid.\n", " (You will call this in order to \"fill-in\" squares )\n", " \n", " Parameters: \n", " pos: a (row, column) tuple\n", " color: a color string\n", " canvas: a Tkinter canvas object\n", " \n", " Return value: None (void function)\"\"\"\n", "def drawSquare(pos, color, grid, canvas): #anytime you visit a cell, you should be calling this function\n", " n = len(grid)\n", " m = len(grid[0])\n", " outline = \"white\"\n", " if color == \"white\":\n", " outline = \"black\"\n", " (row, col) = pos\n", " scaleRow = SCALE*row + 10\n", " scaleCol = SCALE*col + 10\n", " canvas.create_rectangle(scaleCol, scaleRow, scaleCol + SCALE, scaleRow + SCALE,\n", " outline=outline, fill=color, width=1)\n", " if row == 0:\n", " canvas.create_line(scaleCol, scaleRow, scaleCol + SCALE, scaleRow, fill=\"black\")\n", " if col == 0:\n", " canvas.create_line(scaleCol, scaleRow, scaleCol, scaleRow + SCALE, fill=\"black\")\n", " if row == n-1:\n", " canvas.create_line(scaleCol, scaleRow+SCALE, scaleCol+SCALE, scaleRow + SCALE, fill=\"black\")\n", " if col == m-1: \n", " canvas.create_line(scaleCol+SCALE, scaleRow, scaleCol+SCALE, scaleRow + SCALE, fill=\"black\")\n", " \n", "\"\"\"Do a depth first search on grid starting at site \n", " (row, col). If draw == True, visualize the \n", " percolation using tortoise.\"\"\"\n", "def dfs(grid, row, col, canvas, draw):\n", " # given the below function, change it to fit the above function. \n", "def dfs(grid, source, dest): # source is a tuple basically the same thing as row and col except your adding two parameter\n", "#dest is destination \n", " if source == dest: # base case, return true if we are at destination \n", " return True\n", " \n", " #define variable based on the grid\n", " (row, col) = square\n", " numberRow = len(grid)\n", " numCols = len(grid[0])\n", " \n", " #base case: return False if source is invalid\n", " if (row < 0)="" or="" (row="">= numRow) \\\n", " or (col < 0)="" or="" (col="">= numCols)\\\n", " or grid[row][col] == BLOCKED \\\n", " or grid[row][col] == VISITED \\\n", " return false\n", " grid[row][col] = VISTIED # mark that we have visited the location\n", " \n", " #Recursive case: do DFS on each adjacent position (above, below, left, right)\n", " above = (row - 1, col)\n", " below = (row + 1, col)\n", " left = (row, col - 1)\n", " right = = (row, col + 1)\n", " \n", " if dfs (grid, above, dest) == True: #dfs above\n", " return True\n", " if dfs (grid, below, dest) == True: # dfs below\n", " return True\n", " if dfs (grid, left, dest) == True: # dfs left\n", " return True\n", " if dfs (grid, right, dest) == True: #def rigth\n", " return True\n", " # no dfs was able to reach destination so return False\n", " return False\n", "\n", "\"\"\" Decide whether a grid percolates.\n", " If draw == True, then visualize the percolation with \n", " in a graphics window.\"\"\" \n", "def percolates(grid, draw):\n", "\n", " numRows = len(grid)\n", " numCols = len(grid[0])\n", " canvas = None\n", " \n", " # if boolean draw is set to be true...\n", " if draw == True: # set of the the graphics window\n", " tkObject = tkinter.Tk()\n", " canvas_width = numCols*SCALE + 20\n", " canvas_height = numRows*SCALE + 20\n", " canvas = tkinter.Canvas(tkObject, width = canvas_width, height = canvas_height)\n", " drawGrid(grid, canvas)\n", " canvas.pack()\n", " \n", " # *** \"POUR THE FLUID\" ***\n", " # (add your code here)\n", " \n", " # final update to graphics window \n", " if draw == True:\n", " tkObject.update()\n", " tkObject.mainloop()\n", " \n", " # return true or false \n", " pass \n", "\n", "def main(): \n", " exampleGrid = [[OPEN,OPEN,OPEN,OPEN,OPEN],\n", " [BLOCKED,BLOCKED,BLOCKED,BLOCKED,OPEN],\n", " [OPEN,OPEN,OPEN,OPEN