How to group numbers by predefined sum

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP












0















Data is in two columns (number and description):



1,item1
8,item2
4,item3
9,item4


The sum (using number column) is predefined 10 and input data should be ordered as close to that as possible (but not exceed it):



group1:
1,item1
9,item4

group2:
8,item2

group3:
4,item3


How to do it in shell script?










share|improve this question













migrated from unix.stackexchange.com Jan 26 at 20:35


This question came from our site for users of Linux, FreeBSD and other Un*x-like operating systems.













  • 2





    Did you do anything yourself? Do you have an algorithm? Why would two groups of 8 and 4 be closer to 10 than one group of 8 and 4?

    – Tomasz
    Jan 26 at 19:44











  • This is the bin packing problem. This is an NP-hard problem to solve (i.e. not trivial). I wrote a few lines of zsh do do it for file sizes here: "Group files into average size of the largest file", but really, it's a bit outside the scope of this site.

    – Kusalananda
    Jan 26 at 19:46











  • Atm I have not write code yet. I'm thinking smb like first sort lines by number and then loop sum til 10 is reached and group1 is complete.. then next lines. Maybe someone knows some program that can already do that. @Tomasz 8 and 4 are not closer to 10 but you can't make any more groups ie. they would exceed 10 whitch is not allowed. Smaller groups are ok.

    – Mart
    Jan 26 at 20:13











  • You should be able to take the code I linked to and modify it to fit your needs. It's currently getting filenames and file sizes, but you could change that to be your strings and numbers.

    – Kusalananda
    Jan 26 at 20:18











  • thanks @Kusalananda I'm looking it :)

    – Mart
    Jan 26 at 20:21















0















Data is in two columns (number and description):



1,item1
8,item2
4,item3
9,item4


The sum (using number column) is predefined 10 and input data should be ordered as close to that as possible (but not exceed it):



group1:
1,item1
9,item4

group2:
8,item2

group3:
4,item3


How to do it in shell script?










share|improve this question













migrated from unix.stackexchange.com Jan 26 at 20:35


This question came from our site for users of Linux, FreeBSD and other Un*x-like operating systems.













  • 2





    Did you do anything yourself? Do you have an algorithm? Why would two groups of 8 and 4 be closer to 10 than one group of 8 and 4?

    – Tomasz
    Jan 26 at 19:44











  • This is the bin packing problem. This is an NP-hard problem to solve (i.e. not trivial). I wrote a few lines of zsh do do it for file sizes here: "Group files into average size of the largest file", but really, it's a bit outside the scope of this site.

    – Kusalananda
    Jan 26 at 19:46











  • Atm I have not write code yet. I'm thinking smb like first sort lines by number and then loop sum til 10 is reached and group1 is complete.. then next lines. Maybe someone knows some program that can already do that. @Tomasz 8 and 4 are not closer to 10 but you can't make any more groups ie. they would exceed 10 whitch is not allowed. Smaller groups are ok.

    – Mart
    Jan 26 at 20:13











  • You should be able to take the code I linked to and modify it to fit your needs. It's currently getting filenames and file sizes, but you could change that to be your strings and numbers.

    – Kusalananda
    Jan 26 at 20:18











  • thanks @Kusalananda I'm looking it :)

    – Mart
    Jan 26 at 20:21













0












0








0








Data is in two columns (number and description):



1,item1
8,item2
4,item3
9,item4


The sum (using number column) is predefined 10 and input data should be ordered as close to that as possible (but not exceed it):



group1:
1,item1
9,item4

group2:
8,item2

group3:
4,item3


How to do it in shell script?










share|improve this question














Data is in two columns (number and description):



1,item1
8,item2
4,item3
9,item4


The sum (using number column) is predefined 10 and input data should be ordered as close to that as possible (but not exceed it):



group1:
1,item1
9,item4

group2:
8,item2

group3:
4,item3


How to do it in shell script?







shell scripting






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 26 at 19:33







Mart











migrated from unix.stackexchange.com Jan 26 at 20:35


This question came from our site for users of Linux, FreeBSD and other Un*x-like operating systems.









migrated from unix.stackexchange.com Jan 26 at 20:35


