博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Go程序设计语言练习题-第4章(4.1-4.9)
阅读量:6566 次
发布时间:2019-06-24

本文共 5510 字,大约阅读时间需要 18 分钟。

4.1

package mainimport (    "crypto/sha256"    "fmt")func popCount(b [32]byte) (res int) {    for _, v := range b {        n := int(v)        for n > 0 {            n = n & (n - 1)            res++        }    }    return}func main() {    b := sha256.Sum256([]byte("x"))    fmt.Println(popCount(b))}
View Code

4.2

package mainimport (    "crypto/sha256"    "crypto/sha512"    "flag"    "fmt"    "os")var t = flag.Int("t", 2, "-t 2|3|5")func main() {    flag.Parse()    c := make([]byte, 1024)    n, err := os.Stdin.Read(c)    if err != nil {        fmt.Println(err)        return    }    switch *t {    case 3:        fmt.Printf("%x\n", sha512.Sum384(c[0:n]))    case 5:        fmt.Printf("%x\n", sha512.Sum512(c[0:n]))    default:        fmt.Printf("%x\n", sha256.Sum256(c[0:n]))    }}
View Code

4.3~4.7

package mainimport (    "fmt"    "unicode"    "unicode/utf8"    "unsafe")// 4.3 重写reverse函数,使用数组指针作为参数而不是slice// ?数组长度是类型的一部分func reverse(arr *[5]int) {    length := len(*arr)    for i, j := 0, length-1; i < j; i, j = i+1, j-1 {        (*arr)[i], (*arr)[j] = (*arr)[j], (*arr)[i]    }}// 4.4 编写函数rotate,实现一次遍历就可以完成元素旋转func rotate(s []int, n int) (t []int) {    if n <= 0 || n >= len(s) {        return s    }    t = make([]int, len(s))    for i, v := range s {        t[(i+n)%len(s)] = v    }    return}// rotate另一种思路:原地旋转,遍历两次func reverseSlice(s []int) {    for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {        s[i], s[j] = s[j], s[i]    }}func rotate2(s []int, n int) {    if n > len(s) {        fmt.Errorf("parameter n:%d is out of range(%d)", n, len(s))        return    }    reverseSlice(s[0 : len(s)-n])    reverseSlice(s[len(s)-n:])    reverseSlice(s)}// 4.5 编写一个就地处理函数,用于去除[]string slice中相邻重复字符串元素func distinct(s []string) []string {    dist := s[0:1]    for i := 1; i < len(s); i++ {        for s[i] != dist[len(dist)-1] {            dist = append(dist, s[i])        }    }    return dist}// 4.6 编写一个就地处理函数,用于将一个UTF-8编码的字节slice中所有相邻的Unicode空白字符(查看unicode.IsSpace)缩减为一个ASCII空白字符。func ex4_6(b []byte) []byte {    var i int    for i, l := 0, 0; l < len(b); {        r, size := utf8.DecodeRune(b[i:])        l += size        if unicode.IsSpace(r) {            if i > 0 && b[i-1] == byte(32) {                copy(b[i:], b[i+size:])            } else {                b[i] = byte(32)                copy(b[i+1:], b[i+size:])                i++            }        } else {            i += size        }    }    return b[0:i]}// 4.7 修改函数reverse,来反转一个UTF-8编码字符串中的字符元素,传入参数是该字符串对应的字节slice类型([]byte)。是否可以做到不重新分配内存就实现该功能。func mbreverse(b []byte) []byte {    var res []byte    for i := len(b); i > 0; {        r, size := utf8.DecodeLastRune(b[:i])        res = append(res, []byte(string(r))...)        i -= size    }    return res}func main() {    // 4.3    arr := [...]int{
1, 2, 3, 4, 5} reverse(&arr) fmt.Println(arr) // 4.4 r := []int{
1, 2, 3, 4, 5} fmt.Println(rotate(r, 0)) fmt.Println(rotate(r, 1)) fmt.Println(rotate(r, 2)) fmt.Println(rotate(r, 5)) fmt.Println(rotate(r, 6)) // 4.5 s := []string{
" ", " ", "aaa", " ", "bbb", "bbb", "c", "d", "d"} dist := distinct(s) fmt.Printf("原slice的底层数组地址:%p\n", unsafe.Pointer(uintptr(unsafe.Pointer(&s)))) fmt.Printf("原slice的len:%d\n", *(*int)(unsafe.Pointer(uintptr(unsafe.Pointer(&s)) + unsafe.Sizeof(&s)))) fmt.Printf("原slice的cap:%d\n", *(*int)(unsafe.Pointer(uintptr(unsafe.Pointer(&s)) + 2*unsafe.Sizeof(&s)))) fmt.Printf("去重后底层数组地址:%p\n", unsafe.Pointer(uintptr(unsafe.Pointer(&dist)))) fmt.Printf("len:%d\n", *(*int)(unsafe.Pointer(uintptr(unsafe.Pointer(&dist)) + unsafe.Sizeof(&dist)))) fmt.Printf("cap:%d\n", *(*int)(unsafe.Pointer(uintptr(unsafe.Pointer(&dist)) + 2*unsafe.Sizeof(&dist)))) // 4.6 b := []byte("北京\t欢迎\n您") fmt.Printf("%s\n%v\n", b, b) ex4_6(b) fmt.Printf("%s\n%v\n", b, b) // 4.7 b = []byte("北京欢迎您welcome") fmt.Printf("%s\n", b) fmt.Printf("%s\n", mbreverse(b))}
View Code

 4.8~4.9

