Infinite Spiral of Numbers (due 12 Jun 2020)

Consider the natural numbers laid out in a square spiral, with 1 occupying the center of the spiral. The central 11 x 11 subset of that spiral is shown in the table below.

111112113114115 116117118119120121
11073747576 777879808182
10972434445 464748495083
10871422122 232425265184
1077041207 8910275285
1066940196 1211285386
1056839185 4312295487
10467381716 151413305588
10366373635 343332315689
10265646362 616059585790
101100999897 969594939291

This spiral has several interesting features. The southeast diagonal has several prime numbers (3, 13, 31, and 91) along it. The southwest diagonal has a weaker concentration of prime numbers (5, 17, 37) along it.

To construct the spiral we start with 1 at the center, with 2 to the right, and 3 below it, 4 to the left, and so on. A part of the problem for this assignment is to figure out the rule to fill the spiral for an arbirary size. Once you have that rule you can complete the rest of the assignment.

INPUT: You will read your input from a file called spiral.in. The file has a single line with two integers separated by a space.

11 42
The first number indicates the dimension of the square spiral. This number should be an odd number. If it is not then choose the dimension to be the next higher odd number. The second number must be in the range 1 and the square of the dimension. If the second number is not in that range you may write -1 in the output file and exit the program.

OUTPUT: You will write your output in a file called spiral.out. It will be a single number. For example, for the above input file, the numbers surrounding 42 is:

72 43 44
71 42 21
70 41 20
The sum of those numbers is 424. So the output file will have:
424
If the other hand, the input values were
11 94
Then the numbers surrounding 94 are
60 59 58
95 94 93
The output that you write will be 459.

For this assignment you may work with a partner. Both of you must read the paper on Pair Programming and abide by the ground rules as stated in that paper. If you are working with a partner then only one of you will submit the code. Make sure that in the header in HackerRank that you have your name and UT EID and your partner's name and UT EID. If you are working alone then you will just have your name and your UT EID.

The file (Spiral.py) that you will be submitting will have the following structure. You may NOT change the names of the functions but you may add as many helper functions as needed. You will follow the standard coding conventions in Python.


#  Input: dim is a positive odd integer
#  Output: function returns a 2-D list of integers arranged
#          in a spiral
def create_spiral (dim):

#  Input: grid a 2-D list containing a spiral of numbers
#         val is a number within the range of numbers in
#         the grid
#  Output: sum_sub_grid returns the sum of the numbers (including val)
#          surrounding the parameter val in the grid 
#          if val is out of bounds, returns -1
def sum_sub_grid (grid, val):

def main():
  # read the dimension of the grid and value from input file

  # test that dimension is odd

  # create a 2-D list representing the spiral

  # find sum of adjacent terms

  # write the sum in the output file

if __name__ == "__main__":
  main()

Use the HackerRank platform to submit your code. We should receive your work by 11 PM on Friday, 12 Jun 2020. There will be substantial penalties if you do not adhere to the guidelines. HackerRank will not assign late penalties (if any), we will make the adjustments.