当前位置: 首页 > news >正文

GO语言圣经 第四章习题

练习4.1

编写一个函数,计算两个SHA256哈希码中不同bit的数目。(参考2.6.2节的PopCount函数。)

func PopCount(ptr *[32]byte) int {var res intfor i := 0; i < 32; i++ {x := int(ptr[i])for x != 0 {res += x & 1x >>= 1}}return res
}

练习4.2

编写一个程序,默认情况下打印标准输入的SHA256编码,并支持通过命令行flag定制,输出SHA384或SHA512哈希算法。

package mainimport ("bufio""crypto/sha256" //!+"crypto/sha512""flag""fmt""os"
)var n = flag.Int("n", 256, "SHA param")func main() {flag.Parse()var s stringinput := bufio.NewScanner(os.Stdin)for input.Scan() {s += input.Text()}switch *n {case 256:fmt.Printf("%x", sha256.Sum256([]byte(s)))case 384:fmt.Printf("%x", sha512.Sum384([]byte(s)))case 512:fmt.Printf("%x", sha512.Sum512([]byte(s)))}
}

练习4.3

重写reverse函数,使用数组指针代替slice。

func reverse(s *[6]int) {for i, j := 0, len(*s)-1; i < j; i, j = i+1, j-1 {s[i], s[j] = s[j], s[i]}
}

练习4.4

编写一个rotate函数,通过一次循环完成旋转。

func rotate(s []int, ind int) {out := make([]int, cap(s)-ind)copy(out, s[ind:])for i := 0; i < ind; i++ {out = append(out, s[i])}copy(s, out)
}

练习4.5

写一个函数在原地完成消除[]string中相邻重复的字符串的操作。

func nonrepeat(strings []string) []string {i := 0for j := range strings {if j != 0 && strings[j] == strings[j-1] {continue} else {strings[i] = strings[j]i++}}return strings[:i]
}

练习4.6

编写一个函数,原地将一个UTF-8编码的[]byte类型的slice中相邻的空格(参考unicode.IsSpace)替换成一个空格返回

func nonrepeat(s []byte) []byte {for j := 0; j < len(s); j++ {if j != 0 && unicode.IsSpace(rune(s[j])) && s[j] == s[j-1] {copy(s[j-1:], s[j:])s = s[:len(s)-1]j--}}return s
}

练习4.7

修改reverse函数用于原地反转UTF-8编码的[]byte。是否可以不用分配额外的内存?

不能不分配额外的内存,byte需要转为rune,否则会出现乱码

func reverse(s []byte) []rune {r := []rune(string(s))for i, j := 0, len(r)-1; i < j; i, j = i+1, j-1 {r[i], r[j] = r[j], r[i]}return r
}

练习4.8

修改charcount程序,使用unicode.IsLetter等相关的函数,统计字母、数字等Unicode中不同的字符类别。

package mainimport ("bufio""fmt""io""os""unicode""unicode/utf8"
)func main() {counts := make(map[rune]int)    // counts of Unicode charactersvar utflen [utf8.UTFMax + 1]int // count of lengths of UTF-8 encodingsinvalid := 0                    // count of invalid UTF-8 charactersvar utftype [3]intin := bufio.NewReader(os.Stdin)for {r, n, err := in.ReadRune() // returns rune, nbytes, errorif err == io.EOF {break}if err != nil {fmt.Fprintf(os.Stderr, "charcount: %v\n", err)os.Exit(1)}if r == unicode.ReplacementChar && n == 1 {invalid++continue}switch {case unicode.IsLetter(r):utftype[0]++case unicode.IsDigit(r):utftype[1]++default:utftype[2]++}counts[r]++utflen[n]++}fmt.Printf("rune\tcount\n")for c, n := range counts {fmt.Printf("%q\t%d\n", c, n)}fmt.Print("\nlen\tcount\n")for i, n := range utflen {if i > 0 {fmt.Printf("%d\t%d\n", i, n)}}fmt.Print("\ntype\tcount\n")for i, n := range utftype {switch i {case 0:fmt.Print("letter:")case 1:fmt.Print("digit:")case 2:fmt.Print("other:")}fmt.Println(n)}if invalid > 0 {fmt.Printf("\n%d invalid UTF-8 characters\n", invalid)}
}

