在给定范围内分配空白。

huangapple go评论60阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年5月11日 02:42:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76221678.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定