sum value of a 3rd row and divide rows in such a way that their sum values matches

风格不统一 提交于 2020-08-10 19:29:45

问题


I have a file as below with n number of rows, I want to total it's sum(based on 3rd column) and distribute rows accordingly in 3 different files(based on sum of each)

For example- if we sum all the 3rd column values it's total is coming as 516 and if we divide it by 3 it is 172.

So i want to add a rows to a file so it doesn't exceed 172 mark, same with the 2nd file and rest all rows should move to the third file.

  • Just have to make sure sum value of all the 3 files should match(small difference is fine)
    • may be it should start checking the values from the top and keep on searching till the end and try to add as much it can(just have to make sure total value doesn't exceed the 172 mark(which is one third of the total sum))
    • It should avoid all the rows which has third value as 0 and at last distribute all of them amongst all the 3 files in such a way that their row count should also be somewhere near equal.

This is in addition to question-"sum value of a 3rd row and divide rows accordingly"

Input file

a aa 0
b ab 55
c ac 17
a dy 0
y ae 12
a dl 34
a fk 45
l ah 56
o aj 76 
l ai 19 
q al 0
d pl 64
e ik 0
f ll 0
g dl 25 
h fr 17
i dd 23
j we 27
k rt 25
l yt 0
m tt 19

expected output

file1 Total (172)

b ab 55
c ac 17
y ae 12
a dl 34
a fk 45
m tt 9

file2 Total (168)

l ah 56
o aj 76 
l ai 19 
h fr 17

file3 Total (174)

d pl 64
g dl 25
i dd 23
j we 27
k rt 35

Later it should check for all the 0 column and divide it in such a way that total rows of each file can also be balanced a bit.

a aa 0
a dy 0
q al 0
e ik 0
f ll 0
l yt 0

so the final output would be

file1

b ab 55
c ac 17
y ae 12
a dl 34
a fk 45
m tt 9
a aa 0

file2

l ah 56
o aj 76 
l ai 19 
h fr 17
a dy 0
q al 0
e ik 0

file3

d pl 64
g dl 25
i dd 23
j we 27
k rt 35
f ll 0
l yt 0

OP's attempts from comments:

awk '{ L[nr++]=$0; sum+=$3 } END{ sumpf=sum/3; sum=0; file=1; for(i in L) { split(L[i],a); if ((sum+a[3])>sumpf && file<3) { file+=1; sum=0; }; print i, L[i] > "file" file; sum+=a[3]; } }' input


回答1:


There are a couple of scenario's you can follow:

Scenario 1)

  1. Read values and add them to file1, as long as, total is below required total

  2. If Value of file1 is near required total, add values to file2, until a required total

  3. If value of file2 is near required total start adding the rest to file3.

This will get you a result like:

With totals of respectively for file1, file2 and file 3: 163, 153, 200

This might not satisfy the needs, so,

Scenario 2)

  1. What is we first order the items descending, and then follow Scenario 1 ?

We end up with:

Wow, this is even less satisfying! I will not even count the totals!

Now it's up to the OP TrueEntertainer to think of another scenario, where the three tables are filled more evenly.

It should really not be that hard to find a better way than the above 2 scenario's !!



来源:https://stackoverflow.com/questions/62441945/sum-value-of-a-3rd-row-and-divide-rows-in-such-a-way-that-their-sum-values-match

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!