From af1a19b020e5c9ebcf592e0b69e48eccfd243c72 Mon Sep 17 00:00:00 2001 From: Swarnalatha Natarajan <35426480+SwarnaLathaNatarajan@users.noreply.github.com> Date: Wed, 20 May 2020 23:32:48 -0600 Subject: [PATCH 1/5] Add files via upload --- quiz/main.go | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 quiz/main.go diff --git a/quiz/main.go b/quiz/main.go new file mode 100644 index 0000000..3238918 --- /dev/null +++ b/quiz/main.go @@ -0,0 +1,62 @@ +package main + +import ( + "bufio" + "encoding/csv" + "fmt" + "log" + "math/rand" + "os" + "strings" + "time" +) + +func main() { + in := bufio.NewReader(os.Stdin) + + //Opening and reading question file + csvfile, err := os.Open("problems.csv") + if err != nil { + log.Fatalln("Couldn't open csv file ", err) + } + r := csv.NewReader(csvfile) + records, err := r.ReadAll() + if err != nil { + log.Fatalln(err) + } + + //Asking shuffle and time limit choice + var totalScore, totalQuestions, timeLimit, timeChoice int + var shuffle string + shuffleOptions := map[string]bool{"y": true, "n": false} + fmt.Println("Do you want to shuffle the questions ? y or n") + fmt.Scanln(&shuffle) + if shuffleOptions[shuffle] { + rand.Seed(time.Now().UnixNano()) + rand.Shuffle(len(records), func(i, j int) { records[i], records[j] = records[j], records[i] }) + } + timeOptions := map[int]int{1: 30, 2: 45, 3: 60} + fmt.Println("Choose time limit \n 1. 30 seconds \t 2. 45 seconds \t 3. 60 seconds") + fmt.Scanln(&timeChoice) + + //Timer begins + timeLimit = timeOptions[timeChoice] + timer := time.AfterFunc(time.Second*time.Duration(timeLimit), func() { + fmt.Println("Total Score : ", totalScore) + fmt.Println("No. of Questions : ", totalQuestions) + os.Exit(0) + }) + defer timer.Stop() + for i := 0; i < len(records); i++ { + record := records[i] + fmt.Println("What is " + record[0] + " ?") + userAnswer, _ := in.ReadString('\n') + + //Ignores case mismatch and additional white spaces + if strings.Join(strings.Fields(strings.ToLower(userAnswer)), "") == strings.ToLower(record[1]) { + totalScore++ + } + totalQuestions++ + } + +} From 00a1973419b48f0921a15e905f14f2947283c3f1 Mon Sep 17 00:00:00 2001 From: Swarnalatha Natarajan Date: Thu, 21 May 2020 08:59:50 -0600 Subject: [PATCH 2/5] urlshort exercise done --- urlshort/cmd/main.go | 4 ++-- urlshort/handler.go | 32 ++++++++++++++++++++++++++++++-- 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/urlshort/cmd/main.go b/urlshort/cmd/main.go index 6f97644..ce93ef5 100644 --- a/urlshort/cmd/main.go +++ b/urlshort/cmd/main.go @@ -4,7 +4,7 @@ import ( "fmt" "net/http" - "go_learning/urlshort" + "github.com/SwarnaLathaNatarajan/go_learning/urlshort" ) func main() { @@ -23,7 +23,7 @@ func main() { - path: /urlshort url: https://github.com/gophercises/urlshort - path: /urlshort-final - url: https://github.com/gophercises/urlshort/tree/solution + url: https://github.com/gophercises/urlshort/tree/final ` yamlHandler, err := urlshort.YAMLHandler([]byte(yaml), mapHandler) if err != nil { diff --git a/urlshort/handler.go b/urlshort/handler.go index 50379b3..2954ce5 100644 --- a/urlshort/handler.go +++ b/urlshort/handler.go @@ -2,6 +2,8 @@ package urlshort import ( "net/http" + + "gopkg.in/yaml.v2" ) // MapHandler will return an http.HandlerFunc (which also @@ -12,7 +14,14 @@ import ( // http.Handler will be called instead. func MapHandler(pathsToUrls map[string]string, fallback http.Handler) http.HandlerFunc { // TODO: Implement this... - return nil + return func(w http.ResponseWriter, r *http.Request) { + path, ok := pathsToUrls[r.URL.Path] + if ok { + http.Redirect(w, r, path, http.StatusFound) + } else { + fallback.ServeHTTP(w, r) + } + } } // YAMLHandler will parse the provided YAML and then return @@ -33,5 +42,24 @@ func MapHandler(pathsToUrls map[string]string, fallback http.Handler) http.Handl // a mapping of paths to urls. func YAMLHandler(yml []byte, fallback http.Handler) (http.HandlerFunc, error) { // TODO: Implement this... - return nil, nil + parsedYaml, err := parseYAML(yml) + if err != nil { + return nil, err + } + pathMap := buildMap(parsedYaml) + return MapHandler(pathMap, fallback), nil +} + +func parseYAML(yml []byte) (out []map[string]string, err error) { + err = yaml.Unmarshal(yml, &out) + return out, err +} + +func buildMap(parsedYaml []map[string]string) map[string]string { + pathMap := make(map[string]string) + for _, entry := range parsedYaml { + key := entry["path"] + pathMap[key] = entry["url"] + } + return pathMap } From 824b700f95929fbbae38f6d5f5a069d45d93e371 Mon Sep 17 00:00:00 2001 From: Swarnalatha Natarajan <35426480+SwarnaLathaNatarajan@users.noreply.github.com> Date: Thu, 21 May 2020 10:39:16 -0600 Subject: [PATCH 3/5] Update main.go --- urlshort/cmd/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/urlshort/cmd/main.go b/urlshort/cmd/main.go index ce93ef5..805f862 100644 --- a/urlshort/cmd/main.go +++ b/urlshort/cmd/main.go @@ -23,7 +23,7 @@ func main() { - path: /urlshort url: https://github.com/gophercises/urlshort - path: /urlshort-final - url: https://github.com/gophercises/urlshort/tree/final + url: https://github.com/gophercises/urlshort/tree/solution ` yamlHandler, err := urlshort.YAMLHandler([]byte(yaml), mapHandler) if err != nil { From dd479f08b4efe28ada34341d47b302a6c1eb111f Mon Sep 17 00:00:00 2001 From: Swarnalatha Natarajan Date: Thu, 21 May 2020 16:11:03 -0600 Subject: [PATCH 4/5] Flags and pointer receivers added --- .idea/.gitignore | 8 +++++ .idea/go_learning.iml | 8 +++++ .idea/misc.xml | 6 ++++ .idea/modules.xml | 8 +++++ .idea/vcs.xml | 6 ++++ quiz/main.go | 83 ++++++++++++++++++++++++++----------------- 6 files changed, 87 insertions(+), 32 deletions(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/go_learning.iml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..73f69e0 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/.idea/go_learning.iml b/.idea/go_learning.iml new file mode 100644 index 0000000..c956989 --- /dev/null +++ b/.idea/go_learning.iml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..28a804d --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..e8108a3 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/quiz/main.go b/quiz/main.go index 3238918..861aa9b 100644 --- a/quiz/main.go +++ b/quiz/main.go @@ -3,6 +3,7 @@ package main import ( "bufio" "encoding/csv" + "flag" "fmt" "log" "math/rand" @@ -11,11 +12,17 @@ import ( "time" ) -func main() { - in := bufio.NewReader(os.Stdin) +type Quiz struct { + filename string + timeLimit int + shuffle bool + problems [][]string + totalScore int + totalQuestions int +} - //Opening and reading question file - csvfile, err := os.Open("problems.csv") +func (q *Quiz) readProblems() { + csvfile, err := os.Open(q.filename) if err != nil { log.Fatalln("Couldn't open csv file ", err) } @@ -24,39 +31,51 @@ func main() { if err != nil { log.Fatalln(err) } + q.problems = records +} +func (q *Quiz) shuffleProblems() { + rand.Seed(time.Now().UnixNano()) + rand.Shuffle(len(q.problems), func(i, j int) { q.problems[i], q.problems[j] = q.problems[j], q.problems[i] }) +} - //Asking shuffle and time limit choice - var totalScore, totalQuestions, timeLimit, timeChoice int - var shuffle string - shuffleOptions := map[string]bool{"y": true, "n": false} - fmt.Println("Do you want to shuffle the questions ? y or n") - fmt.Scanln(&shuffle) - if shuffleOptions[shuffle] { - rand.Seed(time.Now().UnixNano()) - rand.Shuffle(len(records), func(i, j int) { records[i], records[j] = records[j], records[i] }) - } - timeOptions := map[int]int{1: 30, 2: 45, 3: 60} - fmt.Println("Choose time limit \n 1. 30 seconds \t 2. 45 seconds \t 3. 60 seconds") - fmt.Scanln(&timeChoice) - - //Timer begins - timeLimit = timeOptions[timeChoice] - timer := time.AfterFunc(time.Second*time.Duration(timeLimit), func() { - fmt.Println("Total Score : ", totalScore) - fmt.Println("No. of Questions : ", totalQuestions) - os.Exit(0) - }) - defer timer.Stop() - for i := 0; i < len(records); i++ { - record := records[i] - fmt.Println("What is " + record[0] + " ?") +func (q *Quiz) printProblems() { + in := bufio.NewReader(os.Stdin) + for i := 0; i < len(q.problems); i++ { + question, answer := q.problems[i][0], q.problems[i][1] + fmt.Println("What is " + question + " ?") userAnswer, _ := in.ReadString('\n') //Ignores case mismatch and additional white spaces - if strings.Join(strings.Fields(strings.ToLower(userAnswer)), "") == strings.ToLower(record[1]) { - totalScore++ + if strings.Join(strings.Fields(strings.ToLower(userAnswer)), "") == strings.ToLower(answer) { + q.totalScore++ } - totalQuestions++ + q.totalQuestions++ + } +} + +func main() { + + //Get flags values + filename := flag.String("filename", "problems.csv", "File containing the questions") + timeLimit := flag.Int("timeLimit", 30, "Time limit in seconds") + shuffle := flag.Bool("shuffle", false, "Shuffle questions") + flag.Parse() + + q := Quiz{filename: *filename, timeLimit: *timeLimit, shuffle: *shuffle} + + q.readProblems() + if q.shuffle { + q.shuffleProblems() } + //Setting timer + timer := time.AfterFunc(time.Second*time.Duration(q.timeLimit), func() { + fmt.Println("Total Score : ", q.totalScore) + fmt.Println("No. of Questions : ", q.totalQuestions) + os.Exit(0) + }) + defer timer.Stop() + + q.printProblems() + } From 79231aa7b8b6a38959fca2aba3bb8187820b82f1 Mon Sep 17 00:00:00 2001 From: Swarnalatha Natarajan <35426480+SwarnaLathaNatarajan@users.noreply.github.com> Date: Fri, 22 May 2020 11:10:22 -0600 Subject: [PATCH 5/5] Updated quiz problem