英文:
Reassign a variable without having to create a temporary variable
问题
我正在学习Golang,之前使用的是Python。
下面的函数类似于Python的pop()
方法,它可以从列表(或在Go中称为切片)中删除给定索引处的项,并返回被删除的项。
func popElement(indexOfElement int, slice []int) (int, []int) {
element := slice[indexOfElement]
newSlice := append(slice[:indexOfElement], slice[indexOfElement+1:]...)
return element, newSlice
}
然后,我想将这个函数作为下面排序函数的一部分使用。但是我必须创建一个临时变量newSlice
。
func sortSlice(sliceToSort []int) []int {
var sortedSlice []int
for 1 <= len(sliceToSort) {
indexOfSmallest := findIndexOfSmallest(sliceToSort)
smallestElement, newSlice := popElement(indexOfSmallest, sliceToSort)
sliceToSort = newSlice
sortedSlice = append(sortedSlice, smallestElement)
}
return sortedSlice
}
有没有一种方法可以在不创建临时变量newSlice
的情况下实现相同的结果?
类似于:
func sortSlice(sliceToSort []int) []int {
var sortedSlice []int
for 1 <= len(sliceToSort) {
indexOfSmallest := findIndexOfSmallest(sliceToSort)
smallestElement, sliceToSort = popElement(indexOfSmallest, sliceToSort)
sortedSlice = append(sortedSlice, smallestElement)
}
return sortedSlice
}
英文:
I'm learning Golang and coming from Python.
The following function is similar to python's pop()
method that removes an item at a given index from a list (or slice in Go) and returns the removed item.
func popElement(indexOfElement int, slice []int) (int, []int) {
element := slice[indexOfElement]
newSlice := append(slice[:indexOfElement], slice[indexOfElement+1:]...)
return element, newSlice
}
I would then like to use this function as part of the below sorting function. But I have to create a temporary variable newSlice
func sortSlice(sliceToSort []int) []int {
var sortedSlice []int
for 1 <= len(sliceToSort) {
indexOfSmallest := findIndexOfSmallest(sliceToSort)
smallestElement, newSlice := popElement(indexOfSmallest, sliceToSort)
sliceToSort = newSlice
sortedSlice = append(sortedSlice, smallestElement)
}
return sortedSlice
}
Is there a way to achieve the same result, without having to create the temporary newSlice
variable?
Something like:
func sortSlice(sliceToSort []int) []int {
var sortedSlice []int
for 1 <= len(sliceToSort) {
indexOfSmallest := findIndexOfSmallest(sliceToSort)
smallestElement, sliceToSort = popElement(indexOfSmallest, sliceToSort)
sortedSlice = append(sortedSlice, smallestElement)
}
return sortedSlice
}
答案1
得分: 3
根据 @mkopriva 的建议,需要预先声明 smallestElement。
func sortSlice(sliceToSort []int) []int {
var sortedSlice []int
var smallestElement int
for 1 <= len(sliceToSort) {
indexOfSmallest := findIndexOfSmallest(sliceToSort)
smallestElement, sliceToSort = popElement(indexOfSmallest, sliceToSort)
sortedSlice = append(sortedSlice, smallestElement)
}
return sortedSlice
}
英文:
As @mkopriva suggested smallestElement needs to be predeclared.
func sortSlice(sliceToSort []int) []int {
var sortedSlice []int
var smallestElement int
for 1 <= len(sliceToSort) {
indexOfSmallest := findIndexOfSmallest(sliceToSort)
smallestElement, sliceToSort = popElement(indexOfSmallest, sliceToSort)
sortedSlice = append(sortedSlice, smallestElement)
}
return sortedSlice
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论