Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Task 3: Complete the following function template: function PERMUTeRows(puzzle, x, y, z) end function This function should take a four-element vector called puzzle, which will
Task 3: Complete the following function template: function PERMUTeRows(puzzle, x, y, z) end function This function should take a four-element vector called puzzle, which will be of the form of the output of MAKEVEC- TOR as an input parameter, as well as three integers x, y and z. The function will return puzzle but with elements puzzle[1], puzzle[2] and puzzle(3) cyclically permuted by x, y and z elements respectively to the left: x,y and z should all be numbers between 0 and 3 (inclusive). To be able to get full marks you should call the function PERMUTEVECTOR appropriately. HINT: You do not need to loop over integers x, y and z. [3 marks] Checking the Pseudoku conditions The next step in constructing the algorithm is to write methods to decide if the Pseudoku conditions are satisfied. If we start with the output of the function MAKEVECTOR(row). then all of the row conditions are satisfied as long as row is a four-element vector with the numbers 1 to 4 only appearing once. However, the column conditions are not satisfied: only one number appears in each column (four times). The 2-by-2 sub-grid conditions are also not satisfied: in each sub-grid only two numbers appear (twice). We need a convenient way to refer to elements of the two-dimensional puzzle. We will use a coordinate sys- tem of (row.col) for the four-element vector puzzle as produced by MAKEVECTOR: row is the number of the element of puzzle that we care about, and col is the number of the element in puzzle[row] that we care about. Consider the following vector: Bonet a 1 8 4 + 2 1 E 1 3 The coordinates of the element in yellow are (3,2), for example. So to select the value stored there we use the syntax puzzle[3][2), since we are looking at the second element in puzzle[3]. Using this coordinate system to re- fer to the 2-by-2 sub-grids, for example, the top-left sub-grid will consist of the elements (1.1) (1,2), (2.1) and (2.2). You will check the Pseudoku conditions using a stack. In particular, you will start with a stack containing all numbers from 1 to 4, and then inspect each element in each column (or sub-grid) to see if the number in that element is stored in the stack: if it is stored in the stack, pop that value from the stack and return the stack; otherwise, return false. This process will be repeated for every element in a column or sub-grid. If, at the end of checking every element, the stack is empty then all numbers appear in the the column or sub-grid; if the stack is not empty, then not all numbers from 1 to 4 appear. We will complete a function called SEARCH STACK(stack, item) that will search a stack for the value item: if item is in one of the elements of the stack, we remove that element storing item. Otherwise, the function should return FALSE Let's demonstrate this with diagram. We have the following stack: 1 2 Top We want to look for the value 3 in the stack and remove it if it is there, otherwise if it is not there, retum FALSE. The value 3 is stored in the stack so we then need to remove it so the stack could look like this: 1 2 4 1 Top The next task will be to complete a function that does this process of searching a stack and possibly removing an element
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started