package mainimport (    "bufio"    "fmt"    "io"    "os"    "unicode")func charcount() {    seen := make(map[rune]bool)    var letterCount, numberCount, otherCount, invalid int    in := bufio.NewReader(os.Stdin)    for {        r, n, err := in.ReadRune()        if err == io.EOF {            break        }        if err != nil {            fmt.Fprintln(os.Stderr, "charcount:%v\n", err)            os.Exit(1)        }        if r == unicode.ReplacementChar && n == 1 {            invalid++            continue        }        if unicode.IsLetter(r) && !seen[r] {            letterCount++            seen[r] = true            continue        }        if unicode.IsNumber(r) && !seen[r] {            numberCount++            seen[r] = true            continue        }        if !seen[r] {            otherCount++            seen[r] = true        }    }    fmt.Fprintf(os.Stdout, "letterCount:%d\tnumberCount:%d\totherCount:%d\tinvalidCount:%d\n", letterCount, numberCount, otherCount, invalid)}// 4.9func wordfreq() map[string]int {    scanner := bufio.NewScanner(os.Stdin)    scanner.Split(bufio.ScanWords)    words := make(map[string]int)    for scanner.Scan() {        words[scanner.Text()]++    }    if err := scanner.Err(); err != nil {        fmt.Fprintln(os.Stderr, err)    }    return words}func main() {    fmt.Println("ex4.8\n")    charcount()    fmt.Println("ex4.9 Word Frequency:")    words := wordfreq()    for w, n := range words {        fmt.Printf("%s\t%d\n", w, n)    }}
View Code

 

转载于:https://www.cnblogs.com/ling-diary/p/10408376.html

你可能感兴趣的文章
iPhone中png图片格式处理(附:linux下alias命令详解)
查看>>
Iphone开发问题集
查看>>
php实现一致性hash算法
查看>>
docker使用命令
查看>>
VGA、DVI、HDMI、DP 接口介绍及优劣
查看>>
从19本书中选取五本,并且要求这五本互相不相邻,一共有多少种方法?
查看>>
ubuntu16.04 caffe(GPU模式)安装
查看>>
批处理+VBS+注册表实现开机自动启动EXE程序
查看>>
Python爬虫之设置selenium webdriver等待
查看>>
测试用例编写思路
查看>>
R语言与Tableau集成之可视化应用
查看>>
嘿嘿,无聊的时候,来点好玩的,翻滚吧,杀马特!!!
查看>>
删除整数字符串删除k个数后最小的数
查看>>
Canvas学习:globalCompositeOperation详解
查看>>
一个简单的mud客户端(python 3.2)
查看>>
OAuth2.0 错误码
查看>>
NSIS隐藏窗体标题栏自带的button(最大化,最小化,关闭X)
查看>>
201521123076《Java程序设计》第1周学习总结
查看>>
python基础
查看>>
Python字符串方法
查看>>