How to achieve following pattern using for loops without relying on external memory outside the loop:
I want to return 3 zeros, 6 ones and 12 tows and so on..
// the number on the left represents the current index
// the number on the right represents the value that needs to be returned
/*
0 = 0
1 = 0
2 = 0
3 = 1
4 = 1
5 = 1
6 = 1
7 = 1
8 = 1
9 = 2
10 = 2
11 = 2
12 = 2
13 = 2
14 = 2
15 = 2
16 = 2
17 = 2
18 = 2
19 = 2
20 = 2
*/
basically this will help me achieve the following star pattern:
***
******
************
************************
************************************************
I can use the following following but it's not what I really need.
let x = 3;
while (x < 30) {
console.log(x)
x = x * 2
}
In my case, I have a function that exposes an index prop that increments from 0 to a certain value x:
animation.start(({ index }) => {
// is it really possible to achieve the above patten (3, 6, 12..) using only the index prop.
})
Related
I have a task to find an order ('2 4 5 10 123' , the next value must be bigger than the previous one) in separate columns of nested array.
function createArray(n){
let result = [];
for (var i = 0 ; i < n; i++) {
result[i] = [];
for (var j = 0; j < n; j++) {
result[i][j] = (Math.random() * 5 | 0) + 6;
}
}
return result;
}
var n = prompt("Enter the size of array"), result = createArray(n);
I've created the function to fill this array with random values and I'd like to have:
Loop to compare values in separate columns
Sort columns, depending on how much values included in order:
For example, we have a column 2 4 8 3 7 and we're finding order here. We did and we have 3 values, creating the order -> 2 4 8. We stop it, since the number 3 break it and we start from this particular number: 3 8. Now we have an order having 3 nums included and 2 nums included, but we save the biggest one and than we compare these counter of max ordered nums in different columns and sort them in a way, that the column with the biggest counter is on the left side and column with the smallest one - on the right side;
[
1 3 5
2 7 7
6 9 1
7 2 2
8 3 1
]
^ ^ ^
5 3 2
I'll surely be glad to have some help, at least with comparing values in columns.
Thanks for attention.
Nothing helped, ask for help ;)
I have been running through Codility questions and and one of the questions was to count all the possible factors of a number. I looped through the whole number got the answer but it wasn't efficient of course.
I searched for the answer and got this
function solution(N) {
var i;
var NumFactors = 0;
for(i = 1;i*i < N; i++) {
if(N%i == 0) NumFactors += 2;
}
if(i*i == N) NumFactors++;
return NumFactors
}
for anyone who hasn't tried the challenge if you run solution(24) it should return 8 as number of factors which are (1, 2, 3, 4, 6, 8,12, 24)
Since the person who wrote the code didn't leave any explanation, can someone who get what's happening kindly explain to me the i*i and the reason of incrementing NumFactors by 2.
The i*i is for checking until squareRoot(N). Because if you have a divisor for a number N then you actually have two divisor. Because the division result is another divisor. For example, in case of 24,
If you take divisor 2, you will find another divisor which is 12. Because 2 X 12 = 24. If you loop through N i.e. 1 to 24 you will get the divisors like this,
2 X 12 = 24
3 X 8 = 24
4 X 6 = 24
6 X 4 = 24
8 X 3 = 24
12 X 2 = 24
24 X 1 = 24
You see we have got redundant values after squareRoot(N). That is why for optimization we are going from 1 to squareRoot(N).
Now about increase factors by 2 is already described above. For the special case when N is a perfect square number like 36 or 49 you will face a case where 6 X 6 = 36 and 7 X 7 = 49 that is why in that case we are increasing the factor by one. Because there is actually on divisor namely 6 and 7 in our case.
EDIT: Thanks to previous comments of #Bergi and #MattEllen, I have progressed in my thougths, so I repost this question.
Suppose that we have two sorted lists with duplicated values
// here arr1.length = arr2.length = 16
ar11 = 0 0 0 0 1 1 1 1 2 2 2 2 3 3 4 5
arr2 = 0 0 1 1 1 1 2 2 2 2 3 4 5 6 7 8
and we want to compute their interesection (isect)
// duplicates preserved !
isect = 0 0 1 1 1 1 2 2 2 2 3 4 5
we can use a simple linear algorithm adapted from Python version described inside: Intersection of two lists including duplicates?
// basic intersection code
// allows lists to handle duplicates
const dumb_intersect = (arrays, intervals, params) => {
const { arr1, arr2, results } = arrays
const { matches} = params
const { i1, i2 } = intervals
ii_iterate(arr1, i1, (value, rank) => {
const count = matches.get(value) || 0
matches.set(value, count + 1)
})
ii_iterate(arr2, i2, (value, rank) => {
const count = matches.get(value) || 0
if(count > 0) {
results.push(value)
if (count > 1) {
matches.set(value, count - 1)
} else {
matches.delete(value)
}
}
})
}
In this example:
matches is a global map, and it could be reused for later calls of dumb_intersect
i1 = { min:0, max: 15 } and { min:0, max: 15 } (obvious!)
ii_iterate takes a list and an interval, and applies a callback to each element of the list when its index is bound to the interval
For the sake of simplicity I give you the code of ii_iterate:
// apply a callback function to each element of a slice of an array
const ii_iterate = (arr, ii, callback) => {
return arr.slice(ii.min, ii.max + 1).map((val, idx) => {
return callback(val, idx)
})
}
TL:DR; ;-)
ALL THAT STUFF IS AWESOME, BUT I THINK I COULD DO BETTER AND FASTER !!!
Especially if we binary (dichotomic) serie of cuts of the intervals until a thresold:
// THRESOLD = 4 elements
cuts[0]: [0..15] # 1 interval * 16 elements
cuts[1]: [0..7] [8..15] # 2 * 8
cuts[2]: [0..3] [4..7] [8..11] [12..15] # 4 * 4
... and apply dumb_intersect only of overlapping intervals
Normally, for huge lists thresold is computed easily:
thresold = 1 + Math.floor(Math.log((1 + arr1.length) * (1 + arr2.length)))
N = 1000 thresold = 15
N = 1000000 thresold = 28
But this is a minor aspect of the problem. Bianry split process is also easy to do. Arr1 and Arr2 of the initial example becomes, with a thresold of 4 :
ar11 = 0 0 0 0; 1 1 1 1; 2 2 2 2; 3 3 4 5
arr2 = 0 0 1 1; 1 1 2 2; 2 2 3 4; 5 6 7 8
(remember that that could be large lists)
This problem seems to be a "turtle rallye racing" between two lists of interval :
I need some king of cursors to iterate sperately the lists
then compare intervals for INF_STRICT, SUP_STR or MATCHING
apply dumb_intersect to MATCHING(s) intervals
until it ends
Since complexity of dumb_intersect is linear O(N) I 'm not sure today that it could be optimized in O(log(N)) or O(sqrt(N)) --> so I will just use the linear version for future coding !!
Thanks to all, best regards.
Notice also that arr1 and arr2 are pre-sorted lists, with ascending order.
In that case, what is all the hassle with splitting the intervals about? We just need a merge algorithm to do this:
function intersection(arrays, intervals) {
const { arr1, arr2, results } = arrays
const { i1, i2 } = intervals
let i = i1.min, j = i2.min;
while (i <= i1.max && j <= i2.max) {
if (arr1[i] < arr2[j]) {
i++
} else if (arr1[i] > arr2[j]) {
j++
} else { // arr1[i] == arr2[j]
results.push(arr1[i])
i++
j++
}
}
}
Okay, so this is the idea.
1.I got an array with 12 numbers (a=[1,2,3,4,5,6,7,8,9,10,11,12]
2.I want to split it into 4 chunks so i did this...
a=[1,2,3,4,5,6,7,8,9,10,11,12];
var b = [];
while(a.length) {
b.push(a.splice(0,3));
}
This gave me an array with 4 elements with 3 values inside each element
i.e. [1,2,3,4] = [ 1 2 3 , 4 5 6 , 7 8 9 , 10 11 12 ]
3.Now my problem is that i would like it to be organized in a way that the first value goes into the first element, the second into the second, the third into the third, and the fourth into the fourth and it repeats the process until i got something like this:
i.e. [1,2,3,4] = [ 1 5 9 , 2 6 10 , 3 7 11 , 4 8 12 ]
This should do it;
var a = [1,2,3,4,5,6,7,8,9,10,11,12];
var chunksize = 3;
var numOfChunks = Math.ceil(a.length/ chunksize);
var b = new Array(numOfChunks);
for(var i=0; i<a.length; i++) {
var pos = i%numOfChunks;
if(!b[pos]) b[pos] = [];
b[pos].push(a[i]);
}
I have no idea to covert int like 1>=5, 6>=10, 11>=15 ...
I don't know how this call, but I think this example will explain my question...
1 = 5
2 = 5
3 = 5
4 = 5
5 = 5
6 = 10
7 = 10
8 = 10
9 = 10
10 = 10
11 = 15
//and more..
So JS
var x = 1;
var result = (???) // 5
This should do the trick:
Math.floor((x-1)/5 + 1) * 5
Simpler version (#RichardTowers):
Math.ceil(x/5) * 5
You can do this way:
var x=1;
var change= numberChange(x);
function numberChange(x){
while(x%5!=0){
x=x+1;
}
return x;
}
alert(change);
Demo Fiddle