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 — Conditional Map Filtering

Objective

Create a function that searches selected entries in a map[int][]float64 and returns only the values that satisfy a specified comparison condition.

The function must allow the caller to:

  • select which map keys should be processed
  • choose a comparison operator
  • provide a comparison value

The result is another map[int][]float64 containing the matching values.

Input

The function has four input arguments.

First Argument

The first argument is:

map[int][]float64

Example:

m1 := map[int][]float64{
    1: {3.8, 4.6, 5.2},
    2: {2.2, 3.5, 4.9},
    3: {2.7, 3.1, 4.1},
}

Each map key is associated with a collection of float64 values.

Second Argument

The second argument is:

[]int

This slice defines which map keys should be searched.

For example:

[]int{1, 3}

means that only map entries with keys 1 and 3 should be processed.

Third Argument

The third argument is a string representing the comparison operator.

The supported values are:

L  -> Less Than
LE -> Less Than or Equal To
G  -> Greater Than
GE -> Greater Than or Equal To

Therefore, the third argument can contain one of the following values:

L
LE
G
GE

Fourth Argument

The fourth argument is a float64 comparison value.

For example:

4.0

The selected operator and this value together define the filtering condition.

Function

The function is named:

FindAll(...)

Conceptually, the function receives:

map[int][]float64
+
[]int containing selected keys
+
comparison operator
+
float64 comparison value

and returns:

map[int][]float64

Comparison Rules

The comparison operator determines which values should be returned.

L — Less Than

For:

operator = L
value    = 4.5

return values satisfying:

value < 4.5

LE — Less Than or Equal To

For:

operator = LE
value    = 4.5

return values satisfying:

value <= 4.5

G — Greater Than

For:

operator = G
value    = 4.5

return values satisfying:

value > 4.5

GE — Greater Than or Equal To

For:

operator = GE
value    = 4.5

return values satisfying:

value >= 4.5

Requirements

The function must process only the map keys specified in the second argument.

For every selected key:

  1. Find the corresponding entry in the input map.
  2. Iterate through its float64 values.
  3. Apply the comparison defined by the third and fourth arguments.
  4. Collect all values that satisfy the condition.
  5. Add the matching values to the output map under the same key.

The result must have the type:

map[int][]float64

The keys in the result correspond to the selected keys that were searched.

The values associated with each result key are the values that satisfied the selected comparison condition.

Example Data

Given:

m1 := map[int][]float64{
    1: {3.8, 4.6, 5.2},
    2: {2.2, 3.5, 4.9},
    3: {2.7, 3.1, 4.1},
}

Case 1

Call:

FindAll(m1, []int{1, 3}, "G", 4.0)

Only keys 1 and 3 are searched.

For key 1:

3.8 -> does not satisfy > 4.0
4.6 -> satisfies > 4.0
5.2 -> satisfies > 4.0

For key 3:

2.7 -> does not satisfy > 4.0
3.1 -> does not satisfy > 4.0
4.1 -> satisfies > 4.0

Expected Result

map[int][]float64{
    1: {4.6, 5.2},
    3: {4.1},
}

Case 2

Call:

FindAll(m1, []int{2, 3}, "G", 3.0)

For key 2:

2.2 -> does not satisfy > 3.0
3.5 -> satisfies > 3.0
4.9 -> satisfies > 3.0

For key 3:

2.7 -> does not satisfy > 3.0
3.1 -> satisfies > 3.0
4.1 -> satisfies > 3.0

Expected Result

map[int][]float64{
    2: {3.5, 4.9},
    3: {3.1, 4.1},
}

Case 3

Call:

FindAll(m1, []int{1, 2, 3}, "LE", 3.5)

For key 1:

3.8 -> does not satisfy <= 3.5
4.6 -> does not satisfy <= 3.5
5.2 -> does not satisfy <= 3.5

For key 2:

2.2 -> satisfies <= 3.5
3.5 -> satisfies <= 3.5
4.9 -> does not satisfy <= 3.5

For key 3:

2.7 -> satisfies <= 3.5
3.1 -> satisfies <= 3.5
4.1 -> does not satisfy <= 3.5

Expected Result from the Original Task

map[int][]float64{
    1: {},
    2: {2.2, 3.5},
    3: {2.7, 3.1},
}

Important Note About Empty Results

The original task description states that if a selected key does not contain any values satisfying the condition, that key should not be included in the output map.

However, the original third example includes:

1: {}

even though key 1 contains no values less than or equal to 3.5.

This creates a difference between the written rule and the provided example.

When implementing the task, decide which behavior will be followed and keep it consistent:

  • omit keys that have no matching values, according to the written requirement
  • or preserve selected keys with an empty slice, according to the third example

The original source does not further clarify which behavior takes priority.

Validation

Consider validation for:

  • unsupported comparison operators
  • requested keys that do not exist in the input map
  • empty key lists
  • empty value collections

The original task explicitly defines only the comparison operators:

L
LE
G
GE

Behavior for other operator values is not specified.

Implementation Notes

The implementation should work with other valid map[int][]float64 values, selected key collections, operators, and comparison values.

The function should not be implemented specifically around the example data.

Only keys listed in the second argument should be considered during filtering.

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