Two sum: Difference between revisions
Content added Content deleted
mNo edit summary |
(→version 2: updated the REXX program to handle the latest task requirements/specifications, used a template for the output, added a solution count (and message), added an aligned summation, indicated that solution contains (zero based) indices.) |
||
Line 1,272: | Line 1,272: | ||
===version 2=== |
===version 2=== |
||
All solutions are listed (if any), along with a count of the number of solutions. |
|||
This REXX version uses optimization in using integers that take advantage |
|||
that the numbers are an ordered list of positive integers. |
|||
Also, it's mentioned that the indices are zero─based, and formatted solutions are shown. |
|||
All solutions are listed (if any). |
|||
⚫ | |||
parse arg targ list /*obtain optional arguments from the CL*/ |
|||
if targ='' | targ="," then targ= 21 /*Not specified? Then use the defaults*/ |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
@.#=_; w=max(w, length(_) ) /*assign a number to an indexed array. */ |
|||
⚫ | |||
say /* [↓] look for sum of 2 numbers=target*/ |
|||
do a=0 for # /*scan up to the last number in array. */ |
|||
⚫ | |||
sol=sol+1 /*bump count of the number of solutions*/ |
|||
say 'a solution: zero─based indices ['a"," b']' , |
|||
' ' right(@.a, w) " + " right(@.b, w) ' = ' targ |
|||
end /*b*/ /*show the 2 indices and the summation.*/ |
|||
⚫ | |||
say |
|||
if sol==0 then sols= 'None' /*prettify the number of solutions if 0*/ |
|||
say 'number of solutions found: ' sol /*stick a fork in it, we're all done. */</lang> |
|||
{{out|output|text= when using the default inputs:}} |
|||
<pre> |
|||
the list: 0 2 11 19 90 |
|||
the target sum: 21 |
|||
a solution: zero─based indices [1, 3] 2 + 19 = 21 |
|||
This REXX solution makes allowances for the inclusion of zero, even though it is specifically excluded by the task's description. However zero is included in the task's requirement. |
|||
number of solutions found: 1 |
|||
If the list <u>isn't</u> an ordered list of positive integers, then |
|||
</pre> |
|||
the '''while''' clause (below) should be removed, and then this REXX |
|||
{{out|output|text= when using the input of: 21 -78 -5 -4 -1 0 1 2 3 5 11 14 16 17 18 18 19 23 24 25 26 99}} |
|||
version will work with any list (including negative numbers and also with any |
|||
real number. |
|||
⚫ | |||
⚫ | |||
sum=21 ; say 'target sum: ' sum |
|||
⚫ | |||
@.#=word(list, #+1) |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
'''output''' |
|||
<pre> |
<pre> |
||
the list: -78 -5 -4 -1 0 1 2 3 5 11 14 16 17 18 18 19 23 24 25 26 99 |
|||
target sum: 21 |
the target sum: 21 |
||
a solution: zero─based indices [0, 20] -78 + 99 = 21 |
|||
a solution: zero─based indices [1, 19] -5 + 26 = 21 |
|||
a solution: zero─based indices [2, 18] -4 + 25 = 21 |
|||
a solution: zero─based indices [6, 15] 2 + 19 = 21 |
|||
a solution: zero─based indices [7, 13] 3 + 18 = 21 |
|||
a solution: zero─based indices [7, 14] 3 + 18 = 21 |
|||
a solution: zero─based indices [8, 11] 5 + 16 = 21 |
|||
number of solutions found: 7 |
|||
a solution: [1, 3] |
|||
</pre> |
</pre> |
||