Length of Python overlapping interval (center point, length)

Asked 1 years ago, Updated 1 years ago, 284 views

Hi, everyone. There are two line segments A and B above the vertical line. Each segment can be represented by two numbers: its center position and its length. At this time, write a program to find the length of the overlapping section between A and B according to the instructions. Each line segment is expressed as (center point, length) as follows. That's the problem

//python
a = input().split(',')
b = input().split(',')

midA = int(a[0])
maxA = int(a[1])
minA = midA - (maxA-midA)


midB = int(b[0])
maxB = int(b[1])
minB = midB - (maxB-midB)

n = []

if minA <= maxB and maxA >= minB :
    n += minA, maxB, maxA, minB
    n.sort()
    print(n[2]-n[1])
else:
    print("0")

I solved it like this, and I think the answer is coming out right. Is it possible to solve it in another way?

python

2022-10-28 00:01

1 Answers

If the distance from the center of A to the center of B * 2 is less than the length of A + the length of B, the lines will likely overlap.


2022-10-28 00:01

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.