Add all the A tasks
This commit is contained in:
parent
2dd7375f96
commit
051504d65b
1608 changed files with 18584 additions and 0 deletions
35
Task/Array-concatenation/Go/array-concatenation-1.go
Normal file
35
Task/Array-concatenation/Go/array-concatenation-1.go
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
// Example 1: Idiomatic in Go is use of the append function.
|
||||
// Elements must be of identical type.
|
||||
a := []int{1, 2, 3}
|
||||
b := []int{7, 12, 60} // these are technically slices, not arrays
|
||||
c := append(a, b...)
|
||||
fmt.Println(c)
|
||||
|
||||
// Example 2: Polymorphism.
|
||||
// interface{} is a type too, one that can reference values of any type.
|
||||
// This allows a sort of polymorphic list.
|
||||
i := []interface{}{1, 2, 3}
|
||||
j := []interface{}{"Crosby", "Stills", "Nash", "Young"}
|
||||
k := append(i, j...) // append will allocate as needed
|
||||
fmt.Println(k)
|
||||
|
||||
// Example 3: Arrays, not slices.
|
||||
// A word like "array" on RC often means "whatever array means in your
|
||||
// language." In Go, the common role of "array" is usually filled by
|
||||
// Go slices, as in examples 1 and 2. If by "array" you really mean
|
||||
// "Go array," then you have to do a little extra work. The best
|
||||
// technique is almost always to create slices on the arrays and then
|
||||
// use the copy function.
|
||||
l := [...]int{1, 2, 3}
|
||||
m := [...]int{7, 12, 60} // arrays have constant size set at compile time
|
||||
var n [len(l) + len(m)]int
|
||||
copy(n[:], l[:]) // [:] creates a slice that references the entire array
|
||||
copy(n[len(l):], m[:])
|
||||
fmt.Println(n)
|
||||
|
||||
}
|
||||
57
Task/Array-concatenation/Go/array-concatenation-2.go
Normal file
57
Task/Array-concatenation/Go/array-concatenation-2.go
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// Generic version
|
||||
// Easier to make the generic version accept any number of arguments,
|
||||
// and loop trough them. Otherwise there will be lots of code duplication.
|
||||
func ArrayConcat(arrays ...interface{}) interface{} {
|
||||
if len(arrays) == 0 {
|
||||
panic("Need at least one arguemnt")
|
||||
}
|
||||
var vals = make([]*reflect.SliceValue, len(arrays))
|
||||
var arrtype *reflect.SliceType
|
||||
var totalsize int
|
||||
for i,a := range arrays {
|
||||
v := reflect.NewValue(a)
|
||||
switch t := v.Type().(type) {
|
||||
case *reflect.SliceType:
|
||||
if arrtype == nil {
|
||||
arrtype = t
|
||||
} else if t != arrtype {
|
||||
panic("Unequal types")
|
||||
}
|
||||
vals[i] = v.(*reflect.SliceValue)
|
||||
totalsize += vals[i].Len()
|
||||
default: panic("not a slice")
|
||||
}
|
||||
}
|
||||
ret := reflect.MakeSlice(arrtype,totalsize,totalsize)
|
||||
targ := ret
|
||||
for _,v := range vals {
|
||||
reflect.Copy(targ, v)
|
||||
targ = targ.Slice(v.Len(),targ.Len())
|
||||
}
|
||||
return ret.Interface()
|
||||
}
|
||||
|
||||
// Type specific version
|
||||
func ArrayConcatInts(a, b []int) []int {
|
||||
ret := make([]int, len(a) + len(b))
|
||||
copy(ret, a)
|
||||
copy(ret[len(a):], b)
|
||||
return ret
|
||||
}
|
||||
|
||||
func main() {
|
||||
test1_a, test1_b := []int{1,2,3}, []int{4,5,6}
|
||||
test1_c := ArrayConcatInts(test1_a, test1_b)
|
||||
fmt.Println(test1_a, " + ", test1_b, " = ", test1_c)
|
||||
|
||||
test2_a, test2_b := []string{"a","b","c"}, []string{"d","e","f"}
|
||||
test2_c := ArrayConcat(test2_a, test2_b).([]string)
|
||||
fmt.Println(test2_a, " + ", test2_b, " = ", test2_c)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue