go 1d array combine 2d array with append - arrays

I have two one dimensional array and I want to combine the two single arrays into one multi dimensional array with append.
How would this be done for the fastest in go?
val time []int64
val value []float64
val 2darray [][]int64, float64
Would append be the best way to do this in go?

Here's an example of how it can be done:
package main
import (
"fmt"
)
type TimeAndValue struct {
time int64
value float64
}
func main() {
times := []int64{0, 1, 2, 3, 4}
values := []float64{1.23, 2.34, 3.45, 4.56, 5.67}
timesAndValues := zip(times, values)
fmt.Println(timesAndValues)
}
func zip(ts []int64, vs []float64) []TimeAndValue {
if len(ts) != len(vs) {
panic("not same length")
}
var res []TimeAndValue
for i, t := range ts {
res = append(res, TimeAndValue{time: t, value: vs[i]})
}
return res
}
https://play.golang.org/p/1OWJ1HG1XL

Related

Convert float64 array to float32 array

I have an array of float64 and want to convert each value to float32.
I've tried:
# What I have
features64 [120]float64
# What I've tried
features32 = [120]float32(features64)
But that gives the compile error:
cannot convert features (type [120]float64) to type [120]float32
For example,
package main
func main() {
var features64 [120]float64
var features32 [len(features64)]float32
for i, f64 := range features64 {
features32[i] = float32(f64)
}
}
You can't convert one slice/array type to another. You'll need to create a new array and iterate over the original converting each element:
for i,f := range features64 {
features32[i] = float32(f)
}
Simply
var arr1 [120]float64
var arr2 [120]float32
for i, v := range arr1 {
arr2[i] = float32(v)
}

Is there any function in golang that will find the value is in array or not? [duplicate]

This question already has answers here:
How to search for an element in a golang slice
(8 answers)
Closed 7 months ago.
Suppose there is an slice having integers in it. And we have declared a variable which contains a integer value then I have to find the value from that slice without using for loop.
Using for loop I do like this:-
package main
import (
"fmt"
)
func main() {
value := 10
var interf []interface{}
for i := 1; i <= value; i++{
interf = append(interf, i)
}
fmt.Println(interf)
for _,v := range interf{
if value == v{
fmt.Println("Matched")
}
}
}
How we do this same thing without using for loop
Without a for loop, no* (see How to search for an element in a golang slice).
* Actually you could do it without a for loop using a recursive function. See solution at the end of the answer.
There is no ready function for this in the standard library, but this is how easy it is to create one yourself:
func find(what interface{}, where []interface{}) (idx int) {
for i, v := range where {
if v == what {
return i
}
}
return -1
}
And using it:
what := 10
where := []interface{}{1, 2, 3, 10, 5}
fmt.Println(find(what, where))
Output (try it on the Go Playground):
3
Also note that it would be faster and more convenient to use []int slice type instead of []interface{}:
func find(what int, where []int) (idx int) {
for i, v := range where {
if v == what {
return i
}
}
return -1
}
And then using it:
what := 10
where := []int{1, 2, 3, 10, 5}
fmt.Println(find(what, where))
Output is the same. Try this one on the Go Playground.
You could create a function that accepts slices of any type using the interface{} type, but that would require reflection to implement it, which would be slower and not worthy to be used. Instead just create a function with concrete slice types if you need, or just use the for loop in place.
For completeness, here's the solution that uses no for loops but a recursive function. This is here only for educational purposes, the solutions above are superior to this:
func find(what int, where []int) (idx int) {
if len(where) == 0 {
return -1
}
if what == where[0] {
return 0
}
if idx = find(what, where[1:]); idx < 0 {
return -1 // Not found in the rest of the slice
}
return 1 + idx
}
Try this one on the Go Playground.

N-Dimensional array swift

