• [日常] Go语言*--Map习题


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

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

    // Charcount computes counts of Unicode characters.
    package main
    
    import (
        "bufio"
        "fmt"
        "io"
        "os"
        "unicode"
        "unicode/utf8"
    )
    
    func main() {
        //counts := make(map[string]map[rune]int)    // counts of Unicode characters
        var utflen [utf8.UTFMax + 1]int // count of lengths of UTF-8 encodings
        invalid := 0                    // count of invalid UTF-8 characters
    
        letters := make(map[rune]int)
        numbers := make(map[rune]int)
        in := bufio.NewReader(os.Stdin)
        for {
            r, n, err := in.ReadRune() // returns rune, nbytes, error
            if err == io.EOF {
                break
            }
            if err != nil {
                fmt.Fprintf(os.Stderr, "charcount: %v
    ", err)
                os.Exit(1)
            }
            if r == unicode.ReplacementChar && n == 1 {
                invalid++
                continue
            }
            /*
            练习 4.8: 修改charcount程序,使用unicode.IsLetter等相关的函数,统计字母、数字等Unicode中不同的字符类别。
            */
            //判断是字母
            if unicode.IsLetter(r){ 
                    letters[r]++
            }
            //判断是数字
            if unicode.IsNumber(r){
                    numbers[r]++
            }
            //counts[r]++
            utflen[n]++
        }
        fmt.Printf("rune	count
    ")
        for c, n := range letters {
            fmt.Printf("%q	%d
    ", c, n)
        }
        fmt.Printf("rune(number)	count
    ")
        for c, n := range numbers {
            fmt.Printf("%q	%d
    ", c, n)
        }
    
        fmt.Print("
    len	count
    ")
        for i, n := range utflen {
            if i > 0 {
                fmt.Printf("%d	%d
    ", i, n)
            }
        }
        if invalid > 0 {
            fmt.Printf("
    %d invalid UTF-8 characters
    ", invalid)
        }
    }
    
    package main
    
    import (
        "bufio"
        "fmt"
        //"io"
        "os"
        //"unicode"
        //"unicode/utf8"
    )
    
    func main() {
            counts := make(map[string]int)
            input := bufio.NewScanner(os.Stdin)
            input.Split(bufio.ScanWords)
            for input.Scan(){
                    counts[input.Text()]++
            }   
            for k,v :=range counts{
                    fmt.Printf("%s == %d 
    ",k,v)
            }   
    }
    /*
    练习 4.9: 编写一个程序wordfreq程序,报告输入文本中每个单词出现的频率。在第一次调用Scan前先调用input.Split(bufio.ScanWords)函数,这样可以按单词而不是按行输入。
    */
    

      

      

  • 相关阅读:
    TensorFlow(十五):使用inception-v3实现各种图像识别
    TensorFlow(十四):谷歌图像识别网络inception-v3下载与查看结构
    TensorFlow(十三):模型的保存与载入
    TensorFlow(十二):使用RNN实现手写数字识别
    TensorFlow(十一):递归神经网络(RNN与LSTM)
    TensorFlow(十):卷积神经网络实现手写数字识别以及可视化
    ffmpeg-20160628-git-bin.7z
    ffmpeg-20160617-git-bin.7z ffmpeg-20160626-git-bin.7z
    欧洲杯 2016 高清直播
    YY 神曲 李明霖 14部合集
  • 原文地址:https://www.cnblogs.com/taoshihan/p/8806446.html
Copyright © 2020-2023  润新知