def count_overlapping_disks(disks):
Right on the heels of the previous Manhattan skyline problem, another classic problem of similar spirit that is here best solved with a sweepline algorithm. Given a list of disks on the two-dimensional plane as tuples (x, y, r) so that (x, y) is the center point and r is the radius of that disk, count how many pairs of disks intersect each other in that their areas, including the edge, have at least one point in common. To test whether two disks (x1, y1, r1) and (x2, y2, r2) intersect, use the Pythagorean formula (x2-x1)**2 + (y2-y1)**2 <= (r1+r2)**2. (Note again how this precise formula uses only integer arithmetic whenever all individual components are integers. And no square roots or some other nasty irrational numbers.)
For this problem, crudely looping through all possible pairs of disks would be horrendously inefficient as the list grows larger. However, a sweepline algorithm can solve this problem by looking at a far fewer pairs of disks. Again, sweep through the space from left to right for all relevant x-coordinate values and maintain the set of active disks at the moment. Each individual disk (x, y, r) enters the active set when the sweep line reaches the x-coordinate x-r, and leaves the active set when the sweep line reaches x+r. When a disk enters the active set, check for an intersection between that disk and only the disks presently in the active set.
disks |
Expected result |
[(0, 0, 3), (6, 0, 3), (6, 6, 3), (0, 6, 3)] |
4 |
[(4, -1, 3), (-3, 3, 2), (-3, 4, 2), (3, 1, 4)] |
2 |
[(-10, 6, 2), (6, -4, 5), (6, 3, 5), (-9, -8, 1), (1, -5, 3)] |
2 |
[(x, x, x // 2) for x in range(2, 101)] |
2563 |
# Thank you for asking the question.
def count_overlapping_disks(disks):
count=0
for i in range(len(l)-1):
for j in range(i+1,len(l)):
distSq = (l[i][0] - l[j][0]) * (l[i][0] - l[j][0]) + (l[i][1] -
l[j][1]) * (l[i][1] - l[j][1]);
radSumSq = (l[i][2] + l[j][2]) * (l[i][2] + l[j][2]);
if (distSq <= radSumSq):
count+=1
return count
# Driver code
l=[]
for a in range(2,101):
l.append((a,a,a//2))
t = count_overlapping_disks(l)
print(t) # output will be 2563
# Kindly check indentation because indentation gets change after submitting the question.
# Hope this will help you.
# Feel free to ask any doubt and once again thank you
very much.
def count_overlapping_disks(disks): Right on the heels of the previous Manhattan skyline problem, another classic problem of...
Problem 4 [25 pts] Grader & Score Consider two circular disks centered on the origin so that when viewed from the side (i.e. the negative y-axis) they appear as bars in the diagram. Each disk has an area R2 and a positive charge Q. The disks are separated by a small distance s R so that the center of the top disk is located at<0,Q,s/2 > and the center of the bottom disk is at < 0,0, -s/2 >. Point...
Consider the following problem based on the transformation of a sequence (or collection) of coloured disks Assume that you have a very large collection of disks, each with an integer value representing the disk colour from the range [0, c. For example, the colour mapping might be: O-red, 1-yellow, 2-blue, 3-pink. ..., c-black For a given sequence of coloured disks e.g., ( 0,1,2,3,4 ), at each time step (or iteration) you are only allowed to perform one of three basic...
Consider the following problem based on the transformation of a sequence (or collection) of coloured disks Assume that you have a very large collection of disks, each with an integer value representing the disk colour from the range [0, c. For example, the colour mapping might be: O-red, 1-yellow, 2-blue, 3-pink. ..., c-black For a given sequence of coloured disks e.g., ( 0,1,2,3,4 ), at each time step (or iteration) you are only allowed to perform one of three basic...
Problem 1 part II and Problem 2 part I and II Problem 1: (Short Answer) 6 pts] The region R is bounded by y 0, 0, 2, and y- 2 4 1, 3 pts] If R is revolved about the line x = 5. If an integral or sunn of integrals with respect to z is used to calculate the volume, explain whether the washer or shell method should be used II. 3 pts) Suppose that R is the base...
i need the solution in pseudo code please. 4 Dynamic Programmii Consider the following problem based on the transformation of a sequence (or collection) of coloured disks. Assume that you have a very large collection of disks, each with an integer value representing the disk colour from the range [0, cl. For example, the colour mapping might be: O-red, 1-yellow, 2-blue, 3-pink,. c-black For a given sequence of coloured disks eg.,0,1,2,3,4), at each time step (or iteration) you are only...
MATLAB only please I am trying to get my getdatafuntion to work. I am also trying to get all my x's, y's, and v's to popup in the command window so I can put in any value and it will find the value for me using the equation I put in. function project_9_sjl() % PROJECT_9_SJL project_9_sjl() is the driver function for the program. % % Name: Scott Lawrence % Date: 3/27/2019 % Class: CMPSC 200 % Description: Determine the optimal...
Given this pseudocode and problem (as an example), code the continuation algorithm in MATLAB. 1. The nonlinear system fi(x1, x2) = x - xż + 2x2 = 0, $2(x1, x2) = 2x1 + xź – 6= 0 has two solutions, (0.625204094, 2.179355825)' and (2.109511920, -1.334532188)'. Use the Con- tinuation method and Euler's method with N = 2 to approximate the solutions where a. X(0) = (0,0) b. x(0) = (1, 1) c. x(O) = (3,-2) Continuation Algorithm To approximate the...
N MATLAB: This is an easy problem to demonstrate how polyfit and polyval work! The values in y are basically a sine function on x. You can validate by checking in MATLAB >> x = 0:9 x = 0 1 2 3 4 5 6 7 8 9 >> y = sin(x) y = 0 0.8415 0.9093 0.1411 -0.7568 -0.9589 -0.2794 0.6570 0.9894 0.4121 Now, let us try to see what happens if we use polyfit on the values of...
NEED HELP !! LINEAR ALGEBRA Problem 5.3.11 142 Diagonalize the matrix A3 4 0 For this problem, we will go through the steps of computing the characteristic polynomial (by definition the characteristic polynonial is defined by det(4 followed by computing the eigenvectors. From there the diagonalization will be computed. XI)), port synpy as sp rl1,4,-2 Input the raws #Enter space or camias separated integers for your problem r2: 3,40 r1, ..1, 4, -2· e@peran {type: "string. r2-3, 4, e aparan...
2. Find the force (vector) between Q1-40uC r1 (x1-2,y1-2,z1-3) and Q2-47uCr2 (x2-3,y2-3,z2-1) A) .68i 34j -69k B) 1.25 .62j-1.26k 12 0 Fi C) 1.44i .72-145k D) 1.06i .53j-1.07k 5. When the coordinates of a system don't have components over the coordinates, we determine that they are D. Rectangular A. Orthogonal C. Inclusive 7. A vector V1 (x=4, y-6, z-8), which is the magnitude of the projection on the YZ plane A) 10 X 1 V 1 B) 8.5 C 13...