38 lines
599 B
Go
38 lines
599 B
Go
package calories
|
|
|
|
import (
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
var (
|
|
caloriesMap = map[string]int{
|
|
"чай": 79,
|
|
"яблоко": 100,
|
|
}
|
|
)
|
|
|
|
func CalcCalories(text string) (int, error) {
|
|
count, ok := caloriesMap[strings.ToLower(text)]
|
|
if ok {
|
|
return count, nil
|
|
}
|
|
arr := strings.Split(text, " ")
|
|
if len(arr) == 2 {
|
|
coun1, err := strconv.Atoi(arr[0])
|
|
if err != nil {
|
|
return 0, nil
|
|
}
|
|
count2, err := strconv.Atoi(arr[1])
|
|
if err != nil {
|
|
return 0, nil
|
|
}
|
|
return coun1 * count2 / 100, nil
|
|
}
|
|
count, err := strconv.Atoi(text)
|
|
if err != nil {
|
|
return 0, nil
|
|
}
|
|
return count, nil
|
|
}
|