英文:
Golang: int to slice conversion
问题
完全的golang(和编程)新手!
给定任何一个六位数,如何输出一个切片,其中该数字的每个字符都被分配到切片的一个独立位置?
例如,一个包含所有这些字符的切片(我们称之为s),将有s[0]=第一个数字,s1=第二个数字,s[2]=第三个数字,依此类推。
非常感谢任何帮助!
英文:
Total golang (and programming) noob!
Given any six digit number, how could one output a slice where each character of that number is assigned an individual location within the slice?
For instance, a slice (let's call it s) containing all of these characters, would have s[0]=first digit, s1=second digit, s[2]=third digit and so on.
Any help would be greatly appreciated!
答案1
得分: 8
func IntToSlice(n int64, sequence []int64) []int64 {
if n != 0 {
i := n % 10
// sequence = append(sequence, i) // 逆序输出
sequence = append([]int64{i}, sequence...)
return IntToSlice(n/10, sequence)
}
return sequence
}
这段代码是一个将整数转换为切片的函数。它使用递归的方式将整数的每一位数字添加到切片中。在每一步中,取整数除以10的余数作为当前位的数字,并将其添加到切片的开头。然后,将整数除以10,继续递归调用函数,直到整数变为0。最后,返回得到的切片。
英文:
func IntToSlice(n int64, sequence []int64) []int64 {
if n != 0 {
i := n % 10
// sequence = append(sequence, i) // reverse order output
sequence = append([]int64{i}, sequence...)
return IntToSlice(n/10, sequence)
}
return sequence
}
答案2
得分: 4
以上答案是正确的。这里是另一个版本的MBB的答案。
避免递归和高效的反转可能会提高性能并减少RAM消耗。
package main
import (
"fmt"
)
func reverseInt(s []int) {
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
s[i], s[j] = s[j], s[i]
}
}
func splitToDigits(n int) []int {
var ret []int
for n != 0 {
ret = append(ret, n%10)
n /= 10
}
reverseInt(ret)
return ret
}
func main() {
for _, n := range splitToDigits(12345) {
fmt.Println(n)
}
}
https://play.golang.org/p/M3aOUnNIbdv
英文:
The above answers are correct. Here comes another version of MBB's answer.
Avoiding recursion and efficient reverting may increase performance and reduce RAM consumption.
package main
import (
"fmt"
)
func reverseInt(s []int) {
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
s[i], s[j] = s[j], s[i]
}
}
func splitToDigits(n int) []int{
var ret []int
for n !=0 {
ret = append(ret, n % 10)
n /= 10
}
reverseInt(ret)
return ret
}
func main() {
for _, n := range splitToDigits(12345) {
fmt.Println(n)
}
}
答案3
得分: 1
这是一个两步的过程,首先将整数转换为字符串,然后迭代字符串或将其转换为切片。因为内置的range函数可以迭代字符串中的每个字符,所以我建议将其保持为字符串。可以像这样实现:
import "strconv"
str := strconv.Itoa(123456)
for i, v := range str {
fmt.Println(v) // 在新行上打印每个字符的ASCII值
fmt.Printf("%c\n", v) // 打印字符值
}
英文:
This is a two step process, first converting int to string, then iterating the string or converting to a slice. Because the built in range function lets you iterate each character in a string, I recommend keeping it as a string. Something like this;
import "strconv"
str := strconv.Itoa(123456)
for i, v := range str {
fmt.Println(v) //prints each char's ASCII value on a newline
fmt.Printf("%c\n", v) // prints the character value
}
答案4
得分: 0
我很困惑为什么没有人提到这种方法:
(不需要递归)
import (
"fmt"
"strconv"
)
func main() {
n := 3456
fmt.Println(NumToArray(n))
fmt.Println(NumToArray2(n))
}
func NumToArray(num int) []int {
arr := make([]int, len(strconv.Itoa(num)))
for i := len(arr) - 1; num > 0; i-- {
arr[i] = num % 10
num = int(num / 10)
}
fmt.Println(arr)
return arr
}
// Without converting to string
func NumToArray2(num int) (arr []int) {
for num > 0 {
arr = append(arr, num%10)
num = int(num / 10)
}
// Reverse array to the right order
for i, j := 0, len(arr)-1; i < j; i, j = i+1, j-1 {
arr[i], arr[j] = arr[j], arr[i]
}
fmt.Println(arr)
return arr
}
P.S. 欢迎进行基准测试。
英文:
I'm confused why nobody mentioned this way:
(No need recursion)
import (
"fmt"
"strconv"
)
func main() {
n := 3456
fmt.Println(NumToArray(n))
fmt.Println(NumToArray2(n))
}
func NumToArray(num int) []int {
arr := make([]int, len(strconv.Itoa(num)))
for i := len(arr) - 1; num > 0; i-- {
arr[i] = num % 10
num = int(num / 10)
}
fmt.Println(arr)
return arr
}
// Without converting to string
func NumToArray2(num int) (arr []int) {
for num > 0 {
arr = append(arr, num%10)
num = int(num / 10)
}
// Reverse array to the rigtht order
for i, j := 0, len(arr)-1; i < j; i, j = i+1, j-1 {
arr[i], arr[j] = arr[j], arr[i]
}
fmt.Println(arr)
return arr
}
P.S. Benchmarks are welcome
答案5
得分: 0
对于这个问题,你可以将整数值转换为字符串,然后使用字符串库中的split函数。我希望下面的代码对你有用!
package main
import (
"fmt"
"strings"
"strconv"
)
func main() {
num:=10101
a:=strconv.Itoa(num)
res:=strings.Split(a,"")
fmt.Println("The value of res is",res)
fmt.Printf("The type of res is %T\n",res)
fmt.Println(res[0])
}
输出结果:The value of res is [1 0 1 0 1]
The type of res is []string 1
英文:
For this problem you can convert your int value to string and after that you can use split function which is under strings library.I hope below code will work for you!
package main
import (
"fmt"
"strings"
"strconv"
)
func main() {
num:=10101
a:=strconv.Itoa(num)
res:=strings.Split(a,"")
fmt.Println("The value of res is",res)
fmt.Printf("The type of res is %T\n",res)
fmt.Println(res[0])
}
> Output: The value of res is [1 0 1 0 1]
The type of res is []string 1
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论