0

An number is added to a file on daily basis. Like 1000, 2000, 2560, 5356, etc. till last day of the month, say upto 30/31 numbers. I want to write a shell script with which when I pass a random number like 5560, this 5560 can be combination of any 2 or 3 or 4 or 5 numbers whose sum is equal to 5560. These numbers can be of any permutation or combination. All those numbers should get printed whose sum is equal to the passed random number 5560.

Possible solution I can think of:

  1. Loop thru the file and store all the values in an array.
  2. use brace to make combinations of array elements
  3. ?
4
  • Don't do that in the shell. Take something like Python where you have better tools for handling lists etc.
    – ilkkachu
    Jul 13, 2020 at 21:24
  • I could have chosen python myself but the requirement is to do in shell :(
    – Saurabh
    Jul 14, 2020 at 4:05
  • This is a difficult requirement in shell. What level are you learning at? I have 25 lines of code that generate all required combinations of index (206336 in 16 secs). Not yet got to the arithmetic part, or a test strategy. I wasted a couple of hours trying to understand the answer to question 123800 before testing it, and finding it did not work for more than 3 items anyway. Jul 14, 2020 at 14:21
  • I have achieved the results using python and would now try to convert it in bash. Not sure how far I can go after hearing from you that it's difficult. I am not learning bash topic wise but with on going assignments and tasks.
    – Saurabh
    Jul 14, 2020 at 16:23

1 Answer 1

0

Addition is commutative. So the sum of a specific combination is the same as the sum of any permutation of the same numbers.

That should save some time. I believe the number of combinations of 31 numbers {2,3,4,5} at a time is only 206,336, whereas the number of permutations {2,3,4,5} at a time is 21,172,380.

Combinations lend themselves well to recursive solutions, but your runtime in shell is likely to be annoying, if not prohibitive. Would you consider an awk solution?

Are all the numbers in the file unique? If (3 + 3) = 6, then there are two (identical) combinations because the 3s can be reordered.

2
  • Numbers in the file could be repeated or unique. Requirement is to do in bash and any solution in bash would do. I am new to bash, requirement is strict otherwise I would have chosen Python over shell.
    – Saurabh
    Jul 14, 2020 at 4:07
  • If you have a working Python code, then converting it to Bash is mainly getting around the syntax issues, especially with array notation. I came up with 60 lines that generate combinations of indexes, and a summator, reporter and some setup. Interesting problem. Those repeated values mean that you can't eliminate them (you might still need them for 4 + 4 + 6 = 14). But if you have three 4s on lines a b c, you get three identical matches a+b,a+c,b+c, so you need to eliminate duplicates later. It is also hard to construct proper data. Good Luck! Jul 14, 2020 at 18:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .