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 — Pair Normalization Analysis

Objective

Create two functions.

The first function creates pairs by combining values from opposite ends of an integer slice.

The second function analyzes each pair by multiplying its values and comparing the result with a normalization value.

For every pair, produce a textual description of the comparison result.

Input

The source slice is:

list := []int{
    39,
    50,
    64,
    81,
    72,
    31,
    43,
    48,
    29,
    99,
}

Function 1 — CreatePairs

Create a function named:

CreatePairs(...)

The function receives the source:

[]int

and returns:

[][]int

Pairing Rules

Pairs are created using values from opposite ends of the slice.

The first value is paired with the last value.

The second value is paired with the second-to-last value.

Continue toward the center until all values have been paired.

For the provided input:

39 <-> 99
50 <-> 29
64 <-> 48
81 <-> 43
72 <-> 31

Expected Pair Result

[][]int{
    {39, 99},
    {50, 29},
    {64, 48},
    {81, 43},
    {72, 31},
}

Store this result as:

pairList := CreatePairs(list)

Function 2 — CheckNormalization

Create a second function named:

CheckNormalization(...)

The function receives:

1. the [][]int produced by CreatePairs
2. an integer normalization value

and returns:

[]string

Normalization Calculation

For each pair:

{a, b}

calculate:

multiplication = a * b

Then compare the result with the normalization value.

The output message must describe:

  • which pair was analyzed
  • the multiplication result
  • whether the result is greater than, smaller than, or equal to the normalization value
  • the absolute difference between the multiplication result and the normalization value

Example

Create the pairs:

pairList := CreatePairs(list)

Then call:

infoLog := CheckNormalization(pairList, 3000)

The normalization value is:

3000

Pair 1

Pair:

{39, 99}

Calculation:

39 * 99 = 3861

Difference:

3861 - 3000 = 861

Result:

greater than normalization value by 861

Pair 2

Pair:

{50, 29}

Calculation:

50 * 29 = 1450

Difference:

3000 - 1450 = 1550

Result:

smaller than normalization value by 1550

Pair 3

Pair:

{64, 48}

Calculation:

64 * 48 = 3072

Difference:

3072 - 3000 = 72

Result:

greater than normalization value by 72

Pair 4

Pair:

{81, 43}

Calculation:

81 * 43 = 3483

Difference:

3483 - 3000 = 483

Result:

greater than normalization value by 483

Pair 5

Pair:

{72, 31}

Calculation:

72 * 31 = 2232

Difference:

3000 - 2232 = 768

Result:

smaller than normalization value by 768

Expected Output

The returned []string should contain descriptions equivalent to:

Analyzed pair has values [39, 99]. Multiplication value 3861 is greater than normalization value by 861.

Analyzed pair has values [50, 29]. Multiplication value 1450 is smaller than normalization value by 1550.

Analyzed pair has values [64, 48]. Multiplication value 3072 is greater than normalization value by 72.

Analyzed pair has values [81, 43]. Multiplication value 3483 is greater than normalization value by 483.

Analyzed pair has values [72, 31]. Multiplication value 2232 is smaller than normalization value by 768.

Equality Case

A complete implementation should also handle a multiplication result that is exactly equal to the normalization value.

In that case:

multiplication == normalization

and the difference is:

0

The message should clearly report that the multiplication value is equal to the normalization value.

Odd-Length Input

The original example contains an even number of values, so every element can be paired.

The original specification does not define behavior for a slice with an odd number of elements.

A robust implementation should explicitly choose one of the following behaviors:

reject odd-length input

or define how the single center element should be represented.

For this task, rejecting odd-length input is the simplest unambiguous behavior because every result entry is required to be a pair.

Requirements

CreatePairs must:

  1. process values from both ends of the source slice
  2. create two-element pairs
  3. move toward the center
  4. return the pairs as [][]int

CheckNormalization must:

  1. process every pair
  2. multiply the two values
  3. compare the product with the normalization value
  4. calculate the difference
  5. return one description for each analyzed pair

Implementation Notes

The pair order must follow the source positions demonstrated by the example.

For a slice of length n, the conceptual pairing is:

index 0     with index n-1
index 1     with index n-2
index 2     with index n-3
...

The normalization comparison should use the actual multiplication result, and the reported difference should always be non-negative.

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