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 5 — Conditional Combination Generator

Objective

Create a generic combination generator that selects specified source lists, generates their Cartesian product, calculates the sum of every combination, and returns only combinations whose sums satisfy a configurable target condition.

The function must remain generic regardless of:

  • how many source lists exist
  • which source lists are selected
  • how many values each selected list contains

Source Data

List 1

list1 := []int{
    7, 9, 18, 8, 14, 10, 12,
    21, 15, 6, 20, 17, 13,
}

List 2

list2 := []int{
    31, 41, 33, 40, 38, 35,
}

List 3

list3 := []int{
    45, 60, 55, 50, 65,
    70, 48, 58, 75,
}

List 4

list4 := []int{
    85, 90, 105, 95, 115,
    100, 99, 125, 119,
}

List 5

list5 := []int{
    195, 205, 215, 275, 230,
    240, 220, 250, 290, 305,
}

Represent them as:

data := [][]int{
    list1,
    list2,
    list3,
    list4,
    list5,
}

Function

Create one generic function:

CreateCombinations(...)

Conceptually:

CreateCombinations(
    data,
    selectedLists,
    target,
    delta,
)

Argument 1 — Source Lists

The first argument is:

[][]int

containing all available source lists.

Argument 2 — Selected Lists

The second argument is:

[]int

containing indexes of the source lists that should participate in combination generation.

Indexes are zero-based.

For example:

[]int{0, 1, 3}

selects:

List 1
List 2
List 4

A generated combination will therefore contain exactly three values:

one from List 1
one from List 2
one from List 4

Argument 3 — Target

The third argument is an integer target.

The sum of every generated combination is compared against this target.

Argument 4 — Delta

The fourth argument is a string defining the allowed deviation from the target.

Supported forms are:

+N
-N
*N
0

Positive Delta

For:

target = T
delta  = +N

the allowed range is:

T <= sum <= T + N

Example:

target = 50
delta  = +10

means:

50 <= sum <= 60

Negative Delta

For:

target = T
delta  = -N

the allowed range is:

T - N <= sum <= T

For example:

target = 50
delta  = -5

means:

45 <= sum <= 50

Absolute Delta

For:

target = T
delta  = *N

the allowed range is:

T - N <= sum <= T + N

Example:

target = 100
delta  = *10

means:

90 <= sum <= 110

Zero Delta

For:

delta = 0

no range is used.

A combination is accepted only when:

sum == target

Example:

target = 120
delta  = 0

means:

sum == 120

Combination Generation

The generator must first select the requested source lists.

Then it must generate every possible combination containing one value from every selected list.

For example:

selectedLists := []int{
    0,
    1,
}

means that the combinations are generated from:

List 1 × List 2

For every generated combination:

  1. calculate its sum
  2. compare the sum with the resolved target range
  3. return the combination only when it satisfies the condition

Suggested Result Model

Instead of returning only the raw values, a structured result is useful:

type CombinationResult struct {
    Values []int
    Sum    int
}

The function can then return:

[]CombinationResult

This preserves both:

the generated values

and:

the sum used to accept the combination

Original Example 1

Call:

CreateCombinations(
    data,
    []int{0, 1},
    50,
    "-5",
)

Selected lists:

List 1
List 2

Allowed sum range:

45 <= sum <= 50

Original Example 2

Call:

CreateCombinations(
    data,
    []int{0, 1, 2},
    100,
    "*10",
)

Selected lists:

List 1
List 2
List 3

Allowed range:

90 <= sum <= 110

Original Example 3

Call:

CreateCombinations(
    data,
    []int{0, 1, 3},
    150,
    "+10",
)

Selected lists:

List 1
List 2
List 4

Allowed range:

150 <= sum <= 160

Original Example 4

Call:

CreateCombinations(
    data,
    []int{0, 1, 3, 4},
    350,
    "+30",
)

Selected lists:

List 1
List 2
List 4
List 5

Allowed range:

350 <= sum <= 380

Original Example 5

Call:

CreateCombinations(
    data,
    []int{0, 1, 3, 4},
    355,
    "0",
)

The combination sum must satisfy:

sum == 355

Selection Validation

Every index in:

selectedLists

must reference an existing source list.

For example, with five source lists, valid indexes are:

0
1
2
3
4

An index such as:

5

is invalid.

Duplicate Selected Indexes

A selected list should appear only once.

For example:

[]int{
    0,
    1,
    1,
}

is ambiguous because it requests the same source dimension twice.

A robust implementation should reject duplicate selected indexes.

Empty Selection

If:

selectedLists

is empty, no combination can be formed for this exercise.

Return an empty result or an explicit validation error.

Delta Validation

Valid delta forms are:

0
+N
-N
*N

where N is a positive integer.

Examples:

0
+10
-5
*20

Invalid examples include:

++
abc
*-
10+

Invalid delta input should produce an error rather than silently using a default interpretation.

Generic Requirement

The function must not contain separate implementations for:

2 selected lists
3 selected lists
4 selected lists

The same combination generator must support every valid number of selected dimensions.

Processing Flow

A clean implementation can separate the task into stages:

Validate input
      ↓
Resolve selected lists
      ↓
Parse delta
      ↓
Resolve allowed sum range
      ↓
Generate Cartesian product
      ↓
Calculate combination sum
      ↓
Filter
      ↓
Return matches

Optimization

A straightforward implementation may generate the complete Cartesian product and filter afterward.

However, for larger datasets the number of combinations grows multiplicatively.

For source sizes:

L1, L2, ..., Ln

the number of candidate combinations is:

L1 * L2 * ... * Ln

An advanced implementation may prune partial combinations when the input characteristics and target range make it safe to do so.

Correctness should be established before introducing such optimization.

Relationship to Task 4

Task 4 generates:

all combinations

Task 5 extends that operation with:

list selection
+
sum calculation
+
target filtering
+
delta rules

The Cartesian-product implementation from Task 4 should therefore be reusable rather than reimplemented specifically for this task.

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