2

I am newbie in programming and try to find out how to multiply

[1,1,0]

to

[4,9,7,2,1,6]

for the next result output

[4,9,7,2,0,0]

As you see I want to multiply each value of [1,1,0] array to each two of second array by shifting in them

[1..] * [4,9..] = [4,9]
[.1.] * [.7,2.] = [7,2]
[..0] * [..1,6] = [0,0]

As example in js i writed something like

var firstArray = [1,1,0];
var secondArray = [4,9,7,2,1,6];
var shift = secondArray / firstArray;
var startpos = 0;
var outArray = [];

for(i=0; i< firstArray.length; i++){
    for(z=i; z< shift+i; z++){
        outArray.push(firstArray[i] * secondArray[z]);
    }
}

console.log(outArray);

It may be in python

Max
  • 21
  • 2

6 Answers6

1

You can abuse zip and list slicing:

a = [1, 1, 0]
b = [4, 9, 7, 2, 1, 6]

shift = len(b) // len(a)  # use / in Python 2
li = []
for num_a, num_b1, num_b2 in zip(a, b[::shift], b[1::shift]):
    li.extend([num_a * num_b1, num_a * num_b2])

print(li)
# [4, 9, 7, 2, 0, 0]
DeepSpace
  • 78,697
  • 11
  • 109
  • 154
0

In Javascript, you could use a more functional approach by using

  • Array#reduce for iterating the factors and returning a new array,

  • Array#concat for adding a part result after multiplying to the result set,

  • Array#slice, for getting only two elements of the values array,

  • Array#map for multiplying the part array with the given factor,

  • at least use an array as start value for reduce.

var factors = [1, 1, 0],
    values = [4, 9, 7, 2, 1, 6],
    result = factors.reduce(
        (r, f, i) => r.concat(
            values
                .slice(i * 2, (i + 1) * 2)
                .map(v => f * v)
        ),
        []
    );

console.log(result);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0

You can express this as a standard matrix multiplication by making your inputs 2D arrays.

I got the multiplication algorithm from here: https://stackoverflow.com/a/27205510/5710637

The calculation would now look like:

[1,0,0]   [4,9]   [4,9]
[0,1,0] * [7,2] = [7,2]
[0,0,0]   [1,6]   [0,0]

function multiplyMatrices(m1, m2) {
    var result = [];
    for (var i = 0; i < m1.length; i++) {
        result[i] = [];
        for (var j = 0; j < m2[0].length; j++) {
            var sum = 0;
            for (var k = 0; k < m1[0].length; k++) {
                sum += m1[i][k] * m2[k][j];
            }
            result[i][j] = sum;
        }
    }
    return result;
}

var in1 = [[1, 0, 0], [0, 1, 0], [0, 0, 0]];
var in2 = [[4, 9], [7, 2], [1, 6]]

console.log(multiplyMatrices(in1, in2))
Community
  • 1
  • 1
fafl
  • 7,222
  • 3
  • 27
  • 50
0

You can do this:

  1. Iterate over first array so that you get a number to multiply.

  2. Then extract two elements from the target array and multiply, store the result in res array.

var mul = [1,1,0];


    var target = [4,9,7,2,1,6];
    var start = 0;
    var res = [];
    mul.forEach(function(v,i) {
      var arr = target.slice(start, start+2);//from start index extract two 
                                             //elements
      arr.forEach(function(val,i) {
        res.push(v * val);
      });
      start += 2;
    });

    console.log(res);
Pankaj Shukla
  • 2,657
  • 2
  • 11
  • 18
0

You can use map() on array2 and use one var for incrementing index of array1 for each two elements and then multiply current element of array1 with element from array2 that has index as that var.

var a1 = [1,1,0];
var a2 = [4,9,7,2,1,6];

var j = 0;
var result = a2.map(function(e, i) {
  if(i % 2 == 0 && i != 0) j++
  return e * a1[j];
})

console.log(result)
Nenad Vracar
  • 118,580
  • 15
  • 151
  • 176
0

In python you can use the following code. I'm assuming the length of the second array is larger and it is divisible by the length of the shorter array

def multiply(a, b):
    x, y = len(a), len(b)                        # Find lengths
    assert x % y == 0                            
    n = x / y
    result = [a[i] * b[i / n] for i in range(x)] # For each index in a, calculate the appropriate element in output by multiplying with relevant part
    print result
Rubbal
  • 779
  • 7
  • 19