We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
I'm trying to solve this in Go and all of my attempts are timing out on test cases 1 - 3, despite working on the sample input 0. I think it's the loop reading/handling the query names, but I don't know why. Some examples:
// Using a Scanner to handle queriesscanner:=bufio.NewScanner(os.Stdin)forscanner.Scan(){name:=scanner.Text()ifphone,ok:=addressBook[name];!ok{fmt.Println("Not found")}else{fmt.Printf("%s=%s\n",name,phone)}}iferr:=scanner.Err();err!=nil{log.Fatal(err)}// Using fmt.Scan to handle queriesvarquerystringfor{_,err:=fmt.Scan(&query)iferr!=nil{iferr==io.EOF{break}log.Fatal(err)}ifphone,ok:=addressBook[query];!ok{fmt.Println("Not found")}else{fmt.Printf("%s=%s\n",query,phone)}}// Reading all of STDIN then iterating over itstdin,err:=io.ReadAll(os.Stdin)iferr!=nil{log.Fatal(err)}queries:=strings.Split(string(stdin),"\n")for_,name:=rangequeries{ifphone,ok:=addressBook[name];!ok{fmt.Println("Not found")}else{fmt.Printf("%s=%s\n",name,phone)}}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Day 8: Dictionaries and Maps
You are viewing a single comment's thread. Return to all comments →
I'm trying to solve this in Go and all of my attempts are timing out on test cases 1 - 3, despite working on the sample input 0. I think it's the loop reading/handling the query names, but I don't know why. Some examples: