Page 1 of 1

Problem In this assignment, you are going to write a Scheme program that finds the correct movements in a path. Figure 1

Posted: Fri May 20, 2022 10:14 am
by answerhappygod
Problem In This Assignment You Are Going To Write A Scheme Program That Finds The Correct Movements In A Path Figure 1 1
Problem In This Assignment You Are Going To Write A Scheme Program That Finds The Correct Movements In A Path Figure 1 1 (44.02 KiB) Viewed 28 times
Problem In this assignment, you are going to write a Scheme program that finds the correct movements in a path. Figure 1 shows an example path, which has a grid layout. In the grid, black cells are simply walls, which are basically obstacles for you. You can move among the white cells and you cannot pass the boundaries of the grid. In each path, the starting location will be the square of [0,0]. Additionally, there is also one white cell labeled with F. This label shows the finish square of the path. So, your aim is to find the movements from the starting location to the finish location. To this end, you can move in 4 directions; up, down, left, right. These 4 directions will be represented by characters U, D, L, and R, respectively. 0 1 2 3 4 0 1 2 3 4 F Figure 1: Sample Path The solution for the path shown in Figure 1 is "DDRRRRDD", which means move down 2 times, then move right 4 times and move down 2 times. The path is not a maze! It is a simple one way road and It has only one solution: there is always one possible next square for each move.

Tasks In Scheme, a path will be represented in the form of a linked-list. Figure 2 shows how the path in Figure 1 is represented in terms of a linked list in Scheme. Starting cell [0,0] has the letter S, the finishing cell has the letter F and empty cells have the letter E. The walls have the letter - (minus). 0 1 2 3 4 0 s 1 E - 2 E E E E F 3 TI - E 4 Figure 2 - Linked List representation of the path shown in Figure 1 The following function "buildPath" on the left is given for you which takes a list of lists and creates a path (grid) using the lists. You can use this function to create different paths in order to test your code. On the right the code shows how the path in Figure 2 is created. (define (buildPath rows) (cond ((null? rows) null) (else (cons (build-path (cdr rows) ) (car rows))))) (define sample-path (build-path "("S" "-" "_" "_" "_") "_" "_" "_" "_") ("E" "E" "E" "E" "E") "E") "-" "-" "E")))) Scheme code for creating a path Scheme code of the sample path in Figure 2

Task 1: Define two functions "getHeight" and "getwidth" which takes a path as an input and returns the height and the width of the path. (getHeight sample-path) → should return 5 (getwidth sample-path) →should return 5 Task 2: Define a function "getLetter" which takes a path, a row number and a column number. Then it returns the letter from the path on the corresponding location [row, column] (getLetter sample-path 0 0) → should return S (getLetter sample-path 1 0) →should return E (getLetter sample-path 1 1) → should return - (getLetter sample-path 4 4) → should return F Task 3: Define a function "solve Path" which takes a path and returns the solution for the path. (solve Path sample-path) →should return (D DRRRRD D)