Question

Infinite Spiral of Numbers (due 17 Feb 2020) HELLO, WE ARE USING PYTHON 3 TO COMPLETE...

Infinite Spiral of Numbers (due 17 Feb 2020)

HELLO, WE ARE USING PYTHON 3 TO COMPLETE THIS PROJECT!! PLEASE FOLLOW CODE SKELETON AS GIVEN AT THE END. THIS IS DUE 17TH FEB 2020, ANY AND ALL HELP WOULD BE GREATLY APPRECIATED, THANK YOU!

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.

111 112 113 114 115 116 117 118 119 120 121
110 73 74 75 76 77 78 79 80 81 82
109 72 43 44 45 46 47 48 49 50 83
108 71 42 21 22 23 24 25 26 51 84
107 70 41 20 7 8 9 10 27 52 85
106 69 40 19 6 1 2 11 28 53 86
105 68 39 18 5 4 3 12 29 54 87
104 67 38 17 16 15 14 13 30 55 88
103 66 37 36 35 34 33 32 31 56 89
102 65 64 63 62 61 60 59 58 57 90
101 100 99 98 97 96 95 94 93 92 91

This spiral has several interesting features. The southeast diagonal has several prime numbers (3, 13, 31, 57, 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.

You will prompt the user for the following information:

Enter dimension: 11
Enter number in spiral: 42

The first line 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, print an error message Number not in Range and exit the program.

You will write the sub-grid surrounding the second number. The sub-grid is a 2-D list of size (2 x 2), (2, 3), (3, 2) or (3, 3). When you print the 2-D list each row must be on a line by itself and there must be a single white space separating each number. You do not have align the numbers.

All outputs shown are for the 11 x 11 grid. If the second number was 42, then this should be your output:

72 43 44
71 42 21
70 41 20

If the second number was 64, then this should be your output:

66 37 36
65 64 63
100 99 98

If the second number was 94, then this should be your output:

60 59 58
95 94 93

If the second number was 104, then this should be your output:

105 68
104 67
103 66

If the second number was 121, then this should be your output:

120 121
81 82

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 both of you will submit the same code. In the header make sure that you have your name and your partner's name. If you are working alone then remove the fields that has the partner's name and EID.

The file that you will be turning in will be called Spiral.py. You will follow the standard coding conventions that we have discussed. Your file will have the following template:

#  File: Spiral.py

#  Description:

#  Student's Name:

#  Student's UT EID:

#  Partner's Name:

#  Partner's UT EID:

#  Course Name: CS 313E

#  Unique Number: 

#  Date Created:

#  Date Last Modified:


#  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 withing the range of numbers in
#         the grid
#  Output: sub-grid surrounding the parameter val in the grid
#          sub-grid could be 1-D or 2-D list
def sub_grid (grid, val):

def main:
  # prompt user to enter dimension of grid

  # prompt user to enter value in grid

  # print subgrid surrounding the value

if __name__ == "__main__":
  main()

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#  File: Spiral.py

#  Description:

#  Student's Name:

#  Student's UT EID:

#  Partner's Name:

#  Partner's UT EID:

#  Course Name: CS 313E

#  Unique Number:

#  Date Created:

#  Date Last Modified:

#  Input: dim and coordinates of given number

#  Output: function returns a list of integers with top left coordinate and

#          number ofrows and columns of subgrid

def get_vals(dim,x,y):

  # down right corner

  if x==dim-1 and y==dim-1:

    return list([x-1,y-1,2,2])

  # down left corner

  if x==dim-1 and y==0 :

    return list([x-1,y,2,2])

  # top left corner

  if x==0 and y==0:

    return list([x,y,2,2])

  # top right corner

  if x==0 and y==dim-1:

    return list([x,y-1,2,2])

  # top edge

  if x==0:

    return list([x,y-1,2,3])

  #bottom edge

  if x==dim-1:

    return list([x-1,y-1,2,3])

  # left edge

  if y==0:

    return list([x-1,y,3,2])

  # right edge

  if y==dim-1:

    return list([x-1,y-1,3,2])

  # internal elements

  return list([x-1,y-1,3,3])



#  Input: dim is a positive odd integer

#  Output: function returns a 2-D list of integers arranged

#          in a spiral

def create_spiral(dim):

    if dim%2 == 1:

      dim = dim+1

    N, S, W, E = (0, -1), (0, 1), (-1, 0), (1, 0) # directions

    turn_right = {N: E, E: S, S: W, W: N} # old -> new direction

    x, y = dim // 2, dim // 2 # start near the center

    dx, dy = NORTH # initial direction

    mat = [[None] * dim for _ in range(dim)]

    count = 0

    while True:

        count += 1

        mat[y][x] = count # visit

        # try to turn right

        new_dx, new_dy = turn_right[dx,dy]

        new_x, new_y = x + new_dx, y + new_dy

        if (0 <= new_x < dim and 0 <= new_y < dim and

            mat[new_y][new_x] is None): # can turn right

            x, y = new_x, new_y

            dx, dy = new_dx, new_dy

        else: # try to move straight

            x, y = x + dx, y + dy

            if not (0 <= x < dim and 0 <= y < dim):

                return mat # nowhere to go

#  Input: grid a 2-D list containing a spiral of numbers

#         val is a number withing the range of numbers in

#         the grid

#  Output: sub-grid surrounding the parameter val in the grid

#          sub-grid could be 1-D or 2-D list

def sub_grid(grid,val):

  dim = len(grid)

  nx=0

  ny=0

  for i in range(dim):

    for j in range(dim):

      if grid[i][j] == val:

        nx = i

        ny = j

  vals = get_vals(dim,nx,ny)

  for i in range(vals[2]):

    for j in range(vals[3]):

      print(grid[i+vals[0]][j+vals[1]],end=" ")

    print("")

def main:

  # prompt user to enter dimension of grid

  dim = int(input("Enter dimensions of the grid : "))

  # prompt user to enter value in grid

  val = int(input("Enter value in grid : "))

  # print subgrid surrounding the value

  create_spiral(dim)

  sub_grid(grid,val)

if __name__ == "__main__":

  main()

#If you got the answer, please upvote :)

Add a comment
Know the answer?
Add Answer to:
Infinite Spiral of Numbers (due 17 Feb 2020) HELLO, WE ARE USING PYTHON 3 TO COMPLETE...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Python: Using your favorite text editor, write a program that outputs the string representation of numbers...

    Python: Using your favorite text editor, write a program that outputs the string representation of numbers from 1 to n. But for multiples of three it should output “Fizz” instead of the number and for the multiples of five output “Buzz”. For numbers which are multiples of both three and five output “FizzBuzz”. Submit by uploading a .py file Example: n = 15, Return: [ "1", "2", "Fizz", "4", "Buzz", "Fizz", "7", "8", "Fizz", "Buzz", "11", "Fizz", "13", "14", "FizzBuzz"...

  • Using Python, Can someone please assist in the following: These are the hints: Summary This week's lab is to create a simple multiplication table using nested loops and if statements. Prompt the...

    Using Python, Can someone please assist in the following: These are the hints: Summary This week's lab is to create a simple multiplication table using nested loops and if statements. Prompt the user for the size of the multiplication table (from 2x2 to 10x10). Use a validation loop to display a warning if the number is less than 2 or greater than 10 and prompt the user to enter the data again until they enter a valid number Put a...

  • In Python !!! In this exercise you will continue to work with Classes and will build...

    In Python !!! In this exercise you will continue to work with Classes and will build on the Book class in Lab 13.10; you should copy the code you created there as you will need to extend it in this exercise. You will extend the Book class to accommodate the case where there may be multiple authors for a book. The attribute author should become a list. The constructor will still take a parameter that is a string for author,...

  • 23.4 Project 4: Using Pandas for data analysis and practice with error handling Python Please! 23.4...

    23.4 Project 4: Using Pandas for data analysis and practice with error handling Python Please! 23.4 PROJECT 4: Using Pandas for data analysis and practice with error handling Overview In this project, you will use the Pandas module to analyze some data about some 20th century car models, country of origin, miles per gallon, model year, etc. Provided Input Files An input file with nearly 200 rows of data about automobiles. The input file has the following format (the same...

  • Need some help I am not understanding this programming class at all. We are using Microsoft...

    Need some help I am not understanding this programming class at all. We are using Microsoft visual studio with python in console mode to complete these labs. Double-click to hide white space CIS115 Week 4 Lab Overview Title of Lab: Multiplication Table in Python Summary This week's lab is to create a simple multiplication table using nested loops and if statements. Prompt the user for the size of the multiplication table (from 2x2 to 10x10). Use a validation loop to...

  • Could anyone help add to my python code? I now need to calculate the mean and...

    Could anyone help add to my python code? I now need to calculate the mean and median. In this programming assignment you are to extend the program you wrote for Number Stats to determine the median and mode of the numbers read from the file. You are to create a program called numstat2.py that reads a series of integer numbers from a file and determines and displays the following: The name of the file. The sum of the numbers. The...

  • 1. Write a program in Assembly language using MIPS instruction set that reads two integer numbers...

    1. Write a program in Assembly language using MIPS instruction set that reads two integer numbers from the user named as start and end number and finds out all the prime numbers between start and end (including start and end). Your program should do the validation of both the numbers as follows: i. start number must be smaller or equal to the end number. ii. Both numbers must be positive. iii. The maximum value for the end number is 10000...

  • Choose 3 of 5 Problems to Solve Need Soon as possible Choose 3 of5 Problems to...

    Choose 3 of 5 Problems to Solve Need Soon as possible Choose 3 of5 Problems to Solve (20 Point/Each) Problem l: Triangle Printing Program: Write an application that prints a right triangle with 1 to 8 rows. Prompt the user to enter an odd number in the range of 1 to 8 to specify the number of rows in the diamond. Add a validation loop to ensure the user does not enter an even number or a number outside the...

  • python 2..fundamentals of python 1.Package Newton’s method for approximating square roots (Case Study 3.6) in a...

    python 2..fundamentals of python 1.Package Newton’s method for approximating square roots (Case Study 3.6) in a function named newton. This function expects the input number as an argument and returns the estimate of its square root. The script should also include a main function that allows the user to compute square roots of inputs until she presses the enter/return key. 2.Convert Newton’s method for approximating square roots in Project 1 to a recursive function named newton. (Hint: The estimate of...

  • Python, given a code in class i just need help with the third bullet point ; using a and n (defin...

    Python, given a code in class i just need help with the third bullet point ; using a and n (defined in the second picture of python code) find the first digit for k! for k =1,...,n. you dont have to type in all this code just help me write the code in the first picture where it says: def benford(a): b = [0 for d in range(1,10)] #Do everthything in here return b 2.2 Generating data In this assignment...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT