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 — Sector Range Reconstruction

Objective

Create a function that analyzes three integer collections assigned to predefined numeric sectors.

The function must:

  1. detect values placed in the wrong sector
  2. report those values
  3. move them to the correct sector
  4. determine which values are missing from every sector
  5. produce complete sorted sector information

Sector Definitions

There are three sectors.

Sector 1

Valid values:

1 through 50

inclusive.

Sector 2

Valid values:

51 through 100

inclusive.

Sector 3

Valid values:

101 through 150

inclusive.

A complete sector would therefore contain every integer inside its assigned range exactly as required by the task.

Input

Sector 1 Input

list1 := []int{
    7, 9, 31, 40, 18, 8, 14, 61,
    48, 104, 22, 44, 10, 38, 50,
    12, 41, 21, 15, 110, 6, 33,
    74, 20, 17, 13, 35, 39, 19,
    14, 29,
}

Sector 2 Input

list2 := []int{
    70, 100, 60, 90, 4, 55, 99,
    50, 75, 132, 65, 59, 81, 62,
    72, 92, 51, 66, 16, 58, 142,
    94, 77, 63, 88, 68, 83, 96,
}

Sector 3 Input

list3 := []int{
    135, 102, 131, 108, 141, 114,
    101, 28, 139, 144, 128, 119,
    87, 133, 122, 107, 147, 105,
    150, 124, 109, 121, 34, 136,
    103, 123, 95, 143, 106, 127,
    117, 125, 112, 120, 130,
}

Misplaced Values

A misplaced value is valid globally but stored in the wrong sector.

For the provided input, the misplaced values are:

61   : Sector 1 -> Sector 2
104  : Sector 1 -> Sector 3
110  : Sector 1 -> Sector 3
74   : Sector 1 -> Sector 2

4    : Sector 2 -> Sector 1
50   : Sector 2 -> Sector 1
132  : Sector 2 -> Sector 3
16   : Sector 2 -> Sector 1
142  : Sector 2 -> Sector 3

28   : Sector 3 -> Sector 1
87   : Sector 3 -> Sector 2
34   : Sector 3 -> Sector 1
95   : Sector 3 -> Sector 2

There are therefore:

13

misplaced values.

Correction to the Original Example

The original task lists the following misplaced values:

4, 16, 28, 34, 61, 74, 87, 95, 104, 110, 132, 142

However, the source data also contains:

50

inside Sector 2.

Since Sector 2 is defined as:

51 through 100

the value 50 belongs to Sector 1.

It is therefore also a misplaced value and must be corrected.

Suggested Result Model

A structured result can be used:

type MisplacedValue struct {
    Value      int
    FromSector int
    ToSector   int
}

type SectorResult struct {
    SectorID int
    Values   []int
    Missing  []int
}

type ReconstructionResult struct {
    Misplaced []MisplacedValue
    Sectors   []SectorResult
}

The exact naming may be changed, but the report must preserve equivalent information.

Missing Values

After all misplaced values have been moved to their correct sectors, determine which values are absent.

Missing Values — Sector 1

[]int{
    1, 2, 3, 5, 11,
    23, 24, 25, 26, 27,
    30, 32, 36, 37,
    42, 43, 45, 46, 47, 49,
}

Missing Values — Sector 2

[]int{
    52, 53, 54, 56, 57,
    64, 67, 69, 71, 73,
    76, 78, 79, 80,
    82, 84, 85, 86,
    89, 91, 93, 97, 98,
}

Missing Values — Sector 3

[]int{
    111, 113, 115, 116, 118,
    126, 129, 134, 137, 138,
    140, 145, 146, 148, 149,
}

Duplicate Values

The input may contain duplicate values.

For example, Sector 1 contains:

14

more than once.

A complete sector represents numeric membership rather than occurrence count.

Therefore duplicates should not cause a value to appear multiple times in the reconstructed sorted sector.

Requirements

The function must:

  1. know the valid range of every sector
  2. inspect every input value
  3. determine the sector where the value currently exists
  4. determine the sector where it belongs
  5. report every misplaced value
  6. move misplaced values to their correct sectors
  7. normalize duplicate membership where appropriate
  8. sort each corrected sector
  9. determine all missing values
  10. return a structured report

Values Outside All Sectors

A value outside:

1 through 150

does not belong to any defined sector.

Such a value should be reported as invalid rather than silently inserted into a sector.

Implementation Notes

A useful approach is to separate:

classification

from:

missing-value detection

First normalize every value into its correct sector.

Then compare each normalized sector with its complete expected range.

This prevents misplaced values from being incorrectly reported as missing in one sector while still being stored in another.

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