Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read space separated integers from stdin into int slice

Tags:

go

I'm trying to read from stdin two lines of an unknown number of space-separated integers. I would like to store each lines ints into their own int slice.

For example, my input may look like this:

1 2 3
4 5 6

and I want to read this into two []int:

[1,2,3]
[4,5,6]

This is what I have so far. scanner.Scan() is giving me the line, but I'm not sure how to convert that into a []int:

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

func main() {
    var firstLine []int
    var secondLine []int

    scanner := bufio.NewScanner(os.Stdin)
    for scanner.Scan() {
        t := scanner.Text()
    }
}
like image 528
Nate Jenson Avatar asked Apr 24 '17 23:04

Nate Jenson


People also ask

How do you read space separated integers in python?

Use input(), map() and split() function to take space-separated integer input in Python 3. You have to use list() to convert the map to a list.

How do I take the input of a line of integers separated by a space in Java?

There are 2 methods to take input from the user which are separated by space which are as follows: Using BufferedReader Class and then splitting and parsing each value. Using nextInt( ) method of Scanner class.


3 Answers

For example,

numbers.go:

package main

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

func numbers(s string) []int {
    var n []int
    for _, f := range strings.Fields(s) {
        i, err := strconv.Atoi(f)
        if err == nil {
            n = append(n, i)
        }
    }
    return n
}

func main() {
    var firstLine, secondLine []int
    scanner := bufio.NewScanner(os.Stdin)
    for i := 1; i <= 2 && scanner.Scan(); i++ {
        switch i {
        case 1:
            firstLine = numbers(scanner.Text())
        case 2:
            secondLine = numbers(scanner.Text())
        }
    }
    fmt.Println(firstLine)
    fmt.Println(secondLine)
}

Output:

$ go run numbers.go
1 2 3
4 5 6
[1 2 3]
[4 5 6]
$
like image 112
peterSO Avatar answered Nov 08 '22 20:11

peterSO


If you are looking for code to read input to solve problems in hackathons, here is your best solution

package main

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

func main() {

    reader := bufio.NewReader(os.Stdin)

    a:= read(reader,100000)

    fmt.Println(a)
}

func read (reader *bufio.Reader, n int)([]uint32) {

    a := make([]uint32, n)
    for i:=0; i<n; i++ {
        fmt.Fscan(reader, &a[i])
    }

    return a
}
like image 37
Aswin Avatar answered Nov 08 '22 21:11

Aswin


So, this is what I ended up doing. There is likely a more idiomatic way of solving it, though.

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

func main() {

    scanner := bufio.NewScanner(os.Stdin)

    scanner.Scan()
    parts := strings.Split(scanner.Text(), " ")
    lineOne := createIntSlice(parts)

    scanner.Scan()
    parts = strings.Split(scanner.Text(), " ")
    lineTwo := createIntSlice(parts)

    fmt.Println(lineOne)
    fmt.Println(lineTwo)
}

func createIntSlice(nums []string) []int {
    var r []int
    for _, v := range nums {
        i, _ := strconv.Atoi(v)
        r = append(r, i)
    }
    return r
}
like image 42
Nate Jenson Avatar answered Nov 08 '22 21:11

Nate Jenson