Is there any way to have an n dimensional array in swift? I would like to be able to make a function that creates an array with n dimensions but I cannot figure out how.
Basically something like this:
func ndarray <T> (dimensions: Int...) -> [[T]] { // What do I tell it I return?
var out
for d in dimensions {
out = Array<T>(repeating: out, count: d)
}
return out
}
The above code does not work for obvios reasons but, I think it points out the main problems I am having:
How do I define a return type
How do I actually create the array
Once created how do I traverse and populate the array
Here is the implementation of an N-Dimensional Array. It uses a normal array internally for storage and converts the multi-dimensional indices into a single index for the internal array.
struct NDimArray<T> {
let dimensions: [Int]
var data: [T]
init(dimensions: Int..., initialValue: T) {
self.dimensions = dimensions
data = Array(repeating: initialValue, count: dimensions.reduce(1, *))
}
init(dimensions: Int..., initUsing initializer: () -> T) {
self.dimensions = dimensions
data = (0 ..< dimensions.reduce(1, *)).map { _ in initializer() }
}
// Compute index into data from indices
private func computeIndex(_ indices: [Int]) -> Int {
guard indices.count == dimensions.count else { fatalError("Wrong number of indices: got \(indices.count), expected \(dimensions.count)") }
zip(dimensions, indices).forEach { dim, idx in
guard (0 ..< dim) ~= idx else { fatalError("Index out of range") }
}
var idx = indices
var dims = dimensions
var product = 1
var total = idx.removeLast()
while !idx.isEmpty {
product *= dims.removeLast()
total += (idx.removeLast() * product)
}
return total
}
subscript(_ indices: Int...) -> T {
get {
return data[computeIndex(indices)]
}
set {
data[computeIndex(indices)] = newValue
}
}
}
Example:
// Create a 3 x 4 x 5 array of String with initial value ""
var arr = NDimArray<String>(dimensions: 3, 4, 5, initialValue: "")
for x in 0 ..< 3 {
for y in 0 ..< 4 {
for z in 0 ..< 5 {
// Encode indices in the string
arr[x, y, z] = "(\(x),\(y),\(z))"
}
}
}
// Show internal storage of data
print(arr.data)
["(0,0,0)", "(0,0,1)", "(0,0,2)", "(0,0,3)", "(0,0,4)", "(0,1,0)", "(0,1,1)", "(0,1,2)", "(0,1,3)", "(0,1,4)", "(0,2,0)", "(0,2,1)", "(0,2,2)", "(0,2,3)", "(0,2,4)", "(0,3,0)", "(0,3,1)", "(0,3,2)", "(0,3,3)", "(0,3,4)", "(1,0,0)", "(1,0,1)", "(1,0,2)", "(1,0,3)", "(1,0,4)", "(1,1,0)", "(1,1,1)", "(1,1,2)", "(1,1,3)", "(1,1,4)", "(1,2,0)", "(1,2,1)", "(1,2,2)", "(1,2,3)", "(1,2,4)", "(1,3,0)", "(1,3,1)", "(1,3,2)", "(1,3,3)", "(1,3,4)", "(2,0,0)", "(2,0,1)", "(2,0,2)", "(2,0,3)", "(2,0,4)", "(2,1,0)", "(2,1,1)", "(2,1,2)", "(2,1,3)", "(2,1,4)", "(2,2,0)", "(2,2,1)", "(2,2,2)", "(2,2,3)", "(2,2,4)", "(2,3,0)", "(2,3,1)", "(2,3,2)", "(2,3,3)", "(2,3,4)"]
print(arr[2, 2, 2]) // "(2,2,2)"
print(arr[3, 0, 0]) // Fatal error: Index out of range
print(arr[0, 4, 0]) // Fatal error: Index out of range
print(arr[2]) // Fatal error: Wrong number of indices: got 1, expected 3
Initializing an Array with a Reference Type
As #DuncanC noted in the comments, you have to be careful when initializing an array with a value which is a reference type, because the array will be filled with references to the object and modifying the object at any index will modify all of them.
To solve this, I added a second initializer:
init(dimensions: Int..., initUsing initializer: () -> T)
which takes a closure () -> T which can be used to create a new object for each element of the array.
For example:
class Person {
var name = ""
}
// Pass a closure which creates a `Person` instance to fill the array
// with 25 person objects
let arr = NDimArray(dimensions: 5, 5, initUsing: { Person() })
arr[3, 3].name = "Fred"
arr[2, 2].name = "Wilma"
print(arr[3, 3].name, arr[2, 2].name)
Fred Wilma
Nope, it's not possible. Array dimensions is something that needs to be determined at compile time, while the argument you want to pass to the initializer will not be known until runtime. If you really want to achieve something like this, then you'll need to move the array indexing from compile time to runtime, e.g. by accessing the array via an array of indexes. Still you don't have compile validation, since the array length can at runtime to not match the dimensions of the array.
This problem is similar to the one that attempts to convert a tuple to an array.

GO array expansion

I was testing some go array initialization and general usage but I can not figure out why can not expand an array initialized with a defined length?
package main
func main() {
arr0 := []int{1, 2, 3} //
add(arr0...) // OK
arr1 := [3]int{1, 2, 3} //
slice := arr1[:] //
add(slice...) // OK
arr2 := [3]int{} //
arr2[0] = 1 //
arr2[1] = 2 //
arr2[3] = 3 //
add(arr2...) // cannot use arr2 (type [3]int) as type
// []int in argument to add
}
func add(terms ...int) (sum int) {
for _, term := range terms {
sum += term
}
return
}
As other said:
[n]type - Array (fixed size)
[]type - Slice
A quick solution for your case would be a simple conversion from array to slice.
add(arr2[:]...)
That will just make it a slice pointing to the storage of arr2 (not a copy)
when use the operator ... as a tail, such as T.... T should always be slice.

Go: range and len of multidimensional array?

Is it possible to use range and len on a multidimensional array?
Either with var a [3]int8 or
package main
func main () {
var a [3][5]int8
for h := range a {
println(h)
}
println(len(a))
}
Both produce
0
1
2
3
?
Thanks to dystroy's answer, here's an example of writing and reading a 3-dimensional array i was able to adapt (posting here because I had much trouble finding any examples of this, so maybe this will help someone else):
package main
func main() {
var a [3][5][7]uint8
//write values to array
for x, b := range a {
for y, c := range b {
for z, _ := range c {
a[x][y][z] = uint8(x*100+y*10+z)
}
}
}
//read values from array
for _, h := range a {
for _, i := range h {
for _, j := range i {
print(j, "\t")
}
println()
}
println()
}
}
In Go as in most languages, what you call a multidimensional array is an array of arrays. The len operator only gives you the length of the "external" array.
Maybe the var declaration could be clearer for you if you see it as
var a [3]([5]int8)
which also compiles. It's an array of size 3 whose elements are arrays of size 5 of int8.
package main
import "fmt"
func main() {
var a [3][5]int8
for _, h := range a {
fmt.Println(len(h)) // each one prints 5
}
fmt.Println(len(a)) // prints 3, the length of the external array
}
outputs
5
5
5
3
To loop safely through the whole matrix, you can do this :
for _, h := range a {
for _, cell := range h {
fmt.Print(cell, " ")
}
fmt.Println()
}
If you need to change the content, you may do
for i, cell := range h { // i is the index, cell the value
h[i] = 2 * cell
}
The solution with range is already provided so i'll talk about how to use length (len) to go through a multi-dimesional array in golang.
So if u have an array arr[][] :
[1,2,3]
[4,5,6]
Now the len(arr) will output = 2.
while len(arr[1]) will output = 3.
Sample code is provided here : https://play.golang.org/p/XerzPhkQrhU
No, the first one produces 0,1,2 ( index of in the range )
http://play.golang.org/p/0KrzTRWzKO
And the second one produces 3 ( the length of the array ).
http://play.golang.org/p/0esKFqQZL0
In both cases you're using the outermost array.

Resources