-
-
Save digitalist/e3f643a10907208f9b2ca455e2c9db82 to your computer and use it in GitHub Desktop.
go/py merge arrays/lists
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
слегка усложнил задачу, чтобы были пересечения | |
идею про два счетчика вспомнил из какой-то сортировки | |
логика полностью аналогична питоновской | |
*/ | |
package main | |
import "fmt" | |
func main() { | |
a := []int{1, 5, 6, 8, 20, 800, 900} | |
b := []int{7, 10, 13, 81, 99, 100, 191} | |
// out: [1 5 6 7 8 10 13 20 81 99 100 191 800 900] | |
//... здесь предполагается код для | |
// выбора длиннейшего из слайсов | |
lena := len(a) | |
lenb := len(b) | |
i := 0 | |
j := 0 | |
res := make([]int, 0) | |
for (j < lenb) && (i < lena) { | |
if a[i] < b[j] { | |
res = append(res, a[i]) | |
i++ | |
} else { | |
res = append(res, b[j]) | |
j++ | |
} | |
} | |
res = append(res, a[i:]...) | |
res = append(res, b[j:]...) | |
fmt.Printf("%v", res) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
a = [1, 5, 6, 8, 20, 800,900] | |
b = [7, 10, 13, 81, 99,100,500,600] | |
def srt(a,b): | |
size_1 = len(a) | |
size_2 = len(b) | |
res = [] | |
i, j = 0, 0 | |
while i < size_1 and j < size_2: | |
if a[i] < b[j]: | |
res.append(a[i]) | |
i += 1 | |
else: | |
res.append(b[j]) | |
j += 1 | |
res = res + a[i:] + b[j:] | |
print(res) | |
srt(a,b) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment