Search the whole station

# 数学应用编程代写 Math 124代写 数学编程代写 数学作业代写

290

## Project 1 – The Trapped Knight

### Description

In this project, you will write a computer code to generate a particular sequence of numbers described in the following YouTube video: The Trapped Knight (https://www.youtube.com/watch?v=RGQe8waGJ4w)

Begin by watching the video and make sure you understand exactly how the sequence is generated. Then continue to implement the code in the 3 parts described below.

### Part 1 – Initialize the board数学应用编程代写

We will store the chess board in a 2d-array of integers. The size of the board is -by- , for a given integer . This means the board extends from the center square by steps in all directions.

The first step is to initialize the board by filling it with the integers described in the video. Finish the implementation of the function definition in the cell below such that it returns this “spiral pattern” for any given input parameter .

An example is given below: for the following input

`board = initialize_board(3)`

the correct output is

`7×7 Matrix{Int64}: 37 36 35 34 33 32 31 38 17 16 15 14 13 30 39 18 5 4 3 12 29 40 19 6 1 2 11 28 41 20 7 8 9 10 27 42 21 22 23 24 25 26 43 44 45 46 47 48 49`

Test your function for various values of to make sure it is correct before you continue.

Hints:

Note that since Julia uses 1-based indexing, the center square of the array board is given by element

board[n+1,n+1] .

After the center has been placed, there are exactly “circles” of numbers of increasing radius. This is naturally implemented using a for-loop.

In each “circle”, there are 4 segments going up, left, down, and right. These are also naturally implemented using a sequence of 4 for-loops.

In [1]:

`# Initializes the board on a [-n:n]x[-n:n] domain with spiral numbers## Example: initialize_board(2) returns# 17 16 15 14 13# 18 5 4 3 12# 19 6 1 2 11# 20 7 8 9 10# 21 22 23 24 25## Inputs:# n = integer size of board to allocate# Outputs:# board = 2n+1 x 2n+1 integer array filled with spiral numbersfunction initialize_board(n)end`

Out[1]:

initialize_board (generic function with 1 method)

### Part 2 – Simulate the walk数学应用编程代写

Next we will write the function to simulate the walk and produce the sequence. This function will take an initialized board as input, and produce a list of numbers as well as the corresponding x- and y-coordinates.

For example, the following input:

`board = initialize_board(2)display(board)seq, xs, ys = simulate_walk(board);println("Sequence = ", seq)println("x-coordinates = ", xs)println("y-coordinates = ", ys)`

should produce the following correct output:

`5×5 Matrix{Int64}: 17 16 15 14 13 18 5 4 3 12 19 6 1 2 11 20 7 8 9 10 21 22 23 24 25Sequence = [1, 10, 3, 6, 9, 4, 7, 2, 5, 8, 11, 14]x-coordinates = [0, 2, 1, -1, 1, 0, -1, 1, -1, 0, 2, 1]y-coordinates = [0, 1, -1, 0, 1, -1, 1, 0, -1, 1, 0, -2]`

Again test your code, first using small values of as shown above, which makes it easier to look at the results and find errors.

Hints:

It is convenient to create another 2d-array of booleans, indicating if a square has been visited or not.

Make sure you never allow the knight to jump outside the board. That is, the only valid positions are steps from the center square in either direction

In [2]:

`# Simulates the trapped knight walk on a pre-initialized board and returns information about knight## Inputs:# board = 2n+1 x 2n+1 integer array filled with spiral numbers# Outputs:# sequence = integer array containing the sequence of spiral numbers the knight jumped to during wa# x_path = integer array containing the x coordinates of each step of knight walk# y_path = integer array containing the y coordinates of each step of knight walkfunction simulate_walk(board)end`

Out[2]:

simulate_walk (generic function with 1 method)

### Part 3 – Generate the full sequence and plot the path数学应用编程代写

Finally, use your code to generate the full sequence ( is sufficient), output the last number, and plot the path by straight lines between all the visited x,y-coordinates.

In [ ]:

In [ ]:

The prev: The next:

### Related recommendations

• #### 线性常微分方程代写 数学函数代写 微分方程代写 数学代写

274

CHAPTER 1. LINEAR ORDINARY DIFFERENTIAL EQUATIONS 线性常微分方程代写 2.Suppose that the data in Table 1.3 gave the absolute difference as a constant instead of the relative change. The recursio...

View details
• #### 数学概率作业代写 数学概率代写 数学作业代写 概率作业代写

63

4. Probability on finite sample spaces. 数学概率作业代写 1. Are the following events A, B ⊂ Ωroulette independent? Find P(A|B) and P(B|A) in each case. a) A = Red, B = Even, 1. Are the fol...

View details
• #### 数学线性代数代写 Math 541代写 线性代数代写 数学作业代写

118

Math 541 HW1 - Linear Algebra Refresher 数学线性代数代写 Remarks: A) Definition is just a definition, there is no need to justify or explain it. B) Answers to questions with proofs should b...

View details
• #### 应用数学计算代写 数学计算代写 数据分析代写 数学作业代写

373

Fall 2021 应用数学计算代写 Read these instructions carefully!!! This project involves predicting what happens to a mortgage loans that have been purchased by FNMA during the Read these instr...

View details
1