Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Advanced level school Python programming. Need helps. Write the function generate_points which takes a positive integer n as input and return n randomly generated cartesian
Advanced level school Python programming. Need helps.
Write the function generate_points which takes a positive integer n as input and return n randomly generated cartesian points (x,y) in a list of lists. The function must ensure that the number of points randomly generated are evenly distributed in the following 4 quadrants. You can assume that the positive integer n is always divisible by 4. A. x ranges value from 150 and y ranges from 150 B. x ranges value from 51 to 100 and y ranges from 150 C. x ranges value from 150 and y ranges from 51 to 100 D. x ranges value from 51 to 100 and y ranges from 51 to 100 For example, >>> generate_points (8) > [[70,23],[22,58],[27,71],[100,74],[26,6],[58, 45],[66,95],[26,13]] Take note that: - [26,6] and [26,13] are in quadrant A - [70,23] and [58,45] are in quadrant B - [22,58] and [27,71] are in quadrant C - [100,74] and [66,95] are in quadrant D Task 1.2 Write the function test_1_1 which takes a list of points of the same output format in Task 1.1 as input and returns True if the points are evenly distributed in the 4 quadrants and False otherwise. Write the function euclidean_distance which takes 2 points and returns the integer value of the Euclidean distance between the 2 points (discards all decimal points). The Euclidean distance, d, is calculated using the formula below. d=(x2x1)2+(y2y1)2 For example: euclidean_distance ([1,2],[4,6]) >5 Task 1.4 Write the function distance_matrix which takes a list of points of the same output format in Task 1.1 as input and returns a Euclidean distance matrix of these points. A Euclidean distance matrix is a nxn matrix that contains the distances between all pairs of points in a given set. The distance between two points is calculated using the Euclidean distance formula. For this Euclidean distance matrix, you only need to store the integer value of the Euclidean distances. For example: >distancematrix([52,67],[89,20],[38,79],[9,13]])>[0,59,18,69],[59,0,77,80],[18,77,0,72],[69,80,72,0]] Take note that the size of the input list of points can vary. The table below explains Euclidean distance matrix using the example above. In the distance matrix above, the Euclidean distances from [52,67] to [89,20] and from [89,20] to [52,67] are the same, both have a value of 59 . This explains why the distance matrix is a reflection along the diagonal line. Moreover, the distance between the same point is 0 . This explains the 0s in the distance matrix. Write the function calculate_total_distance which takes a tuple of point indices path and a distance matrix d_- matrix and as inputs and returns the total distance needed to travel through the path. Using the same distance matrix in the previous example. d_matrix =[[0,59,18,69],[59,0,77,80],[18,77,0, 72],[69,80,72,0]] path =(1,0,2,3) > calculate_cost(path, d_matrix) 229 The following explains how 229 is calculated. A path is made up of a tuple of indices which determines how the calculation of the total distances is done. The path makes a loop which means that it includes the subpath from the ending point to the starting point. For example, path =(1,0,2,3) The above means that path is made up of following sub-paths in the same order: - from point 1 to point 0 - from point 0 to point 2 - from point 2 to point 3 - from point 3 to point 1 (go back to starting point) Using the distance matrix d_matrix illustrated below by replacing the actual x and y values of a point with indices 0,1,2 and 3. The distance from point 1 to point 0 is 59 . The distance from point 0 to point 2 is 18 . The distance from point 2 to point 3 is 72 . The distance from point 3 to point 1 is 80 . Hence, the total distance is 59+18+72+80=229. Write the function insertionsort which takes a list of paths, paths, and a distance matrix, d_matrix, as inputs. It sorts the paths in paths in ascending order based on the total distance covered in each path in paths. You must use insertion sort to perform this function. For example, paths =[(1,2,0,3),(0,2,1,3),(1,0,2,3),(3,2, 1,0)] >> insertionsort(paths, d_matrix) >>> paths [(1,0,2,3),(1,2,0,3),(0,2,1,3),(3,2,1,0)] In the example above, the total distance covered for each path is as follows: Take note that the size of each path and d_matrix can vary
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