-
Notifications
You must be signed in to change notification settings - Fork 8
Swarna's solutions for Quiz and Url shortener #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
af1a19b
00a1973
824b700
dd479f0
b9882b4
79231aa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -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) | ||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should probably make sure you close the file after opening it. |
||||||||||||||||||||||||
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()) | ||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seeding should only happen once and in the main function. |
||||||||||||||||||||||||
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') | ||||||||||||||||||||||||
Comment on lines
+43
to
+46
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In Go, you'll typically see people iterating through slices using the range clause.
Suggested change
Also note the use of fmt.Printf() instead of fmt.Println(). It's not actually important in this case, given that question is just a string, but I thought I'd make a note just in case you weren't familiar. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh okay. I'll make sure that I follow this in the future. |
||||||||||||||||||||||||
|
||||||||||||||||||||||||
//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() | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
} |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -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... | ||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you still need 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) | ||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For the status code, I referred the following article https://stackoverflow.com/questions/22887148/is-the-http-statusfound-the-correct-status-for-a-click-redirect There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Checks out |
||||||||||||||||||||||||
} else { | ||||||||||||||||||||||||
fallback.ServeHTTP(w, r) | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
Comment on lines
+18
to
+23
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In go you will typically return instead of using else statements where appropriate.
Suggested change
|
||||||||||||||||||||||||
} | ||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
// 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... | ||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you still need 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) { | ||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure you need this function since it would only be two lines in the YAMLHandler function. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The parseYAML stub was suggested in the problem. That's why I went ahead with it. |
||||||||||||||||||||||||
err = yaml.Unmarshal(yml, &out) | ||||||||||||||||||||||||
return out, err | ||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using the named return values in the signature was a clever idea here. When you do that you don't need to specify the variables in the return statement (as long as you are returning the signature values). |
||||||||||||||||||||||||
} | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
func buildMap(parsedYaml []map[string]string) map[string]string { | ||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is this going to only accept YAML? The bonus objective is to also be able to use JSON. |
||||||||||||||||||||||||
pathMap := make(map[string]string) | ||||||||||||||||||||||||
for _, entry := range parsedYaml { | ||||||||||||||||||||||||
key := entry["path"] | ||||||||||||||||||||||||
pathMap[key] = entry["url"] | ||||||||||||||||||||||||
Comment on lines
+61
to
+62
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like you could have used a struct instead of a map here. Using a struct would be good practice for using go tags. |
||||||||||||||||||||||||
} | ||||||||||||||||||||||||
return pathMap | ||||||||||||||||||||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems like a
Problem
orQuestion
struct would be better than a slice of slices.