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 new file mode 100644 index 0000000..861aa9b --- /dev/null +++ b/quiz/main.go @@ -0,0 +1,81 @@ +package main + +import ( + "bufio" + "encoding/csv" + "flag" + "fmt" + "log" + "math/rand" + "os" + "strings" + "time" +) + +type Quiz struct { + filename string + timeLimit int + shuffle bool + problems [][]string + totalScore int + totalQuestions int +} + +func (q *Quiz) readProblems() { + csvfile, err := os.Open(q.filename) + 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) + } + 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] }) +} + +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(answer) { + q.totalScore++ + } + 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() + +} diff --git a/urlshort/cmd/main.go b/urlshort/cmd/main.go index 6f97644..805f862 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() { 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 }