Two sum: Difference between revisions

Content added Content deleted
(Added 11l)
m (→‎version 2: added/changed comments and whitespace.)
Line 2,071: Line 2,071:
The list of numbers can be in any format,   not just integers.   Also, they need not be unique.
The list of numbers can be in any format,   not just integers.   Also, they need not be unique.


The list of integers need   ''not''   be sorted.
The list of integers   needn't   be sorted.


A   '''numeric digits 500'''   statement was added just in case some rather large numbers were entered.
A   '''numeric digits 500'''   statement was added just in case some humongous numbers were entered.


No verification was performed to ensure that all items were numeric.
No verification was performed to ensure that all items in the list were numeric.


A little extra code was added to have the output columns aligned.
A little extra code was added to have the output columns aligned.
Line 2,085: Line 2,085:
say 'the list: ' list /*echo the list to the terminal*/
say 'the list: ' list /*echo the list to the terminal*/
say 'the target sum: ' targ /* " " target sum " " " */
say 'the target sum: ' targ /* " " target sum " " " */
w= 0; sol= 0 /*width; # of solutions found (so far)*/
@solution= 'a solution: zero─based indices ' /*a SAY literal for space conservation.*/
sol=0; w=0 /*number of solutions found (so far). */
do #=0 for words(list); _=word(list, #+1) /*examine the list, construct an array.*/
do #=0 for words(list); _=word(list, #+1) /*examine the list, construct an array.*/
@.#= _; w= max(w, length(_) ) /*assign a number to an indexed array. */
@.#= _; w= max(w, length(_) ) /*assign a number to an indexed array. */
end /*#*/ /*W: the maximum width of any number. */
end /*#*/ /*W: the maximum width of any number. */
L= length(#) /*L: " " " " " index. */
L= length(#) /*L: " " " " " index. */
@solution= 'a solution: zero─based indices ' /*a SAY literal for space conservation.*/
say /* [↓] look for sum of 2 numbers=target*/
say /* [↓] look for sum of 2 numbers=target*/
do a=0 for # /*scan up to the last number in array. */
do a=0 for # /*scan up to the last number in array. */