You are given a sequence a_n.
The sequences terms are written in terms of two other sequences. a_n progresses as follows:
a_n = [x_1, y_1, x_2, y_2, x_3, y_3, ...]
Decode the sequence and return x_n and y_n as functions in the form [x_n, y_n]
function decodeC(a) {
return [(n) => a((2*n + 1)), (n)=>a(n*2)]
}
// Since Node 10, we're using Mocha.
// You can use `chai` for assertions.
const chai = require("chai");
const assert = chai.assert;
// Uncomment the following line to disable truncating failure messages for deep equals, do:
// chai.config.truncateThreshold = 0;
// Since Node 12, we no longer include assertions from our deprecated custom test framework by default.
// Uncomment the following to use the old assertions:
const Test = require("@codewars/test-compat");
describe("Solution", function() {
it("should test for something", function() {
Test.assertEquals(decodeC((x) => (x)).map((x)=>{let ret = []; for(var i = 1; i < 25; i++) {ret.push(x(i))}; return ret}), decode((x) => (x)).map((x)=>{let ret = []; for(var i = 1; i < 25; i++) {ret.push(x(i))}; return ret}));
// assert.strictEqual(1 + 1, 2);
});
});
Given a matrix M and a vector v, linearly transform the vector using the matrix.
ex:
const M = [[2,0],[0,2]]
const v = [1,1]
transform(M, v); // [2,2]
More examples:
const M = [[2,2],[3,2]]
const v = [1,1]
transform(M, v); // [5,4]
const M = [[2,0],[0,2]]
const v = [2,3]
transform(M, v); // [4,6]
const M = [[2,5],[0,3]]
const v = [1,2]
transform(M, v); // [5,5]
function transpose(A) {
let arrs = []
/*
|4 5| -> |4 6|
|6 7| |5 7|
[[4,5], [6,7]] -> [[4,6], [5,7]]
*/
A.forEach((x,i)=>{
x.forEach((v,i2)=>{
if(!arrs[i2]){
arrs[i2] = [v]
}else arrs[i2].push(v)
})
})
return arrs
}
function scale(v,a) {
let ret = []
v.forEach((x,i)=>{
ret.push(x*a)
})
return ret
}
function transform(M, a) {
let m = transpose(M)
let ae = []
a.forEach((x,i)=>{
ae.push(scale(m[i], x))
})
return sum(ae)
}
function sum(v) {
let s = v[0]
v.forEach((x,i)=>{
if(i == 0) return
x.forEach((y)=>{
s[i] += y
})
})
return s
}
const chai = require("chai");
const assert = chai.assert;
chai.config.truncateThreshold = 0;
describe("Fixed tests", function() {
// |2,0;0,2| & |1,1|
it("Test 1", function() {
assert.strictEqual(transform([[2,0],[0,2]],[1,1]).toString(), [2,2].toString());
});
// it("3*4*2", function() {
// assert.strictEqual(30, 30);
// });
});