3

There are so many option to do this in Go. For example:

scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
    fmt.Println(scanner.Text())
}

or

reader := bufio.NewReader(os.Stdin)
text, _ := reader.ReadString('\n')

Neither is working in my case. I am unable to find the reason why new line scan is not working.

Here's the question I'm working on: https://www.hackerrank.com/challenges/30-dictionaries-and-maps

And here's my code:

package main
import (
    "fmt"
    "bufio"
    "os"
    "strings"
)

func main() {
    var count int
    fmt.Scan(&count)

    m := make(map[string]string)
    for i := 0; i<count; i++{
        reader := bufio.NewReader(os.Stdin)
        text,err := reader.ReadString('\n')
        if err != nil {
           fmt.Println(err)
        }
        value := strings.Fields(text)
        m[value[0]] = value[1]
    }
    var names []string
    for i := 0; i<count; i++{
        var name string
        fmt.Scan(&name)
        names = append(names,name)
    }

    for j:= 0; j<len(names);j++{
        if m[names[j]] != ""{
            fmt.Println(names[j] +" = "+ m[names[j]])
        }else{
            fmt.Println("Not found")
        }

    }

}

It is working in my editor, but when I use the question's input, it doesn't work.

Sapna Mishra
  • 187
  • 1
  • 2
  • 15
  • 2
    What's the actual issue? Are you getting an error? Incorrect output? (If so, what input and what's the wrong output?) – user94559 Oct 14 '16 at 05:44
  • I do notice you're only reading `count` queries (after the `count` phone book entries) despite the fact that the question says you'll receive an unknown number. Not sure if there are other bugs... it would help if you would tell us what problem you're having. – user94559 Oct 14 '16 at 05:46
  • Another issue I spotted: you have spaces around the `=` in your output. (You'll output `foo = 1234` instead of `foo=1234`.) – user94559 Oct 14 '16 at 05:47
  • This code also doesn't compile (extra right parenthesis). – user94559 Oct 14 '16 at 05:59
  • After tesing on the platform, there is indeed some weird strings that are sent, and the `strings.Split(...)` function sometimes only decodes an empty array. – T. Claverie Oct 14 '16 at 06:08
  • I wrote a solution myself and passed all the tests without trouble, but I didn't use `strings.Split` or `strings.Fields`. (I just used `fmt.Scan` for that part.) – user94559 Oct 14 '16 at 06:13
  • There is no too many options in Go, just 2 and they have different deafult behaviors and conceptually there is a lot of difference between a [Reader](https://golang.org/pkg/bufio/#Reader) and a [Scanner](https://golang.org/pkg/bufio/#Scanner) take a look into the documentation – Yandry Pozo Oct 14 '16 at 21:49
  • @smarx Thanks for editing and later I also tried that solution fmt.Scan but It was not working in test case 1. Can you show your code which is working in all test cases ? – Sapna Mishra Oct 15 '16 at 11:32

3 Answers3

2

As requested in the comments, here was my working example:

package main

import (
    "bufio"
    "fmt"
    "os"
)

func main() {
    phonebook := make(map[string]int)

    var count int
    fmt.Scan(&count)
    for i := 0; i < count; i++ {
        var name string
        var number int
        fmt.Scan(&name, &number)
        phonebook[name] = number
    }

    scanner := bufio.NewScanner(os.Stdin)
    for scanner.Scan() {
        name := scanner.Text()
        if number, ok := phonebook[name]; ok {
            fmt.Printf("%s=%d\n", name, number)
        } else {
            fmt.Println("Not found")
        }
    }
}
user94559
  • 59,196
  • 6
  • 103
  • 103
1

First of all, you initiate your reader each time in a loop.
Second, if you go with Reader, stick with it. You initialize input reader, then try to go back to fmt.Scan, but Reader already got your input.
Third, remember that you need to .Trim() your input

    package main

    import (
            "fmt"
    "bufio"
    "os"
    "strings"
    )

    func main() {
        var count int
        fmt.Scan(&count)
        reader := bufio.NewReader(os.Stdin)
        m := make(map[string]string)
        for i := 0; i<count; i++{

            text,err := reader.ReadString('\n')
            if err != nil {
                fmt.Println(err)
            }
            value := strings.Fields(text)
            m[value[0]] = value[1]
        }
        var names []string
        for i := 0; i<count; i++{
            var name string
            name, _ = reader.ReadString('\n')
            names = append(names,strings.Trim(name, " \n"))
        }

        for _, name := range names {

            phone, found := m[name]
            if found {
                fmt.Println(name +"="+ phone)
            }else{
                fmt.Println("Not found")
            }

        }

    }
Alexey Soshin
  • 16,718
  • 2
  • 31
  • 40
0
package main
import "fmt"

func main() {
 //Enter your code here. Read input from STDIN. Print output to STDOUT
    var amount int 
    fmt.Scan(&amount)
    n := make([]int, amount)
   
    for _, v := range n {
        fmt.Scan(&v)
        fmt.Println(v)   
    }
  }

Not optimal, but it passes the Hacker Rank Test cases. Which is the entire point.