英文:
Whitespace allocation in between a given range
问题
I have a range:
start = 87.5
end = 107.5
In between start and end, I have allocations that look like this:
If visualized, it looks like this:
Now I have another input as X = 0.3.
0.3 is actually a value that can be allocated inside the range.
My question is how do I solve this in DataFrame-Pandas and have the most optimal solution.
By optimal solution, I mean the new allocation doesn't create more whitespaces hence decreasing the continuity. In short, I want to increase the congestion in the range so I can place more X's in the range.
If default allocations are:
lower upper
87.85 88.15
89.75 90.05
91.45 91.75
96.05 96.35
96.85 97.15
98.85 99.15
100.85 101.15
101.9 102.1
102.9 103.1
105.35 105.65
107.81 107.99
Then one of the many solutions can be (considering X = 0.3):
lower upper
87.85 88.15
88.55 88.85
89.75 90.05
91.45 91.75
96.05 96.35
96.85 97.15
98.85 99.15
100.85 101.15
101.9 102.1
102.9 103.1
105.35 105.65
107.81 107.99
英文:
i have a range:
start = 87.5
end = 107.5
in between start and end i have allocations that look like this
if visualized it looks like this:
now i have another input as X = 0.3
0.3 is actually a value that can be allocated inside the range.
my question is how do i solve this in dataframe-pandas and have the most optimal solution.
by optimal solution i mean the new allocation doesn't create more whitespaces hence decreasing the continuity.
inshort i want to increase the congestion in the range so i can place more X's in the range.
if default allocations are:
lower upper
87.85 88.15
89.75 90.05
91.45 91.75
96.05 96.35
96.85 97.15
98.85 99.15
100.85 101.15
101.9 102.1
102.9 103.1
105.35 105.65
107.81 107.99
then one of the many solutions can be(considering x= 0.3:
lower upper
87.85 88.15
88.55 88.85
89.75 90.05
91.45 91.75
96.05 96.35
96.85 97.15
98.85 99.15
100.85 101.15
101.9 102.1
102.9 103.1
105.35 105.65
107.81 107.99
答案1
得分: 1
Here's the translated code part:
IIUC,您可以计算空范围的大小,找到大于您目标的最小范围:
X = 0.3
diff = df['lower'].sub(df['upper'].shift()).sort_values()
n = np.searchsorted(diff, X)
idx = diff.index[n]
a = df.loc[idx-1, "upper"]
b = df.loc[idx, "lower"]
delta = (b-a)-X//3
print(f'insert {X} between {a} and {b}')
print(f'potential range: {a+delta}-{b-delta}')
输出:
insert 0.3 between 96.35 and 96.85
potential range: 95.05-92.75
英文:
IIUC, you can compute the size of the empty ranges, find the smallest that is larger than your target:
X = 0.3
diff = df['lower'].sub(df['upper'].shift()).sort_values()
n = np.searchsorted(diff, X)
idx = diff.index[n]
a = df.loc[idx-1, "upper"]
b = df.loc[idx, "lower"]
delta = (b-a)-X//3
print(f'insert {X} between {a} and {b}')
print(f'potential range: {a+delta}-{b-delta}')
Output:
insert 0.3 between 96.35 and 96.85
potential range: 95.05-92.75
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论