将数字根据等级分成块

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

Split number into chunks according to rank

问题

一个非常实际的任务。我有许多需要根据商店排名分发的物品。因此,排名越高,商店将获得更多物品,排名为1的商店将获得最多物品,排名为100的商店将获得最少物品。

所以输入是:

  • 需要分发的物品数量
  • 商店,用它们的排名表示,也可以有重复,不遵循任何模式,例如[1, 4, 40]

我现在拥有以下内容:

  • 我反转排名,使较小的数字变为最大的(sum(n...) - n)
  • 我获取每个商店的百分比:排名倒数 \ sum(倒数排名)
  • 将需要分发的数量乘以百分比

它在一些数字上运行良好,比如:

[1, 4, 40]
1 = 147.2
4 = 137.1
40 = 16.7

显然,排名为40的商店获得最少,但随着更多商店的加入,情况变得更平坦:

[6,3,24,10, 25, 12, 14,35,40, 16,28,29,17,1,26,23,8,10]
1 = 17.7
3 = 17.5
6 = 17.4
8 = 17.3
10 = 17.2
10 = 17.2
12 = 17.1
14 = 16.9
16 = 16.8
17 = 16.8
23 = 16.5
24 = 16.4
25 = 16.4
26 = 16.3
28 = 16.2
29 = 16.1
35 = 15.8
40 = 15.5

因此,排名为40的商店仅获得略少于最高排名的商店。

有没有人有办法使它更加“曲线”?

我的Python代码:

number = 301

ranks = [6,3,24,10, 25, 12, 14,35,40, 16,28,29,17,1,26,23,8,10]
#ranks = [1,4,40]

ranks.sort()

total = sum(ranks)
tmp = 0

reversed = []

for value in ranks:
  reversed.append(total - value)

total = sum(reversed)
print('Items per rank:')
for i in range(len(ranks)):
    percent = reversed[i] / total
    print(str(ranks[i]) + ' = ' + "{:10.1f}".format(percent * number))
    tmp = tmp + percent * number

print('')
print('Total = ' + str(tmp))

您可以在这里进行尝试:https://trinket.io/python/a15c54b978

最好使用更多数学而不是库的解决方案,因为我需要将其转化为Excel VBA。

解决方案还添加了一个控制分配的因子:

number = 301

ranks = [6,3,24,10, 25, 12, 14,35,40, 16,28,29,17,1,26,23,8,10]
#ranks = [1,4,40]
factor = 1

ranks.sort()

total = sum(ranks)
tmp = 0
max = max(ranks) + 1

reversed = []

for value in ranks:
  reversed_value = max - value
  reversed_value = pow(reversed_value, factor) 
  reversed.append(reversed_value)

total = sum(reversed)
print('Items per rank:')
for i in range(len(ranks)):
    percent = reversed[i] / total 
    value = percent * number

    print(str(ranks[i]) + ' = ' + "{:10.1f}".format(value))
    tmp = tmp + value

print('')
print('Total = ' + str(tmp))

希望这对您有所帮助。

英文:

A very practical task. I have a number of items that need to be distributed into several stores, according to store rank. So the higher the rank, the more items store will get, so store with rank 1 will get most items and store with rank 100 least.
So the inputs are:

  • number of items to distribute
  • stores, represented by their rank, can have duplicates as well, and does not follow any pattern, for instance [1, 4, 40]

What I have right now is the following:

  • I inverse the ranks, so that smaller number would become biggest (sum(n...) - n)
  • I take percentage of each store: inversed_rank \ sum(inversed_ranks)
  • Multiply the amount I need to distribute by the percentage

It works fine on some of the numbers, like:

[1, 4, 40]
1 = 147.2     
4 = 137.1     
40 = 16.7  

Clearly the store with rank 40 gets the least, but with more stores it flattens:

[6,3,24,10, 25, 12, 14,35,40, 16,28,29,17,1,26,23,8,10]
1 = 17.7      
3 = 17.5      
6 = 17.4      
8 = 17.3      
10 = 17.2      
10 = 17.2      
12 = 17.1      
14 = 16.9      
16 = 16.8      
17 = 16.8      
23 = 16.5      
24 = 16.4      
25 = 16.4      
26 = 16.3      
28 = 16.2      
29 = 16.1      
35 = 15.8      
40 = 15.5 

