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 3 — Sector Range Analysis

Objective

Create a function that divides an integer slice into a configurable number of equal sectors.

Every sector has its own accepted numeric range.

For each sector, return:

  • values that satisfy the sector range
  • values that do not satisfy the sector range

Input

The function has three input arguments.

The original task refers to two inputs but subsequently defines three separate arguments.

This specification uses the three arguments actually required by the task.

Argument 1 — Values

The first argument is a slice containing exactly 30 integers:

values := []int{
    82, 45, 67, 12, 94,
    38, 71, 56, 53, 29,
    39, 87, 21, 64, 54,
    32, 79, 14, 92, 68,
    53, 27, 81, 60, 49,
    73, 11, 93, 37, 65,
}

Argument 2 — Sector Count

The second argument determines how many equal sectors the input should be divided into.

Allowed values are:

2
3
5
6
10

Because the input contains 30 elements, these values divide the input evenly.

Sector Sizes

The resulting sector sizes are:

2 sectors  -> 15 elements per sector
3 sectors  -> 10 elements per sector
5 sectors  -> 6 elements per sector
6 sectors  -> 5 elements per sector
10 sectors -> 3 elements per sector

Sector Creation

Sectors are created using the original order of the input slice.

For example, with:

sectorCount = 5

the 30 values are divided into five consecutive groups containing six values each.

Conceptually:

Sector 1 -> values[0:6]
Sector 2 -> values[6:12]
Sector 3 -> values[12:18]
Sector 4 -> values[18:24]
Sector 5 -> values[24:30]

Argument 3 — Sector Ranges

The third argument is:

[]string

containing one accepted range for every sector.

A range is represented as:

min-max

For example:

50-60

means:

value >= 50 && value <= 60

Both boundaries are inclusive.

Example Range Configuration

If:

sectorCount = 2

a valid range configuration could be:

[]string{
    "25-35",
    "60-75",
}

The number of ranges must equal the number of sectors.

Result Model

Define a result for each sector.

For example:

type SectorResult struct {
    SectorID      int
    Range         string
    Matching      []int
    NotMatching   []int
}

The complete function may return:

type SectorAnalysis struct {
    SectorCount int
    Sectors     []SectorResult
}

Processing

For every sector:

  1. parse its range
  2. inspect each value belonging to that sector
  3. compare the value with the inclusive range
  4. place matching values into Matching
  5. place non-matching values into NotMatching

Example Condition

For:

range = "50-60"

the condition is:

50 <= value <= 60

Examples:

49 -> not matching
50 -> matching
55 -> matching
60 -> matching
61 -> not matching

Range Parsing

A range must contain two valid integer boundaries.

For:

25-35

parse:

minimum = 25
maximum = 35

A valid range requires:

minimum <= maximum

Validation

The function must validate:

len(values) == 30

for the original exercise.

It must also validate:

sectorCount ∈ {2, 3, 5, 6, 10}

and:

len(ranges) == sectorCount

Every range must be syntactically valid.

Invalid Range Examples

Examples of invalid ranges include:

"abc"
"50"
"60-50"
"10-x"

These should produce a validation error.

Requirements

The function must:

  1. receive the 30-element source slice
  2. validate the requested sector count
  3. divide the source into equal consecutive sectors
  4. validate one range per sector
  5. parse every range
  6. classify each value
  7. return both matching and non-matching values for every sector
  8. report the total number of sectors

Testing

Create multiple tests.

Recommended cases include:

  • 2 sectors
  • 3 sectors
  • 5 sectors
  • 6 sectors
  • 10 sectors
  • invalid sector count
  • incorrect number of ranges
  • malformed range
  • reversed range boundaries
  • sector where all values match
  • sector where no values match

Implementation Notes

The source order determines sector membership.

The task does not ask for values to be sorted before division.

Therefore:

divide first
then analyze

rather than:

sort first
then divide

unless an extended version of the task explicitly introduces sorting.

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