This question came from our site for users of Linux, FreeBSD and other Un*x-like operating systems.









  • 2





    Did you do anything yourself? Do you have an algorithm? Why would two groups of 8 and 4 be closer to 10 than one group of 8 and 4?

    – Tomasz
    Jan 26 at 19:44











  • This is the bin packing problem. This is an NP-hard problem to solve (i.e. not trivial). I wrote a few lines of zsh do do it for file sizes here: "Group files into average size of the largest file", but really, it's a bit outside the scope of this site.

    – Kusalananda
    Jan 26 at 19:46











  • Atm I have not write code yet. I'm thinking smb like first sort lines by number and then loop sum til 10 is reached and group1 is complete.. then next lines. Maybe someone knows some program that can already do that. @Tomasz 8 and 4 are not closer to 10 but you can't make any more groups ie. they would exceed 10 whitch is not allowed. Smaller groups are ok.

    – Mart
    Jan 26 at 20:13











  • You should be able to take the code I linked to and modify it to fit your needs. It's currently getting filenames and file sizes, but you could change that to be your strings and numbers.

    – Kusalananda
    Jan 26 at 20:18











  • thanks @Kusalananda I'm looking it :)

    – Mart
    Jan 26 at 20:21












  • 2





    Did you do anything yourself? Do you have an algorithm? Why would two groups of 8 and 4 be closer to 10 than one group of 8 and 4?

    – Tomasz
    Jan 26 at 19:44











  • This is the bin packing problem. This is an NP-hard problem to solve (i.e. not trivial). I wrote a few lines of zsh do do it for file sizes here: "Group files into average size of the largest file", but really, it's a bit outside the scope of this site.

    – Kusalananda
    Jan 26 at 19:46











  • Atm I have not write code yet. I'm thinking smb like first sort lines by number and then loop sum til 10 is reached and group1 is complete.. then next lines. Maybe someone knows some program that can already do that. @Tomasz 8 and 4 are not closer to 10 but you can't make any more groups ie. they would exceed 10 whitch is not allowed. Smaller groups are ok.

    – Mart
    Jan 26 at 20:13











  • You should be able to take the code I linked to and modify it to fit your needs. It's currently getting filenames and file sizes, but you could change that to be your strings and numbers.

    – Kusalananda
    Jan 26 at 20:18











  • thanks @Kusalananda I'm looking it :)

    – Mart
    Jan 26 at 20:21







2




2





Did you do anything yourself? Do you have an algorithm? Why would two groups of 8 and 4 be closer to 10 than one group of 8 and 4?

– Tomasz
Jan 26 at 19:44





Did you do anything yourself? Do you have an algorithm? Why would two groups of 8 and 4 be closer to 10 than one group of 8 and 4?

– Tomasz
Jan 26 at 19:44













This is the bin packing problem. This is an NP-hard problem to solve (i.e. not trivial). I wrote a few lines of zsh do do it for file sizes here: "Group files into average size of the largest file", but really, it's a bit outside the scope of this site.

– Kusalananda
Jan 26 at 19:46





This is the bin packing problem. This is an NP-hard problem to solve (i.e. not trivial). I wrote a few lines of zsh do do it for file sizes here: "Group files into average size of the largest file", but really, it's a bit outside the scope of this site.

– Kusalananda
Jan 26 at 19:46













Atm I have not write code yet. I'm thinking smb like first sort lines by number and then loop sum til 10 is reached and group1 is complete.. then next lines. Maybe someone knows some program that can already do that. @Tomasz 8 and 4 are not closer to 10 but you can't make any more groups ie. they would exceed 10 whitch is not allowed. Smaller groups are ok.

– Mart
Jan 26 at 20:13





Atm I have not write code yet. I'm thinking smb like first sort lines by number and then loop sum til 10 is reached and group1 is complete.. then next lines. Maybe someone knows some program that can already do that. @Tomasz 8 and 4 are not closer to 10 but you can't make any more groups ie. they would exceed 10 whitch is not allowed. Smaller groups are ok.

– Mart
Jan 26 at 20:13













You should be able to take the code I linked to and modify it to fit your needs. It's currently getting filenames and file sizes, but you could change that to be your strings and numbers.

– Kusalananda
Jan 26 at 20:18





You should be able to take the code I linked to and modify it to fit your needs. It's currently getting filenames and file sizes, but you could change that to be your strings and numbers.

– Kusalananda
Jan 26 at 20:18













thanks @Kusalananda I'm looking it :)

– Mart
Jan 26 at 20:21





thanks @Kusalananda I'm looking it :)

– Mart
Jan 26 at 20:21












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54382508%2fhow-to-group-numbers-by-predefined-sum%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown
























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54382508%2fhow-to-group-numbers-by-predefined-sum%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown






Popular posts from this blog

How to check contact read email or not when send email to Individual?

Bahrain

Postfix configuration issue with fips on centos 7; mailgun relay