So store with rank 40 gets only a fraction less than the top.
Does anyone have an idea how to make it a bit more "curved"?

My Python code:

number = 301

ranks = [6,3,24,10, 25, 12, 14,35,40, 16,28,29,17,1,26,23,8,10]
#ranks = [1,4,40]

ranks.sort()

total = sum(ranks)
tmp = 0

reversed = []

for value in ranks:
  reversed.append(total - value)

total = sum(reversed)
print('Items per rank:')
for i in range(len(ranks)):
    percent = reversed[i] / total
    print(str(ranks[i]) + ' = ' + "{:10.1f}".format(percent * number))
    tmp = tmp + percent * number

print('')
print('Total = ' + str(tmp))

You may play with it here: https://trinket.io/python/a15c54b978
Would be perfect to have a more math rather than library solution as I will need to translate it into Excel VBA

Solution
Í have also added a factor to control the distribution:

number = 301

ranks = [6,3,24,10, 25, 12, 14,35,40, 16,28,29,17,1,26,23,8,10]
#ranks = [1,4,40]
factor = 1

ranks.sort()

total = sum(ranks)
tmp = 0
max = max(ranks) + 1

reversed = []

for value in ranks:
  reversed_value = max - value
  reversed_value = pow(reversed_value, factor) 
  reversed.append(reversed_value)

total = sum(reversed)
print('Items per rank:')
for i in range(len(ranks)):
    percent = reversed[i] / total 
    value = percent * number

    print(str(ranks[i]) + ' = ' + "{:10.1f}".format(value))
    tmp = tmp + value


print('')
print('Total = ' + str(tmp))

答案1

得分: 1

这个工作吗?

rank= [6,3,24,10, 25, 12, 14,35,40, 16,28,29,17,1,26,23,8,10]
rank_= [6,3,24,10, 25, 12, 14,35,40, 16,28,29,17,1,26,23,8,10]

number = 301

# 假设距离最远的点获得值为0
for i, p in enumerate(rank):
    rank[i] = p*-1 + max(rank) + 1

sum = sum(rank)
for i, p in enumerate(rank):
    val = p / sum
    val *= number 
    rank[i] = val

for i in range(len(rank)):
    print(rank_[i], ":", rank[i], end="\n\n")

output:

6: 26.806615776081426

3: 29.104325699745548

24: 13.020356234096692

10: 23.74300254452926

25: 12.254452926208652

12: 22.211195928753177

14: 20.6793893129771

35: 4.595419847328244

40: 0.7659033078880407

16: 17.615776081424936

28: 8.424936386768447

29: 7.659033078880407

17: 16.849872773536894

1: 29.104325699745548

26: 9.956743002544528

23: 12.254452926208652

8: 23.74300254452926

10: 22.211195928753177

英文:

Does this work ?

rank= [6,3,24,10, 25, 12, 14,35,40, 16,28,29,17,1,26,23,8,10]
rank_= [6,3,24,10, 25, 12, 14,35,40, 16,28,29,17,1,26,23,8,10]

number = 301

# assuming the point that is the most distant gets the value 0
for i, p in enumerate(rank):
    rank[i] = p*-1 + max(rank) + 1

sum = sum(rank)
for i, p in enumerate(rank):
    val = p / sum
    val *= number 
    rank[i] = val

for i in range(len(rank)):
    print(rank_[i], ": ", rank[i], end="\n\n")

output:

> 6: 26.806615776081426
>
> 3: 29.104325699745548
>
> 24: 13.020356234096692
>
> 10: 23.74300254452926
>
> 25: 12.254452926208652
>
> 12: 22.211195928753177
>
> 14: 20.6793893129771
>
> 35: 4.595419847328244
>
> 40: 0.7659033078880407
>
> 16: 17.615776081424936
>
> 28: 8.424936386768447
>
> 29: 7.659033078880407
>
> 17: 16.849872773536894
>
> 1: 29.104325699745548
>
> 26: 9.956743002544528
>
> 23: 12.254452926208652
>
> 8: 23.74300254452926
>
> 10: 22.211195928753177

huangapple
  • 本文由 发表于 2023年2月14日 00:17:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/75438514.html
匿名

发表评论

匿名网友

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

确定