ch3/ch3-05 #196
Replies: 8 comments 1 reply
-
3.10 func comma(s string) string {
var buf bytes.Buffer
for i := 0; i < len(s); i++ {
if i > 0 && (len(s)-i)%3 == 0 {
buf.WriteByte(',')
}
buf.WriteByte(s[i])
}
return buf.String()
} |
Beta Was this translation helpful? Give feedback.
-
3.11 func comma(s string) string {
var buf bytes.Buffer
// 如果以 +/- 号开头,设置 start 为 1,将 +/- 号写入 buf
var start int
if s[0] == '+' || s[0] == '-' {
start = 1
buf.WriteByte(s[0])
} else {
start = 0
}
// 获取小数点的索引,根据 start 和 end 获取整数部分
end := strings.Index(s, ".")
ints := s[start:end]
// 对整数部分进行 comma 操作
for i := 0; i < len(ints); i++ {
if i > 0 && (len(ints)-i)%3 == 0 {
buf.WriteByte(',')
}
buf.WriteByte(ints[i])
}
// 拼接小数点及之后的部分
rest := s[end:]
for i := 0; i < len(rest); i++ {
buf.WriteByte(rest[i])
}
return buf.String()
} |
Beta Was this translation helpful? Give feedback.
-
大体上应该是对的 // 3.12
func isEqualOutOfOrder(s1 string, s2 string) bool {
var mp = make(map[rune]int)
for _, c := range s1 {
mp[c]++
}
for _, c := range s2 {
mp[c]--
}
for _, v := range mp {
if v != 0 {
return false
}
}
return true
}
// 3.10
func comma(s string) string {
split := strings.Split(s, ".")
var sz = split[0]
var sf = split[1]
var sNew = bytes.Buffer{}
for i := 0; i < len(sz); i++ {
sNew.WriteByte(sz[i])
if i%3 == 0 {
sNew.WriteByte(',')
}
}
bz := getSZ(sz)
bf := getSF(sf)
return fmt.Sprintf("%s.%s\n", bz.Bytes(), bf.Bytes())
}
func getSF(sf string) bytes.Buffer {
buffer := bytes.Buffer{}
for i := 0; i < len(sf); i++ {
if i%3 == 0 && i != 0 {
buffer.WriteByte(',')
}
buffer.WriteByte(sf[i])
}
return buffer
}
func getSZ(sz string) bytes.Buffer {
nz := len(sz)
buffer := bytes.Buffer{}
var i int = 0
for i = 0; i < nz%3; i++ {
buffer.WriteByte(sz[i])
}
for ; i < nz; i++ {
if (i-nz%3)%3 == 0 {
buffer.WriteByte(',')
}
buffer.WriteByte(sz[i])
}
return buffer
} |
Beta Was this translation helpful? Give feedback.
-
没有什么是循环解决不了的 package main
import (
"fmt"
"bytes"
"strings"
)
func main() {
fmt.Println(comma("+9909999.999"))
fmt.Println(commaBuf("9999999"))
}
func comma(s string) string {
n := len(s)
if n <= 3 {
return s
}
add := strings.LastIndex(s, "+")
min := strings.LastIndex(s, "-")
if add == 0 {
return "+" + comma(s[add+1:])
}
if min == 0 {
return "-" + comma(s[add+1:])
}
if dot := strings.LastIndex(s, "."); dot >= 0 {
return comma(s[:dot]) + "." + comma(s[dot+1:])
}
return comma(s[:n-3]) + "," + s[n-3:]
}
func commaBuf(s string) string {
n := len(s)
if n <= 3 {
return s
}
var buf bytes.Buffer
for i := n - 1;i >= 0;i-- {
if i > 0 && i % 3 == 0 {
buf.WriteByte(s[i])
buf.WriteString(",")
} else {
buf.WriteByte(s[i])
}
}
return buf.String()
} |
Beta Was this translation helpful? Give feedback.
-
//3.10 + 3.11
func comma(s string) string {
var buf bytes.Buffer
var s1, s2 string
var dotIdx int = strings.Index(s, ".")
if dotIdx == -1 {
var sgn, _ = utf8.DecodeRuneInString(s)
if sgn == '+' || sgn == '-' {
s = s[1:]
buf.WriteRune(sgn)
}
for i, ch := range s {
buf.WriteRune(ch)
if (len(s)-1-i)%3 == 0 && i != len(s)-1 {
buf.WriteRune(',')
}
}
return buf.String()
}
var sgn, _ = utf8.DecodeRuneInString(s)
if sgn == '+' || sgn == '-' {
s1 = s[1:dotIdx]
buf.WriteRune(sgn)
} else {
s1 = s[:dotIdx]
}
s2 = s[dotIdx+1:]
for i, ch := range s1 {
buf.WriteRune(ch)
if (len(s1)-1-i)%3 == 0 && i != len(s1)-1 {
buf.WriteRune(',')
}
}
buf.WriteRune('.')
for i, ch := range s2 {
if i%3 == 0 && i != 0 {
buf.WriteRune(',')
}
buf.WriteRune(ch)
}
return buf.String()
}
//3.12
func strMixed(s1, s2 string) bool {
var mp map[rune]int = make(map[rune]int)
for _, ch := range s1 {
mp[ch]++
}
for _, ch := range s2 {
mp[ch]--
}
for _, cnt := range mp {
if cnt != 0 {
return false
}
}
return true
} |
Beta Was this translation helpful? Give feedback.
-
3.10
|
Beta Was this translation helpful? Give feedback.
-
package comma // comma inserts commas in a non-negative decimal integer string. |
Beta Was this translation helpful? Give feedback.
-
func Comma1(s string) string { |
Beta Was this translation helpful? Give feedback.
-
ch3/ch3-05
中文版
https://gopl-zh.github.io/ch3/ch3-05.html
Beta Was this translation helpful? Give feedback.
All reactions