Task 1 — String Pair Conversion
Objective
Create a function that receives a collection of string values and transforms pairs of values into a map[string]float64.
Every two consecutive elements in the input represent one pair:
- the first element represents a name
- the second element represents a decimal value
The name must become the map key, while the decimal value must be converted from string to float64 and stored as the value associated with that key.
Input
The input data is a collection of strings where every two consecutive elements form a pair.
Example:
inputData := []string{
"David", "9.20",
"Alex", "8.10",
"Max", "6.20",
"Ben", "7.50",
}
The pairs are therefore:
"David" -> "9.20"
"Alex" -> "8.10"
"Max" -> "6.20"
"Ben" -> "7.50"
Function
Create a function named:
CreateForm(...)
The function must process the provided string values and return:
map[string]float64
The exact implementation and function signature are left to the developer, provided that the required input can be accepted and the expected output is produced.
Requirements
For every pair of input values:
- Take the first value as the name.
- Use the name as a key in the resulting map.
- Take the second value as the decimal value.
- Convert the decimal value from
stringtofloat64. - Store the converted value under the corresponding name.
For example:
"David", "9.20"
must become:
"David" -> 9.20
The same operation must be performed for every pair in the input.
Example
Given:
inputData := []string{
"David", "9.20",
"Alex", "8.10",
"Max", "6.20",
"Ben", "7.50",
}
the function should transform the data into:
map[string]float64{
"David": 9.20,
"Alex": 8.10,
"Max": 6.20,
"Ben": 7.50,
}
Expected Result
map[string]float64{
"David": 9.20,
"Alex": 8.10,
"Max": 6.20,
"Ben": 7.50,
}
Validation
Pay particular attention to:
- input validation
- conversion of string values to
float64 - conversion errors
- invalid input data
The implementation should not assume that every provided decimal string can always be successfully converted.
Validation and error handling are part of the task.
Implementation Notes
The objective is to design the transformation rather than simply reproduce the example output.
The implementation should work with other valid name/value pairs following the same input format.
Consider how the function should behave when the input does not contain valid pairs or when a numeric value cannot be converted.
Do not optimize specifically for the example data.