4

I tackle with this question.

I need to convert strings to int. In this case, I need to convert "5 2 4 6 1 3" to, for example, [6]int{5,2,4,6,1,3}. I wrote following this code, especially AizuArray(). It seems elements are int here. Would you please let me know if my way is correct? Or could you let me know the better ways? I ask this because I feel my way would be redundant and Java way is much easier. Thank you.

package main

import (
    "fmt"
    "reflect"
    "strconv"
    "strings"
)

func AizuArray(A string, N string) []int {
    a := strings.Split(A, " ")
    n, _ := strconv.Atoi(N) // int 32bit
    b := make([]int, n)
    for i, v := range a {
        b[i], _ = strconv.Atoi(v)
    }
    return b
}

func main() {
    A := "5 2 4 6 1 3"
    N := "6"
    j := strings.Split(A, " ")
    for _, v := range j {
        fmt.Println(reflect.TypeOf(v))
    }
    b := AizuArray(A, N)
    fmt.Println(b)
    for _, v := range b {
        fmt.Println(reflect.TypeOf(v))
    }
}
yamachan
  • 1,029
  • 2
  • 12
  • 28
  • 1
    Keep in mind that int size in go depends on the host CPU, it's 32bit on 32bit CPUs and 64bit on well, 64bit CPUs. – OneOfOne Jun 11 '16 at 21:23
  • @OneOfOne Thank you for your comment. Yes, when I used `ParseInt()`, an error occurred in my netbook. So, I used `Atoi` instead. – yamachan Jun 13 '16 at 12:18

3 Answers3

8

Would you please let me know if my way is correct?

If you just want to convert string(space separated integers) to []int

func AizuArray(A string, N string) []int {
 a := strings.Split(A, " ")
 n, _ := strconv.Atoi(N) // int 32bit
 b := make([]int, n)
 for i, v := range a {
     b[i], err = strconv.Atoi(v)
     if err != nil {
        //proper err handling
        //either b[i] = -1 (in case positive integers)
     }
 }
 return b
}

then your approach is correct.

I tackle with this question.

In context of this question you want to take input from STDIN so should do,

package main

import (
    "fmt"
)

func insertionSort(arr []int) {
    //do further processing here.
   fmt.Println(arr)
}

func main() {
    var N int
    fmt.Scanf("%d", &N)
    b := make([]int, N)
    for iter:=0;iter<N;iter++ {
        fmt.Scanf("%d",&b[iter])
    }
    insertionSort(b)
}
hitesh_noty
  • 365
  • 1
  • 10
  • Thank you for your answer. Ah, I will write error handling later. Thanks. And thank you for your next one. Ah, I will try `fmt.Scanf`. Thank you so much. – yamachan Jun 13 '16 at 12:22
  • Ah, so is `fmt.Scanf()` better than `scanner = bufio.NewScanner(os.Stdin); scanner.Scan(); N := scanner.Text()`? – yamachan Jun 13 '16 at 13:57
  • 1
    so if you want to take faster input http://stackoverflow.com/questions/31333353/faster-input-scanning have a look at this. – hitesh_noty Jun 13 '16 at 14:43
  • Thank you. I use `fmt.Scanf()` instead. – yamachan Jun 16 '16 at 00:34
4

I think you overcomplicating things unless I am missing something.

https://play.golang.org/p/HLvV8R1Ux-

package main

import (
    "fmt"
    "strings"
    "strconv"
)

func main() {
    A := "5 2 4 6 1 3"

    strs := strings.Split(A, " ")
    ary := make([]int, len(strs))
    for i := range ary {
        ary[i], _ = strconv.Atoi(strs[i])
    }

    fmt.Println(ary)    
}
Darigaaz
  • 1,414
  • 10
  • 11
  • 2
    I down voted your answer because I think it is not relevant. First, it adds nothing to the question (you just reuse the code in the question and remove some parts of it). Second avoiding a returned error in go is always a bad advice. Third, according to the link in the question the value N exists and shall be used. And finally, using function is a better way of coding, so no need to remove it. – jnmoal Jun 11 '16 at 17:00
  • I answer to the exact question you asked. Or do you want us to solve your problem completely with clean "production" ready code? Stackoverflow is not the place for these kind of questions. – Darigaaz Jun 11 '16 at 22:15
  • @Darigaaz Thank you for your reply. Yeah, I thought I did make it complicated. So I asked. I am a beginner of Go, so I thought there are something like packages or libraries which I don't know and couldn't find. Ah, I didn't write the comment. I didn't imply your solving the question. I just wanted to ask the part which to convert String to Int. – yamachan Jun 13 '16 at 12:30
  • @Jean-NicolasMoal Thank you for your comment. I will implement the error handling later. Thank you. – yamachan Jun 13 '16 at 12:32
1

Here is a simpler example where you would not have to split the string:

str := "123456"
if _, err := strconv.Atoi(str); err != nil {
    // do stuff, in case str can not be converted to an int
}
var slice []int // empty slice
for _, digit := range str {
    slice = append(slice, int(digit)-int('0')) // build up slice
}

Why do you need the int('0')? Because int() will convert the character to the corresponding ASCII code (ascii table here). For 0 that would be 48. So you will have to substract 48 from whatever your digit corresponds to in "ascii decimal".

User12547645
  • 6,955
  • 3
  • 38
  • 69