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 4 — Indexed Multiplication Limits

Objective

Create a function that associates each decimal value with a collection of multipliers, calculates a resulting value, and compares that result against an individual limit.

The function must return a typed result describing both the calculated value and whether it satisfies the limit condition.

Input

The function has three input arguments.

All three collections are related by index.

First Argument — Values

The first argument is:

[]float64

containing 8 decimal values.

The values should be generated in the range:

value > 1.0 && value <= 10.0

with precision to three decimal places.

Example:

list1 := []float64{
    3.213,
    2.543,
    6.435,
    // ...
}

Second Argument — Multipliers

The second argument is:

[][]float64

Each inner slice contains the multipliers associated with the value at the same index in list1.

Example:

list2 := [][]float64{
    {1.20, 1.40},
    {1.25, 1.45},
    {1.20, 1.30},
    {1.15, 1.25},
    {1.20, 1.35},
    {1.20, 1.25},
    {1.15, 1.40},
    {1.25, 1.35},
}

For example:

list1[0] = 3.213

list2[0] = {1.20, 1.40}

Therefore, both multipliers belong to the value 3.213.

Third Argument — Limits

The third argument is:

[]float64

containing the limit associated with every value.

Example:

list3 := []float64{
    5.5,
    4.5,
    6.5,
    8.5,
    14.5,
    12.5,
    9.5,
    11.5,
}

The limit at index i applies to the calculated result for list1[i].

Result Type

Define a typed result structure:

type CalculationResult struct {
    Value      float64
    BelowLimit bool
}

The function returns:

[]CalculationResult

This replaces the mixed float64 / bool result representation from the original task with an explicit typed result.

Calculation

For every index i, begin with:

list1[i]

and multiply it by every multiplier contained in:

list2[i]

Conceptually:

result =
    list1[i]
    * list2[i][0]
    * list2[i][1]
    * ...

Then compare the calculated result against:

list3[i]

Result Rules

If:

calculatedValue < limit

return:

CalculationResult{
    Value:      calculatedValue,
    BelowLimit: true,
}

If:

calculatedValue >= limit

return:

CalculationResult{
    Value:      calculatedValue,
    BelowLimit: false,
}

The calculated value is preserved in both cases.

Example 1

Given:

value       = 3.213
multipliers = {1.20, 1.40}
limit       = 5.5

calculate:

3.213 * 1.20 * 1.40 = 5.39784

Because:

5.39784 < 5.5

the result is:

CalculationResult{
    Value:      5.39784,
    BelowLimit: true,
}

If the result is presented with three decimal places:

5.398

Example 2

Given:

value       = 2.543
multipliers = {1.25, 1.45}
limit       = 4.5

calculate:

2.543 * 1.25 * 1.45 = 4.6091875

Because:

4.6091875 >= 4.5

the result is:

CalculationResult{
    Value:      4.6091875,
    BelowLimit: false,
}

Requirements

For every index:

  1. take the value from list1
  2. retrieve its multipliers from list2
  3. multiply the value by all associated multipliers
  4. retrieve the corresponding limit from list3
  5. compare the calculated result with the limit
  6. return a CalculationResult

Input Relationship

The three collections are correlated by index.

Therefore:

list1[i]
list2[i]
list3[i]

all describe the same calculation.

The outer lengths must therefore match:

len(list1) == len(list2)
len(list1) == len(list3)

A robust implementation should validate these relationships before performing calculations.

Precision

The generated source values use three decimal places.

Intermediate calculations should not be unnecessarily rounded before the comparison is performed.

If a formatted result is needed for presentation, rounding should occur only after the comparison.

Display Requirement

Before executing the calculation function, print or otherwise display the values from:

list1

and:

list2

as required by the original exercise.

Design Change from the Original Task

The original version returned a heterogeneous collection containing either:

float64

or:

false

depending on the comparison result.

This revised specification uses:

CalculationResult

instead.

The calculation itself is unchanged, but the result is now type-safe and preserves the calculated value even when the limit is exceeded.

Implementation Notes

A typed result avoids runtime type assertions and provides the same representation in languages such as Go and Rust without relying on a generic or empty-interface collection.

The algorithm should support any number of multipliers associated with a value rather than assuming exactly two.

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