Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Task 2 — Pair Sum Delta Matching

Objective

Create a function that combines values from two integer slices by matching index positions and returns the indexes of pairs whose sums satisfy a target value modified by a delta rule.

The delta controls the allowed range relative to the target.

Input

The function has four input arguments.

First Slice

p1 := []int{
    -4, 5, -1, 3, 7,
    -3, 6, -2, 4, 8,
    10, 13, 17, 9, 2,
}

Second Slice

p2 := []int{
    12, 2, 7, 6, 3,
    8, 0, 15, 5, -4,
    -1, -6, -5, -2, 6,
}

Target

The third argument is an integer representing the target sum.

Example:

9

Delta

The fourth argument is a string describing how the target range should be expanded.

Supported forms are:

+N
-N
*N

where N is a positive integer delta.

Examples:

+2
-2
*2

Pair Creation

Values from p1 and p2 are paired using matching indexes.

For example:

Index 0 -> {-4, 12}
Index 1 -> {5, 2}
Index 2 -> {-1, 7}

For every index i:

pairSum = p1[i] + p2[i]

Delta Rules

Positive Delta

A positive delta:

+N

defines the inclusive range:

target <= pairSum <= target + N

For example:

target = 9
delta  = +2

produces:

9 <= pairSum <= 11

Negative Delta

A negative delta:

-N

defines the inclusive range:

target - N <= pairSum <= target

For example:

target = 8
delta  = -2

produces:

6 <= pairSum <= 8

Absolute Delta

An absolute delta:

*N

defines an inclusive range on both sides of the target:

target - N <= pairSum <= target + N

For example:

target = 11
delta  = *2

produces:

9 <= pairSum <= 13

Function

The function is named:

FindSum(...)

Conceptually:

FindSum(p1, p2, target, delta)

returns:

[]int

containing the indexes of matching pairs.

Case 1 — Positive Delta

Call:

FindSum(p1, p2, 9, "+2")

Allowed sum range:

9 <= pairSum <= 11

Expected Result

[]int{
    3, 4, 7, 8, 10,
}

Case 2 — Negative Delta

Call:

FindSum(p1, p2, 8, "-2")

Allowed sum range:

6 <= pairSum <= 8

Expected Result

[]int{
    0, 1, 2, 6, 11, 13, 14,
}

Case 3 — Absolute Delta

Call:

FindSum(p1, p2, 11, "*2")

Allowed sum range:

9 <= pairSum <= 13

Expected Result

[]int{
    3, 4, 7, 8, 10, 12,
}

Delta Validation

The delta is provided as a string and must be interpreted by the function.

A valid delta consists of:

operator + numeric value

where the operator is one of:

+
-
*

and the numeric portion represents a positive integer.

Examples of valid values:

+2
-3
*5

Invalid or unsupported delta values should result in an error rather than silently using an undefined range.

Input Validation

Because p1 and p2 represent correlated values, they should contain the same number of elements.

The implementation should validate:

len(p1) == len(p2)

The delta string should also be validated before pair processing begins.

Implementation Notes

The three delta modes represent three different interval calculations.

It may be useful to convert the delta string into normalized lower and upper boundaries before processing the pairs.

Conceptually:

Target + Delta
      ↓
Resolve allowed range
      ↓
Process pairs
      ↓
Calculate pair sums
      ↓
Return matching indexes

This keeps delta parsing separate from pair matching.

Scalionix Docs

Keyboard Shortcuts

Navigate the documentation without leaving the keyboard.
Navigation
Previous subject
←
Next subject
→
Previous subsection
Alt + ↑
Next subsection
Alt + ↓
Interface
Documentation Home
Ctrl + Enter
Search
Alt + Q
Open shortcuts
?
Close dialog
Esc
Scalionix Docs

Search Documentation