英文:
Insert into output all one + two + three product IDs that start with the digit "1"
问题
我是你的中文翻译助手,以下是你要翻译的内容:
我对Go语言还不熟悉。如何将以数字"1"开头的所有one + two + three产品ID插入到output中?
package main
import (
"fmt"
)
func main() {
var output []string
// 1) 将以数字"1"开头的所有one + two + three产品ID插入到output中
fmt.Println(output)
}
func GetOneProductIDs() (out []string) {
for i := 0; i < 100; i += 10 {
out = append(out, fmt.Sprintf("%d_%s", i, "one"))
}
return out
}
func GetIwoProductIDs() (out []string) {
for i := 0; i < 100; i += 5 {
out = append(out, fmt.Sprintf("%d_%s", i, "two"))
}
return out
}
func GetThreeProductIDs() (out []string) {
for i := 0; i < 100; i += 2 {
out = append(out, fmt.Sprintf("%d_%s", i, "three"))
}
return out
}
https://play.golang.org/p/ftdSMJiSfq_D
英文:
I am new to Go. How do I insert into output all one + two + three product IDs that start with the digit "1"
package main
import (
"fmt"
)
func main() {
var output []string
// 1) Insert into output all one + two + three product IDs that start with the digit "1"
fmt.Println(output)
}
func GetOneProductIDs() (out []string) {
for i := 0; i < 100; i += 10 {
out = append(out, fmt.Sprintf("%d_%s", i, "one"))
}
return out
}
func GetIwoProductIDs() (out []string) {
for i := 0; i < 100; i += 5 {
out = append(out, fmt.Sprintf("%d_%s", i, "two"))
}
return out
}
func GetThreeProductIDs() (out []string) {
for i := 0; i < 100; i += 2 {
out = append(out, fmt.Sprintf("%d_%s", i, "three"))
}
return out
}
答案1
得分: 2
我为您提供了一个相当原始和直接的解决方案,但它可以正常工作:
package main
import (
"fmt"
"strings"
)
func main() {
var output []string
// 1) 将以数字“1”开头的所有 one + two + three 产品ID插入到 output 中
for _, res := range GetOneProductIDs() {
if strings.HasPrefix(res, "1") {
output = append(output, res)
}
}
for _, res := range GetIwoProductIDs() {
if strings.HasPrefix(res, "1") {
output = append(output, res)
}
}
for _, res := range GetThreeProductIDs() {
if strings.HasPrefix(res, "1") {
output = append(output, res)
}
}
fmt.Println(output)
}
func GetOneProductIDs() (out []string) {
for i := 0; i < 100; i += 10 {
out = append(out, fmt.Sprintf("%d_%s", i, "one"))
}
return out
}
func GetIwoProductIDs() (out []string) {
for i := 0; i < 100; i += 5 {
out = append(out, fmt.Sprintf("%d_%s", i, "two"))
}
return out
}
func GetThreeProductIDs() (out []string) {
for i := 0; i < 100; i += 2 {
out = append(out, fmt.Sprintf("%d_%s", i, "three"))
}
return out
}
您可以在以下链接中查看代码:https://play.golang.org/p/RwDFEMJ-5Je
英文:
I've got pretty primitive and straightforward solution for you but it works fine:
package main
import (
"fmt"
"strings"
)
func main() {
var output []string
// 1) Insert into output all one + two + three product IDs that start with the digit "1"
for _, res := range GetOneProductIDs() {
if strings.HasPrefix(res, "1") {
output = append(output, res)
}
}
for _, res := range GetIwoProductIDs() {
if strings.HasPrefix(res, "1") {
output = append(output, res)
}
}
for _, res := range GetThreeProductIDs() {
if strings.HasPrefix(res, "1") {
output = append(output, res)
}
}
fmt.Println(output)
}
func GetOneProductIDs() (out []string) {
for i := 0; i < 100; i += 10 {
out = append(out, fmt.Sprintf("%d_%s", i, "one"))
}
return out
}
func GetIwoProductIDs() (out []string) {
for i := 0; i < 100; i += 5 {
out = append(out, fmt.Sprintf("%d_%s", i, "two"))
}
return out
}
func GetThreeProductIDs() (out []string) {
for i := 0; i < 100; i += 2 {
out = append(out, fmt.Sprintf("%d_%s", i, "three"))
}
return out
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论