目录
习题答案,个人理解,欢迎交流指正
package main
import (
"fmt"
"sort"
)
var dict = map[string][]string{
"algorithms": {"data structures"},
"calculus": {"linear algebra"},
"linear algebra": {"calculus"},
"compilers": {
"data structures",
"formal languages",
"computer organization",
},
"data structures": {"discrete math"},
"databases": {"data structures"},
"discrete math": {"intro to programming"},
"formal languages": {"discrete math"},
"networks": {"operating systems"},
"operating systems": {"data structures", "computer organization"},
"programming languages": {"data structures", "computer organization"},
}
func main() {
list, ok := toposort(dict)
if !ok {
fmt.Println("存在环")
return
}
for i, s := range list {
fmt.Printf("%d\t%s\n", i+1, s)
}
}
func toposort(dict map[string][]string) ([]string, bool) {
var list []string
seen := make(map[string]bool)
wait := make(map[string]bool)
var visitAll func(items []string) bool
visitAll = func(items []string) bool {
for _, item := range items {
if !seen[item] {
seen[item] = true
wait[item] = true
if !visitAll(dict[item]) {
return false
}
list = append(list, item)
wait[item] = false
} else if wait[item] {
return false
}
}
return true
}
var keys []string
for key := range dict {
keys = append(keys, key)
}
sort.Strings(keys)
if !visitAll(keys) {
return nil, false
}
return list, true
}
package main
import (
"fmt"
"io/ioutil"
"net/http"
"strings"
neturl "net/url"
"os"
"golang.org/x/net/html"
"gopl.io/util"
)
func Extract(url string, hosts map[string]bool) ([]string, error) {
resp, err := http.Get(url)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
resp.Body.Close()
return nil, fmt.Errorf("getting %s: %s", url, resp.Status)
}
content, _ := ioutil.ReadAll(resp.Body)
contentstr := string(content)
doc, err := html.Parse(strings.NewReader(contentstr))
resp.Body.Close()
if err != nil {
return nil, fmt.Errorf("parsing %s as HTML: %v", url, err)
}
var links []string
visitNode := func(n *html.Node) {
u, err := neturl.Parse(url)
if err != nil {
fmt.Printf("解析路径%s失败 %s\n", url, err)
}
dir := u.Host
if n.Type == html.ElementNode && n.Data == "title" && hosts[dir] {
name := n.FirstChild.Data
mkErr := os.Mkdir(dir, os.ModePerm)
if mkErr != nil && os.IsNotExist(mkErr) {
fmt.Printf("创建文件夹[%s]失败 %s\n", dir, mkErr)
}
fn := dir + "/" + name + ".html"
file, err := os.OpenFile(fn, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
if err != nil {
fmt.Printf("创建文件[%s]失败 %s\n", fn, err)
} else {
fmt.Printf("保存文件 %s\n", fn)
file.WriteString(contentstr)
file.Close()
}
}
if n.Type == html.ElementNode && n.Data == "a" {
for _, a := range n.Attr {
if a.Key != "href" {
continue
}
link, err := resp.Request.URL.Parse(a.Val)
if err != nil {
continue
}
links = append(links, link.String())
}
}
}
util.ForEachNode(doc, visitNode, nil)
return links, nil
}
package main
import (
"fmt"
"log"
"net/url"
"os"
"strings"
)
func main() {
urls := os.Args[1:]
seen := map[string]bool{}
toVisit := []string{}
toVisit = append(toVisit, urls...)
hosts := map[string]bool{}
for _, u := range urls {
uu, err := url.Parse(u)
if err == nil {
hosts[uu.Host] = true
}
}
for len(toVisit) != 0 {
links := toVisit
toVisit = nil
for _, link := range links {
if strings.HasSuffix(link, "/") {
link = link[:len(link)-1]
}
if !seen[link] {
fmt.Println(link)
seen[link] = true
l, err := Extract(link, hosts)
if err != nil {
log.Print(err)
}
toVisit = append(toVisit, l...)
}
}
}
}
package main
import (
"fmt"
"io/ioutil"
"os"
)
func Bfs(f func(item string) []string, worklist []string) {
seen := make(map[string]bool)
for len(worklist) > 0 {
items := worklist
worklist = nil
for _, item := range items {
if !seen[item] {
seen[item] = true
worklist = append(worklist, f(item)...)
}
}
}
}
func crawlCourse() {
var dict = map[string][]string{
"algorithms": {"data structures"},
"calculus": {"linear algebra"},
"linear algebra": {"calculus"},
"compilers": {
"data structures",
"formal languages",
"computer organization",
},
"data structures": {"discrete math"},
"databases": {"data structures"},
"discrete math": {"intro to programming"},
"formal languages": {"discrete math"},
"networks": {"operating systems"},
"operating systems": {"data structures", "computer organization"},
"programming languages": {"data structures", "computer organization"},
}
f := func(s string) []string {
fmt.Println(s)
return dict[s]
}
items := []string{}
for k, _ := range dict {
items = append(items, k)
}
Bfs(f, items)
}
func crawlFile() {
root := []string{"gopl.io"}
f := func(path string) []string {
s, _ := os.Stat(path)
if !s.IsDir() {
fmt.Println(path)
return nil
}
rd, err := ioutil.ReadDir(path)
if err != nil {
fmt.Printf("read dir[%s] faild %s\n", path, err)
return nil
}
files := []string{}
for _, fi := range rd {
files = append(files, path+"/"+fi.Name())
}
return files
}
Bfs(f, root)
}
func main() {
crawlCourse()
crawlFile()
}
package main
import (
"fmt"
"math"
)
func main() {
fmt.Println(max())
fmt.Println(max(4))
fmt.Println(max(8, 4, 9))
}
func max(vals ...int) int {
if len(vals) == 0 {
return math.MaxInt
}
m := math.MinInt
for _, n := range vals {
if n > m {
m = n
}
}
return m
}
package main
import (
"fmt"
)
func main() {
fmt.Println(max(4))
fmt.Println(max(8, 4, 9))
}
func max(val int, vals ...int) int {
for _, n := range vals {
if n > val {
val = n
}
}
return val
}
package main
import (
"bytes"
"fmt"
)
func main() {
fmt.Println(join(",", []string{"wewei"}...))
fmt.Println(join(",", []string{}...))
fmt.Println(join("", []string{"weiwei", "zheng"}...))
}
func join(sep string, strs ...string) string {
var buf bytes.Buffer
s := ""
for _, str := range strs {
buf.WriteString(s)
buf.WriteString(str)
s = sep
}
return buf.String()
}
package main
import (
"fmt"
"net/http"
"os"
"golang.org/x/net/html"
)
func main() {
resp, err := http.Get("https://www.hao123.com")
if err != nil {
fmt.Fprintf(os.Stderr, "echo: %v\n", err)
os.Exit(1)
}
defer resp.Body.Close()
doc, err := html.Parse(resp.Body)
if err != nil {
fmt.Fprintf(os.Stderr, "echo: %v\n", err)
os.Exit(1)
}
nodes1 := ElementsByTagName(doc, "div")
nodes2 := ElementsByTagName(doc, "a")
nodes3 := ElementsByTagName(doc, "div", "a")
fmt.Printf("%d, %d, %d\n", len(nodes1), len(nodes2), len(nodes3))
}
func ElementsByTagName(doc *html.Node, names ...string) []*html.Node {
tags := make(map[string]bool)
for _, n := range names {
tags[n] = true
}
var list []*html.Node
var forEachNode func(doc *html.Node)
forEachNode = func(doc *html.Node) {
if doc.Type == html.ElementNode && tags[doc.Data] {
list = append(list, doc)
}
for c := doc.FirstChild; c != nil; c = c.NextSibling {
forEachNode(c)
}
}
forEachNode(doc)
return list
}
package main
import (
"fmt"
)
func main() {
fmt.Println(f())
}
func f() (code int) {
defer func() {
if p := recover(); p != nil {
code = 1
}
}()
panic("")
}