From 9fb22f7fa92035be157a2b126ae8d850f1a79e40 Mon Sep 17 00:00:00 2001 From: Inanc Gumus Date: Fri, 12 Apr 2019 16:03:10 +0300 Subject: [PATCH] =?UTF-8?q?add:=20maps=20project=20=E2=80=94=20log=20parse?= =?UTF-8?q?r?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 23-project-log-parser/01-scanning/main.go | 32 +++++++++ .../01-scanning/proverbs.txt | 4 ++ .../02-project-file-parser}/log.txt | 5 +- .../02-project-file-parser/main.go | 65 +++++++++++++++++++ x-tba/xxx-project-domain-visits.1/main.go | 64 ------------------ 5 files changed, 105 insertions(+), 65 deletions(-) create mode 100644 23-project-log-parser/01-scanning/main.go create mode 100644 23-project-log-parser/01-scanning/proverbs.txt rename {x-tba/xxx-project-domain-visits.1 => 23-project-log-parser/02-project-file-parser}/log.txt (61%) create mode 100644 23-project-log-parser/02-project-file-parser/main.go delete mode 100644 x-tba/xxx-project-domain-visits.1/main.go diff --git a/23-project-log-parser/01-scanning/main.go b/23-project-log-parser/01-scanning/main.go new file mode 100644 index 0000000..187ecbd --- /dev/null +++ b/23-project-log-parser/01-scanning/main.go @@ -0,0 +1,32 @@ +// For more tutorials: https://blog.learngoprogramming.com +// +// Copyright © 2018 Inanc Gumus +// Learn Go Programming Course +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ +// + +package main + +import ( + "bufio" + "fmt" + "os" +) + +func main() { + // Create a new scanner that scans from the standard-input + in := bufio.NewScanner(os.Stdin) + + // Stores the total number of lines in the input + var lines int + + // Scan the input line by line + for in.Scan() { + lines++ + + // Get the current line from the scanner's buffer + // fmt.Println("Scanned Text :", in.Text()) + // fmt.Println("Scanned Bytes:", in.Bytes()) + } + fmt.Printf("There %d lines.\n", lines) +} diff --git a/23-project-log-parser/01-scanning/proverbs.txt b/23-project-log-parser/01-scanning/proverbs.txt new file mode 100644 index 0000000..9f48fde --- /dev/null +++ b/23-project-log-parser/01-scanning/proverbs.txt @@ -0,0 +1,4 @@ +Go Proverbs +Make the zero value useful +Clear is better than clever +Errors are values \ No newline at end of file diff --git a/x-tba/xxx-project-domain-visits.1/log.txt b/23-project-log-parser/02-project-file-parser/log.txt similarity index 61% rename from x-tba/xxx-project-domain-visits.1/log.txt rename to 23-project-log-parser/02-project-file-parser/log.txt index 34e1d38..146d527 100644 --- a/x-tba/xxx-project-domain-visits.1/log.txt +++ b/23-project-log-parser/02-project-file-parser/log.txt @@ -1,8 +1,11 @@ learngoprogramming.com 10 learngoprogramming.com 5 -learngoprogramming.com 10 +learngoprogramming.com learngoprogramming.com 20 golang.org 4 golang.org 6 golang.org 5 golang.org 10 +blog.golang.org 5 +blog.golang.org 20 +blog.golang.org 10 diff --git a/23-project-log-parser/02-project-file-parser/main.go b/23-project-log-parser/02-project-file-parser/main.go new file mode 100644 index 0000000..d0b8ead --- /dev/null +++ b/23-project-log-parser/02-project-file-parser/main.go @@ -0,0 +1,65 @@ +// For more tutorials: https://blog.learngoprogramming.com +// +// Copyright © 2018 Inanc Gumus +// Learn Go Programming Course +// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ +// + +package main + +import ( + "bufio" + "fmt" + "os" + "strconv" + "strings" +) + +func main() { + var ( + // Stores the visits per unique domain + sum = make(map[string]int) + + // Stores the unique domain names for printing + domains []string + + in = bufio.NewScanner(os.Stdin) + ) + + // Scan the standard-in line by line + for line := 0; in.Scan(); line++ { + + // Parse the fields + fields := strings.Fields(in.Text()) + if len(fields) != 2 { + fmt.Printf("wrong input: %v (line #%d)\n", fields, line) + return + } + domain, visits := fields[0], fields[1] + + // Collect the unique domains + if _, ok := sum[domain]; !ok { + domains = append(domains, domain) + } + + // Sum the total visits per domain + n, _ := strconv.Atoi(visits) + if n < 0 { + fmt.Printf("wrong input: %q (line #%d)\n", visits, line) + return + } + sum[domain] += n + } + + // Print the visits per domain + var total int + for _, domain := range domains { + hits := sum[domain] + + fmt.Printf("%-25s -> %d\n", domain, hits) + total += hits + } + + // Print the total visits for all domains + fmt.Printf("\n%-25s -> %d\n", "TOTAL", total) +} diff --git a/x-tba/xxx-project-domain-visits.1/main.go b/x-tba/xxx-project-domain-visits.1/main.go deleted file mode 100644 index a4c1a8f..0000000 --- a/x-tba/xxx-project-domain-visits.1/main.go +++ /dev/null @@ -1,64 +0,0 @@ -// For more tutorials: https://blog.learngoprogramming.com -// -// Copyright © 2018 Inanc Gumus -// Learn Go Programming Course -// License: https://creativecommons.org/licenses/by-nc-sa/4.0/ -// - -package main - -import ( - "bufio" - "fmt" - "os" - "strconv" - "strings" -) - -func main() { - var ( - // use a comparable struct for the keys - visits = make(map[string]int) - - domains []string - dupDomains = make(map[string]bool) - - line int - ) - - in := bufio.NewScanner(os.Stdin) - for in.Scan() { - line++ - - fields := strings.Fields(in.Text()) - if len(fields) != 2 { - fmt.Printf("Wrong input: %v\n", fields) - return - } - - domain, svisit := fields[0], fields[1] - - dvisit, err := strconv.Atoi(svisit) - if err != nil { - fmt.Printf("Wrong input: %q (line #%d)\n", svisit, line) - return - } - visits[domain] += dvisit - - // only add the domain once - if !dupDomains[domain] { - domains = append(domains, domain) - dupDomains[domain] = true - } - } - - // print the summary grouped by domain - var total int - for _, d := range domains { - v := visits[d] - fmt.Printf("%-25s -> %d\n", d, v) - total += v - } - - fmt.Printf("\n%-25s -> %d\n", "TOTAL", total) -}