1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| func discountPrices(sentence string, discount int) string { strArr := strings.Split(sentence, " ") n := len(strArr) for i := 0; i < n; i++ { if checkMoney(strArr[i]) { num, _ := strconv.Atoi(strArr[i][1:]) strArr[i] = fmt.Sprintf("$%.2f", float64(num*100-num*discount)/100.0) } } return strings.Join(strArr, " ") }
func checkMoney(str string) bool { n := len(str) if n < 2 || str[0] != '$' { return false } for i := 1; i < n; i++ { if str[i] < '0' || str[i] > '9' { return false } } return true }
|