ch1/ch1-03 #138
Replies: 30 comments 8 replies
-
dup3 处理存在一些缺陷。 b.txt的内容: 执行go run .\dup3.go a.txt b.txt, 无输出 |
Beta Was this translation helpful? Give feedback.
-
修改后的dup2提示,在map中多加一个文件名属性 |
Beta Was this translation helpful? Give feedback.
-
练习1.4,构造新的map存储每一行对应的文件名称 func testDup2() {
counts := make(map[string]int)
line2file := make(map[string]string)
files := os.Args[1:]
if len(files) == 0 {
fmt.Println("please input one or more file names")
} else {
for _, arg := range files {
f, err := os.Open(arg)
if err != nil {
fmt.Fprintf(os.Stderr, "dup2: %v\n", err)
continue
}
countLines(f, counts, line2file)
f.Close()
}
}
for line, n := range counts {
if n > 1 {
fmt.Printf("%d\t%s\t%s\n", n, line, line2file[line])
}
}
}
func countLines(f *os.File, counts map[string]int, line2file map[string]string) {
input := bufio.NewScanner(f)
for input.Scan() {
counts[input.Text()]++
line2file[input.Text()] = f.Name()
}
} |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
dup3在windows中可能会出现问题,由于windows中的换行默认使用的是
|
Beta Was this translation helpful? Give feedback.
-
练习1.4:
|
Beta Was this translation helpful? Give feedback.
-
Exercise 1.4 support show filenames when duplicate lines are in multiple files. package main
import (
"bufio"
"fmt"
"os"
"strings"
)
func main() {
counts := make(map[string]int)
fnmap := make(map[string]string)
files := os.Args[1:]
if len(files) == 0 {
countLines(os.Stdin, counts, fnmap)
} else {
for _, arg := range files {
f, err := os.Open(arg)
if err != nil {
fmt.Fprintf(os.Stderr, "dup2: %v\n", err)
continue
}
countLines(f, counts, fnmap)
f.Close()
}
}
for line, n := range counts {
if n > 1 {
fmt.Printf("%d\t%s\t%s\n", n, line, fnmap[line])
}
}
}
func countLines(f *os.File, counts map[string]int, line2file map[string]string) {
input := bufio.NewScanner(f)
for input.Scan() {
counts[input.Text()]++
if _, ok := line2file[input.Text()]; ok {
if !strings.Contains(line2file[input.Text()], f.Name()) {
line2file[input.Text()] += " " + f.Name()
}
} else {
line2file[input.Text()] = f.Name()
}
}
} |
Beta Was this translation helpful? Give feedback.
-
练习 1.4:
|
Beta Was this translation helpful? Give feedback.
-
// Dup1 prints the text of each line that appears more than
// once in the standard input, preceded by its count.
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
counts := make(map[string]int)
input := bufio.NewScanner(os.Stdin)
for input.Scan() {
counts[input.Text()]++
}
// NOTE: ignoring potential errors from input.Err()
for line, n := range counts {
if n > 1 {
fmt.Printf("%d\t%s\n", n, line)
}
}
} 这段代码要如何结束输入? |
Beta Was this translation helpful? Give feedback.
-
练习1.4 在输出重复行时打印文件名。 package main
import (
"bufio"
"fmt"
"os"
)
func main() {
counts := make(map[string]int)
files := os.Args[1:]
if len(files) == 0 {
countLines(os.Stdin,counts,"stdin")
}else {
for _,arg := range files {
f,err := os.Open(arg)
if err != nil {
fmt.Fprintf(os.Stderr,"dup2: %v\n",err)
continue
}
countLines(f,counts,arg)
f.Close()
}
}
for line,n := range counts {
if n > 1 {
fmt.Printf("%d\t%s\n",n, line)
}
}
}
func countLines(f *os.File,counts map[string]int,arg string) {
input := bufio.NewScanner(f)
for input.Scan() {
counts[input.Text()+" "+arg]++
}
} |
Beta Was this translation helpful? Give feedback.
-
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
counts := make(map[string]int)
files := os.Args[1:]
if len(files) == 0 {
countLines(os.Stdin, counts, "stdin")
} else {
for _, arg := range files {
f, err := os.Open(arg)
if err != nil {
fmt.Fprintf(os.Stderr, "dup2: %v\n", err)
continue
}
countLines(f, counts, arg)
f.Close()
}
}
for line, n := range counts {
if n > 1 {
/
fmt.Printf("%d\t%s\n", n, line)
}
}
}
func countLines(f *os.File, counts map[string]int, fileName string) {
input := bufio.NewScanner(f)
for input.Scan() {
counts[input.Text()]++
if counts[input.Text()] > 1 && fileName != "stdin" {
fmt.Printf("%s: %s\n", fileName, input.Text())
}
}
} |
Beta Was this translation helpful? Give feedback.
-
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
func main() {
//dup1()
//dup2()
dup3()
}
func dup3() {
counts := make(map[string]int)
for _, filename := range os.Args[1:] {
data, err := os.ReadFile(filename)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "dup3: %v\n", err)
continue
}
for _, line := range strings.Split(string(data), " ") {
counts[line]++
}
}
countPrint(counts)
}
func dup2() {
counts := make(map[string]int)
files := os.Args[1:]
if len(files) == 0 {
countLines(os.Stdin, counts)
} else {
for _, arg := range files {
f, err := os.Open(arg)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "dup: %v\n", err)
continue
}
countLines(f, counts)
_ = f.Close()
}
}
countPrint(counts)
}
func countLines(f *os.File, counts map[string]int) {
input := bufio.NewScanner(f)
fmt.Println("Please enter: ")
for input.Scan() {
line := input.Text()
counts[line]++
}
}
func countPrint(counts map[string]int) {
for line, n := range counts {
if n > 1 {
fmt.Printf("k: %s\tv: %d\n", line, n)
}
}
}
func dup1() {
counts := make(map[string]int)
input := bufio.NewScanner(os.Stdin)
fmt.Println("Please enter: ")
for input.Scan() {
text := input.Text()
counts[text]++
if text == "exit" {
break
}
}
countPrint(counts)
} |
Beta Was this translation helpful? Give feedback.
-
package main import ( func main() { func countLines2(f *os.File, counts map[string]int) { |
Beta Was this translation helpful? Give feedback.
-
ioutil.ReadFile is deprecated: As of Go 1.16, this function simply calls [os.ReadFile]. |
Beta Was this translation helpful? Give feedback.
-
练习
|
Beta Was this translation helpful? Give feedback.
-
package main import ( func main() { |
Beta Was this translation helpful? Give feedback.
-
package main
import (
"fmt"
"os"
"strings"
)
func main() {
counts := make(map[string]int)
for _, file := range os.Args[1:] {
if data, err := os.ReadFile(file); err == nil {
for _, line := range strings.Split(string(data), "\n") {
counts[file + ":" + line]++
}
} else {
fmt.Fprintf(os.Stderr, "dup3: %v\n", err)
continue
}
}
for file_line, value := range counts {
if value > 1 {
result := strings.SplitN(file_line, ":", 2)
fmt.Printf("%s\t%d\t%s\n", result[0], value, result[1])
}
}
} |
Beta Was this translation helpful? Give feedback.
-
顺带一提,ioutils.ReadFile从Go 1.16开始已经被弃用,请改用os.ReadFile |
Beta Was this translation helpful? Give feedback.
-
练习: func dup2() {
counts := make(map[string][]string)
files := os.Args[1:]
if len(files) == 0 {
countLines(os.Stdin, counts)
} else {
for _, arg := range files {
f, err := os.Open(arg)
if err != nil {
fmt.Fprintf(os.Stderr, "dup2: %v\n", err)
continue
}
countLines(f, counts)
f.Close()
}
}
for line, n := range counts {
if len(n) > 1 {
//如果打印的文件名嫌有重复的就写个函数,用map记录下打印过的
fmt.Printf("%v\t%d\t%s\n", n, len(n), line)
}
}
}
func countLines(f *os.File, counts map[string][]string) {
input := bufio.NewScanner(f)
for input.Scan() {
var slice = counts[input.Text()]
if slice == nil {
slice = []string{f.Name()}
} else {
slice = append(slice, f.Name())
}
counts[input.Text()] = slice
}
} |
Beta Was this translation helpful? Give feedback.
-
package main import ( func main() { func countLines(f *os.File, counts map[string]int, filename string) { |
Beta Was this translation helpful? Give feedback.
-
// Dup2 prints the count and text of lines that appear more than once import ( func main() { func countLines(f *os.File, counts map[string]int) { |
Beta Was this translation helpful? Give feedback.
-
func main() {
} func countLines(f *os.File, counts map[string]int) { |
Beta Was this translation helpful? Give feedback.
-
// Dup2 prints the count and text of lines that appear more than once import ( func main() {
} func countLines(f *os.File, counts map[string]int, map_line2file map[string][]string) {
} // 判断字符数组中是否有某个元素 |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
package main import ( type Counts struct { func main() {
} |
Beta Was this translation helpful? Give feedback.
-
package main
import (
"fmt"
"os"
"strings"
)
type Counts struct {
linecounts int
filecounts map[string]int
}
func main() {
dup := make(map[string]*Counts)
for _, filename := range os.Args[1:] {
data, err := os.ReadFile(filename)
if err != nil {
fmt.Fprintf(os.Stderr, "dup3: %v\n", err)
continue
}
for _, line := range strings.Split(string(data), "\n") {
_, exists := dup[line]
if !exists {
dup[line] = &Counts{
linecounts: 0,
filecounts: make(map[string]int),
}
}
dup[line].linecounts++
dup[line].filecounts[filename]++
}
}
for line, ct := range dup {
if ct.linecounts > 1 {
fmt.Printf("%d\t%s\n\tfilenames: %v\n", ct.linecounts, line, ct.filecounts)
}
}
} |
Beta Was this translation helpful? Give feedback.
-
/** |
Beta Was this translation helpful? Give feedback.
-
ch1/ch1-03
中文版
https://gopl-zh.github.io/ch1/ch1-03.html
Beta Was this translation helpful? Give feedback.
All reactions