练习4.9

编写一个程序wordfreq程序,报告输入文本中每个单词出现的频率。在第一次调用Scan前先调用input.Split(bufio.ScanWords)函数,这样可以按单词而不是按行输入。

func wordfreq() {counts := make(map[string]int)in := bufio.NewScanner(os.Stdin)in.Split(bufio.ScanWords)for in.Scan() {counts[in.Text()]++}for k, v := range counts {fmt.Println("%s %d\n", k, v)}
}

练习4.10

修改issues程序,根据问题的时间进行分类,比如不到一个月的、不到一年的、超过一年。

package mainimport ("fmt""log""os""time""gopl.io/ch4/github"
)func main() {//now 为现在的时间,yearAgo 为距现在一年的时间,monthAgo 为距现在一月的时间。now := time.Now()yearAgo := now.AddDate(-1, 0, 0)monthAgo := now.AddDate(0, -1, 0)//三个切片,用来存储 不足一个月的问题,不足一年的问题,超过一年的问题。yearAgos := make([]*github.Issue, 0)monthAgos := make([]*github.Issue, 0)noMonthAgos := make([]*github.Issue, 0)result, err := github.SearchIssues(os.Args[1:])if err != nil {log.Fatal(err)}fmt.Printf("%d issues:\n", result.TotalCount)for _, item := range result.Items {if yearAgo.After(item.CreatedAt) {yearAgos = append(yearAgos, item)} else if monthAgo.After(item.CreatedAt) {monthAgos = append(monthAgos, item)} else if monthAgo.Before(item.CreatedAt) {noMonthAgos = append(noMonthAgos, item)}}fmt.Printf("\n一年前\n")for _, item := range yearAgos {fmt.Printf("#%-5d %9.9s %.55s\n",item.Number, item.User.Login, item.Title)}fmt.Printf("\n一月前\n")for _, item := range monthAgos {fmt.Printf("#%-5d %9.9s %.55s\n",item.Number, item.User.Login, item.Title)}fmt.Printf("\n不足一月\n")for _, item := range noMonthAgos {fmt.Printf("#%-5d %9.9s %.55s\n",item.Number, item.User.Login, item.Title)}
}
http://www.lryc.cn/news/150935.html

相关文章:

  • 远程连接Ubuntu 22.04
  • 字节前端实习的两道算法题,看看强度如何
  • 设计模式—策略模式
  • LPDDR4、DDR4
  • ESP32C3 LuatOS RC522①写入数据并读取M1卡
  • MusicBrainz Picard for Mac :音乐文件ID3编辑器
  • ❤ Uniapp使用
  • 解密Spring事务生效的内部机制
  • 大数据时代下的数据安全防护
  • RabbitMQ-常用命令
  • Spring中依赖注入的继承bean的细节问题
  • 海外腾讯云服务器手机上无法访问外网怎么办??
  • python3+requests:接口自动化测试(二)
  • uni-app:允许字符间能自动换行(英文字符、数字等)
  • day 42 |● 121. 买卖股票的最佳时机 ● 122.买卖股票的最佳时机II
  • SQLserver基础入门理论(超基础)
  • (三)行为模式:7、观察者模式(Observer Pattern)(C++示例)
  • 2019CVPR Semantic Graph Convolutional Networks for 3D Human Pose Regression
  • 大数据课程K16——Spark的梯度下降法
  • springboot:时间格式化的5种方法(解决后端传给前端的时间格式转换问题)推荐使用第4和第5种!
  • 六、vim编辑器的使用
  • 【易售小程序项目】项目介绍与系列文章集合
  • 游戏服务器成DDoS最大攻击重灾区
  • [SpringBoot3]博客管理系统(源码放评论区了)
  • C语言——指针基本语法
  • elementui table 在浏览器分辨率变化的时候界面异常
  • 六、Kafka-Eagle监控
  • DBeaver 23.1.5 发布
  • 三种垃圾收集算法,优缺点分析,设计垃圾收集
  • 【链表OJ 10】环形链表Ⅱ(求入环节点)