GO语言圣经 第五章习题

练习5.1

修改findlinks代码中遍历n.FirstChild链表的部分,将循环调用visit,改成递归调用。

go 复制代码
func visit(links []string, n *html.Node) []string {
	if n == nil {
		return links
	}
	if n.Type == html.ElementNode && n.Data == "a" {
		for _, a := range n.Attr {
			if a.Key == "href" {
				links = append(links, a.Val)
			}
		}
	}
	links = visit(links, n.NextSibling)
	links = visit(links, n.FirstChild)
	return links
}

练习5.2

编写函数,记录在HTML树中出现的同名元素的次数。

go 复制代码
package main

import (
	"fmt"
	"os"

	"golang.org/x/net/html"
)

type NodeCount map[string]int

func main() {
	doc, err := html.Parse(os.Stdin)
	if err != nil {
		fmt.Fprintf(os.Stderr, "findlinks1: %v\n", err)
		os.Exit(1)
	}
	nodeCount := NodeCount{}
	fill(&nodeCount, doc)
	fmt.Printf("%v", nodeCount)
}

func fill(nc *NodeCount, cn *html.Node) {
	if cn.Type == html.ElementNode {
		(*nc)[cn.Data]++
	}
	for next := cn.FirstChild; next != nil; next = next.NextSibling {
		fill(nc, next)
	}
}

练习5.3

编写函数输出所有text结点的内容。注意不要访问<script>和<style>元素,因为这些元素对浏览者是不可见的。

go 复制代码
func getText(texts []string, n *html.Node) []string {
	if n.Type == html.TextNode {
		texts = append(texts, n.Data)
	}
	for c := n.FirstChild; c != nil; c = c.NextSibling {
		if c.Data == "script" || c.Data == "style" {
			continue
		}
		texts = getText(texts, c)
	}
	return texts
}

练习5.4

扩展visit函数,使其能够处理其他类型的结点,如images、scripts和style sheets。

go 复制代码
func visit(links []string, n *html.Node) []string {
	if n.Type == html.ElementNode && (n.Data == "a" || n.Data == "img" || n.Data == "link" || n.Data == "scripts") {
		for _, a := range n.Attr {
			if a.Key == "href" {
				// fmt.Println(n.Data)
				links = append(links, a.Val)
			}
		}
	}
	for c := n.FirstChild; c != nil; c = c.NextSibling {
		links = visit(links, c)
	}
	return links
}

练习5.5

实现countWordsAndImages。(参考练习4.9如何分词)

go 复制代码
func countWordsAndImages(n *html.Node) (words, images int) {

	texts, images := visit(nil, 0, n)

	for _, v := range texts {
		words += len(strings.Split(v, " "))
		v = strings.Trim(strings.TrimSpace(v), "\r\n")
		if v == "" {
			continue
		}
		fmt.Println(strings.Split(v, " "))
		words += len(strings.Split(v, " "))
		fmt.Println(len(strings.Split(v, " ")))
	}
	return
}

func visit(texts []string, imgs int, n *html.Node) ([]string, int) {
	if n.Type == html.TextNode {
		texts = append(texts, n.Data)
	}
	if n.Type == html.ElementNode && (n.Data == "img") {
		imgs++
	}
	for c := n.FirstChild; c != nil; c = c.NextSibling {
		if c.Data == "script" || c.Data == "style" {
			continue
		}
		texts, imgs = visit(texts, imgs, c)
	}
	return texts, imgs
}

练习5.6

修改gopl.io/ch3/surface(§3.2)中的corner函数,将返回值命名,并使用bare return。

go 复制代码
func corner(i, j int) (sx float64, sy float64) {
    // Find point (x,y) at corner of cell (i,j).
    x := xyrange * (float64(i)/cells - 0.5)
    y := xyrange * (float64(j)/cells - 0.5)

    // Compute surface height z.
    z := f(x, y)

    // Project (x,y,z) isometrically onto 2-D SVG canvas (sx,sy).
    sx := width/2 + (x-y)*cos30*xyscale
    sy := height/2 + (x+y)*sin30*xyscale - z*zscale
    return
}
相关推荐
吃着火锅x唱着歌1 小时前
Redis设计与实现 学习笔记 第五章 跳跃表
golang
技术卷4 小时前
Redis数据库与GO完结篇:redis操作总结与GO使用redis
数据库·redis·golang
white.tie7 小时前
vscode配置golang
ide·vscode·golang
陈序缘7 小时前
Go语言实现长连接并发框架 - 任务管理器
linux·服务器·开发语言·后端·golang
0x派大星12 小时前
【Golang】语法基础——切片:灵活、高效的数据处理利器
golang
技术卷20 小时前
GO网络编程(二):客户端与服务端通信【重要】
golang·网络编程
小帅吖20 小时前
浅析Golang的Context
开发语言·后端·golang
MarisTang20 小时前
Go语言实现随机森林 (Random Forest)算法
算法·随机森林·golang
技术卷1 天前
Redis数据库与GO(二):list,set
数据库·redis·golang
__AtYou__1 天前
Golang | Leetcode Golang题解之第441题排列硬币
leetcode·golang·题解