Combination Algorithm without sort() - javascript

I need results starting from the beginning with 123 as shown below. I want to print from the beginning without sort().
1 2 3
1 2 4
1 2 5
.....
3 5 6
4 5 6
But, The results are shown below.
4 5 6
3 5 6
2 5 6
1 5 6
3 4 6
2 4 6
1 4 6
2 3 6
1 3 6
1 2 6
3 4 5
2 4 5
1 4 5
2 3 5
1 3 5
1 2 5
2 3 4
1 3 4
1 2 4
1 2 3
result = "";
var N = 6;
var M = 3;
var arr = new Array(M);
combi(N, M, arr, M);
alert(result);
function combi(n, r, arr, sz) {
var i = n + 1;
while (i-- > r) {
// choose the first element
arr[r - 1] = i;
if (r > 1) { // if still needs to choose
// recursive into smaller problem
combi(i - 1, r - 1, arr, sz);
} else {
// print out one solution
var j = -1;
while (++j < sz) {
result += arr[j] + " ";
}
result += "\n";
}
}
}

Beside the inverting of all, you could start with one and iterat until the wanted value.
function combination(n, r) {
function iter(i, temp) {
if (temp.length === r) { // set complete
result.push(temp.join(' ')); // join values
return;
}
if (i + r > n + 1 + temp.length) { // exit early
return;
}
iter(i + 1, temp.concat(i)); // take the value
iter(i + 1, temp); // go without the value
}
var result = []; // result set
iter(1, []); // start with 1 and
return result; // empty array for collecting sets
}
console.log(combination(6, 3));
.as-console-wrapper { max-height: 100% !important; top: 0; }

You can revert the order by changing your code this way:
function combi(n, r, arr, sz) {
var i = r - 1;
while (i++ < n) {
// choose the first element
arr[r - 1] = i;
if (r > 1) { // if still needs to choose
// recursive into smaller problem
combi(i - 1, r - 1, arr, sz);
} else {
// print out one solution
var j = -1;
while (++j < sz) {
result += arr[j] + " ";
}
result += "\n";
}
}
}

Related

Generate list of numbers with a character in between every nth numbers

I am trying to generate a string with the numbers from 1 to 1000 with the character '*' after every 5th numbers e.g.,
1 2 3 4 5 * 6 7 8 9 10 * 11 12 13 14 15 * 16 17 18 19 20 * …
Here's my attempt but as you can it is not working as expected:
const insertCharacter = () => {
let contain = [];
let new_value;
for (let i = 1; i <= 1000; i++) {
contain += [i];
if (contain.length)
contain.toString()
let parts = contain.match(/.{0,5}/g);
new_value = parts.join("*");
}
return new_value;
}
console.log(insertCharacter());
I would suggest using the modulus here:
var output = "";
for (var i=1; i <= 1000; ++i) {
if (i > 1 && i % 5 == 1) output += " *";
if (i > 1) output += " ";
output += i;
}
console.log(output);
Older JavaScript format version, but still valid.
The % is called a Remainder and is the key here.
Sample code is only counting to 50.
Edit: Changed to commented version from mplungjan.
Edit 2, for comment from georg. If you do not want a trailing Asterisk, some options:
count to 999, then add 1000 to the result
use result.substring(0,result.length-2)
var i, result = "";
for(i=1;i<51;i++){
result += i + (i % 5 ? " " : " * ");
}
console.log(result);
Please refer this code.
const result = [...Array(1000)].map((val, index) => (index + 1) % 5 === 0 ? index + 1 + " *" : index + 1);
console.log(result.join(' '));
Assuming you don't want the trailing character, you could simply do:
Array.from(Array(1000 / 5),
(_, i) => (i *= 5, `${i+1} ${i+2} ${i+3} ${i+4} ${i+5}`))
.join(' * ');
We don't need to iterate 1000 times: we know all the numbers already and we know we'll have 200 groups of 5 numbers each. We just need to produce those groups and join them up together.
But there are many issues with this:
The interval is hardcoded
The separator is hardcoded
What if we can't split evenly the numbers?
Let say we need to insert a | after every 4th numbers of 10 numbers:
1 2 3 4 | 5 6 7 8 | 9 10
We have 2 groups of 4 numbers each and 1 group with the last two numbers.
(I am going to annotate these examples so you can hopefully connect them with the full example below)
The first two groups can be produced as follow:
Array.from(Array(Math.floor(10 / 4)), (_, i) => (i*=4, [i+1, i+2, i+3, i+4].join(' ')))
// ^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^
// imax tmpl
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// nseq
//
//=> ['1 2 3 4', '5 6 7 8']
The last group with:
Array(10 % 4).fill(0).map((_, i) => Math.floor(10 / 4) * 4 + i + 1).join(' ')
// ^^^^^^ ^^^^^^^^^^^^^^^^^^
// tmax imax
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// nseq
//
//=> '9 10'
Then you basically do:
['1 2 3 4', '5 6 7 8'].concat('9 10').join(' | ')
//=> '1 2 3 4 | 5 6 7 8 | 9 10'
Demo
console.log("sprintnums(10, 7, '|') -> " + sprintnums(10, 7, '|'));
console.log("sprintnums(10, 4, '|') -> " + sprintnums(10, 4, '|'));
console.log("sprintnums(10, 2, '|') -> " + sprintnums(10, 2, '|'));
console.log("sprintnums(10, 1, '|') -> " + sprintnums(10, 1, '|'));
console.log(`
In your case:
${sprintnums(1000, 5, '*')}
`);
<script>
function sprintnums(n, x, c) {
const tmpl = Array(x).fill(0);
const nseq = (arr, mul) => arr.map((_, i) => mul * x + i + 1).join(' ');
const imax = Math.floor(n / x);
const tmax = n % x;
const init = Array.from(Array(imax), (_, mul) => nseq(tmpl, mul));
const tail = tmax ? nseq(Array(tmax).fill(0), imax) : [];
return init.concat(tail).join(` ${c} `);
}
</script>
check this code
function insertChar() {
let res = '';
for (let i = 0; i < 100; i++) {
res += i;
if (i % 5 == 0) {
res += '*'
}
}
return res;
}
console.log(insertChar());
let str = '';
for(let i = 1; i <=1000;i++){
str+=i;
if(i - 1 % 5 ===0) str+='*';
}
console.log(str)

find sum of multiples 3 and 5, JS

I'm given a number and I need to find the sum of the multiples of 3 and 5 below the number.
For example:
20 => 78 = 3 + 5 + 6 + 9 + 10 + 12 + 15 + 18
My code works, but not for numbers greater than 1,000,000 (I tested it for 100,000 - it gives the result with 2sec delay). So, it should be optimized. Could someone help me? Why is my code slow? Thanks.
My logic is as follows:
add multiples to an array
filter duplicate values
sum all values
my code:
function sumOfMultiples(number) {
let numberBelow = number - 1;
let numberOfThrees = Math.floor(numberBelow / 3);
let numberOfFives = Math.floor(numberBelow / 5);
let multiples = [];
let multipleOfThree = 0;
let multipleOfFive = 0;
for (var i = 0; i < numberOfThrees; i++) {
multiples.push(multipleOfThree += 3);
}
for (var j = 0; j < numberOfFives; j++) {
multiples.push(multipleOfFive += 5);
}
return multiples
.filter((item, index) => multiples.indexOf(item) === index)
.reduce((a, b) => a + b);
}
You can also do this without using any loops.
For example if N is 1000, the sum of all multiples of 3 under 1000 is 3 + 6 + 9 ..... 999 => 3( 1 + 2 + 3 .... 333)
Similarly for 5, sum is 5(1 + 2 + 3 .... 200). But we have to subtract common multiples like 15, 30, 45 (multiples of 15)
And sum of first N natural numbers is N*(N+1)/2;
Putting all of this together
// Returns sum of first N natural numbers
const sumN = N => N*(N+1)/2;
// Returns number of multiples of a below N
const noOfMulitples = (N, a) => Math.floor((N-1)/a);
function sumOfMulitples(N) {
const n3 = noOfMulitples(N, 3); // Number of multiples of 3 under N
const n5 = noOfMulitples(N, 5); // Number of multiples of 5 under N
const n15 = noOfMulitples(N, 15); // Number of multiples of 3 & 5 under N
return 3*sumN(n3) + 5*sumN(n5) - 15*sumN(n15);
}
You can just run a loop from 1 to number, and use the modulo operator % to check if i divides 3 or 5:
function sumOfMultiples(number) {
var result = 0;
for (var i = 0; i < number; i++) {
if (i % 5 == 0 || i % 3 == 0) {
result += i;
}
}
return result;
}
console.log(sumOfMultiples(1000));
console.log(sumOfMultiples(100000));
console.log(sumOfMultiples(10000000));
You can do that just using a single loop.
function sumOfMultiples(number) {
let sum = 0;
for(let i = 1; i < number; i++){
if(i % 3 === 0 || i % 5 === 0){
sum += i;
}
}
return sum;
}
console.time('t');
console.log(sumOfMultiples(100000))
console.timeEnd('t')
You can do something like this
Set the difference equal to 5 - 3
Start loop with current as 0, keep looping until current is less than number,
Add 3 to current in every iteration,
Add difference to current and check if it is divisible by 5 only and less than number, than add it final result,
Add current to final result
function sumOfMultiples(number) {
let num = 0;
let difference = 5 - 3
let current = 0
while(current < number){
current += 3
let temp = current + difference
if((temp % 5 === 0) && (temp %3 !== 0) && temp < number ){
num += temp
}
difference += 2
if(current < number){
num += current
}
}
return num
}
console.log(sumOfMultiples(20))
console.log(sumOfMultiples(1000));
console.log(sumOfMultiples(100000));
console.log(sumOfMultiples(10000000));
you can do something like this
function multiplesOfFiveAndThree(){
let sum = 0;
for(let i = 1; i < 1000; i++) {
if (i % 3 === 0 || i % 5 === 0) sum += i;
}
return sum;
}
console.log(multiplesOfFiveAndThree());

Getting a array.push to do nothing

Trying to figure out a way that if the results of my loop equals undefined, to have my array.push do nothing. this is my code.
barcodes.sort();
var first = barcodes.slice(0, (barcodes.length + 1) / 2);
var second = barcodes.slice(((barcodes.length + 1) / 2), barcodes.length);
var result = [];
for (var i = 0; i < barcodes.length; i++) {
if (i % 2 === 0) {
result.push(first[i / 2]);
result.push(second[parseInt(i)/2]);
}else {
}
}
getting results when its odd as such: (when even is fine)
array results (12) ["1001", "9003", "3001", "9005", "3002", "9006", "3003", "9007", "9001", "9008", "9002", undefined]
result.push(second[parseInt(i)/2]);
This is undefined if you have an odd number because second.length is one less than half of barcodes. So if barcodes.length == 11, that second push is pushing second[5] when second.length == 5. This doesn't exist since indices start at 0
barcodes = ["1001", "9003", "3001", "9005", "3002", "9006", "3003", "9007", "9001", "9008", "9002"];
barcodes.sort();
var first = barcodes.slice(0, (barcodes.length + 1) / 2);
var second = barcodes.slice(((barcodes.length + 1) / 2), barcodes.length);
var result = [];
for (var i = 0; i < barcodes.length; i++) {
if (i % 2 === 0) {
result.push(first[i / 2]);
if(second[(i / 2)]) {
result.push(second[(i / 2)]);
}
}
}
console.log(result);
You could add this line: if(second[(i / 2)]) to avoid the undefined but again I'm not 100% sure what your end goal is so I'm not sure if this fully answers the question
You could take the barcode array and take an ofset for the second value for pushing.
The adjustment for the length keeps same element groups and add only at the end an undefined.
Examples:
given
barcode 0 1 2 3 4 5 6 7 8
first 0 1 2 3 4
second 5 6 7 8
result
0 5 1 6 2 7 3 8 4 undefined
given
barcode 0 1 2 3 4 5 6 7 8 9
first 0 1 2 3 4
second 5 6 7 8 9
result
0 5 1 6 2 7 3 8 4 9
var barcodes = [0, 1, 2, 3, 4, 5, 6, 7, 8],
result = [],
i,
l = (barcodes.length + 1) >> 1;
for (var i = 0; i < l; i++) {
result.push(barcodes[i], barcodes[i + l]);
}
console.log(result);

Split number into 4 random numbers

I want to split 10 into an array of 4 random numbers, but neither can be 0 or higher than 4. For example [1,2,3,4], [1,4,4,1] or [4,2,3,1].
I think it's an easy question, but for some reason I can't think of how to do this. If someone has some instruction that would be very helpful!
Edit:
This is the code I have now, but I generates also a total number under 10:
let formation = [];
let total = 0;
for (let i = 0; i < 4; i ++) {
if (total < 9) {
formation[i] = Math.floor(Math.random() * 4) + 1;
} else {
formation[i] = 1;
}
}
You could create all possible combinations and pick a random array.
function get4() {
function iter(temp) {
return function (v) {
var t = temp.concat(v);
if (t.length === 4) {
if (t.reduce(add) === 10) {
result.push(t);
}
return;
}
values.forEach(iter(t));
};
}
const
add = (a, b) => a + b,
values = [1, 2, 3, 4],
result = [];
values.forEach(iter([]));
return result;
}
console.log(get4().map(a => a.join(' ')));
.as-console-wrapper { max-height: 100% !important; top: 0; }
An algorithm for getting random values without a list of all possible combinations
It works by using a factor for the random value and an offset, based on the actual sum, index, minimum sum which is needed for the next index, and the maximum sum.
The offset is usually the minimum sum, or the greater value of the difference of sum and maximum sum. For getting the factor, three values are taken for the minimum for multiplying the random value.
The table illustrates all possible values of the sum and the needed iterations, based on a given value and the iteration for getting all values.
At the beginning the sum is the value for distribution in small parts. The result is the second block with a rest sum of 14 ... 10, because it is possible to take a value of 1 ... 5. The third round follows the same rules. At the end, the leftover sum is taken as offset for the value.
An example with 1, ..., 5 values and 5 elements with a sum of 15 and all possibilities:
min: 1
max: 5
length: 5
sum: 15
smin = (length - index - 1) * min
smax = (length - index - 1) * max
offset = Math.max(sum - smax, min)
random = 1 + Math.min(sum - offset, max - offset, sum - smin - min)
index sum sum min sum max random offset
------- ------- ------- ------- ------- -------
_ 0 15 4 20 5 1
1 14 3 15 5 1
1 13 3 15 5 1
1 12 3 15 5 1
1 11 3 15 5 1
_ 1 10 3 15 5 1
2 13 2 10 3 3
2 12 2 10 4 2
2 11 2 10 5 1
2 10 2 10 5 1
2 9 2 10 5 1
2 8 2 10 5 1
2 7 2 10 5 1
2 6 2 10 4 1
_ 2 5 2 10 3 1
3 10 1 5 1 5
3 9 1 5 2 4
3 8 1 5 3 3
3 7 1 5 4 2
3 6 1 5 5 1
3 5 1 5 4 1
3 4 1 5 3 1
3 3 1 5 2 1
_ 3 2 1 5 1 1
4 5 0 0 1 5
4 4 0 0 1 4
4 3 0 0 1 3
4 2 0 0 1 2
4 1 0 0 1 1
The example code takes the target 1, ..., 4 with a length of 4 parts and a sum of 10.
function getRandom(min, max, length, sum) {
return Array.from(
{ length },
(_, i) => {
var smin = (length - i - 1) * min,
smax = (length - i - 1) * max,
offset = Math.max(sum - smax, min),
random = 1 + Math.min(sum - offset, max - offset, sum - smin - min),
value = Math.floor(Math.random() * random + offset);
sum -= value;
return value;
}
);
}
console.log(Array.from({ length: 10 }, _ => getRandom(1, 4, 4, 10).join(' ')));
.as-console-wrapper { max-height: 100% !important; top: 0; }
The simplest solution is brute force.
Make a while loop to nest your calculations in
In the loop, create an empty array and fill it with random values until length is reached
Check if the sum of the array is your desired value, and if it is then break the loop
The above should run until you have a result.
Two things worth considering though.
Your can easily test if a solution is at all possible by calculating, that length-of-array times minimum-value isn't more than the sum and length-of-array times maximum-value isn't less than the sum.
A loop based on random conditions could potentially run forever, so a maximum amount of iterations might be desirable.
Both of these points are considered in the snippet below:
function randomNumber(max, min) {
while (true) {
var r = Math.round(Math.random() * max);
if (r >= min) {
return r;
}
}
}
function splitXintoYComponentsBetweenMaxAndMin(numberToSplit, numberOfSplits, maxValue, minValue, onUpdate) {
if (minValue === void 0) {
minValue = 1;
}
//Test that a result can exist
if (maxValue * numberOfSplits < numberToSplit || minValue * numberOfSplits > numberToSplit) {
return new Promise(function(resolve, reject) {
resolve(false);
});
}
//Create returner array
var arr = [];
var accumulator = 0;
while (arr.length < numberOfSplits) {
var val = randomNumber(Math.floor(numberToSplit / numberOfSplits), minValue);
accumulator += val;
arr.push(val);
}
return new Promise(function(resolve, reject) {
function runTest() {
var d = Date.now();
var localMaxValue = Math.min(maxValue, Math.ceil((numberToSplit - accumulator) / 4));
//Combination loop
while (accumulator < numberToSplit && Date.now() - d < 17) {
var index = Math.round(Math.random() * (arr.length - 1));
if (arr[index] >= maxValue) {
continue;
}
var r = randomNumber(localMaxValue, minValue);
while (arr[index] + r > maxValue || accumulator + r > numberToSplit) {
if (Date.now() - d >= 17) {
break;
}
r = randomNumber(localMaxValue, minValue);
}
if (arr[index] + r > maxValue || accumulator + r > numberToSplit) {
continue;
}
arr[index] += r;
accumulator += r;
}
if (accumulator < numberToSplit) {
if (onUpdate !== void 0) {
onUpdate(arr);
}
requestAnimationFrame(runTest);
} else {
resolve(arr);
}
}
runTest();
});
}
//TEST
var table = document.body.appendChild(document.createElement('table'));
table.innerHTML = "<thead><tr><th>Number to split</th><th>Number of splits</th><th>Max value</th><th>Min value</th><th>Run</th></tr></thead>" +
"<tbody><tr><th><input id=\"number-to-split\" value=\"10\" type=\"number\" min=\"1\"/></th><th><input id=\"number-of-splits\" value=\"4\" type=\"number\" min=\"1\"/></th><th><input id=\"max-value\" type=\"number\" min=\"1\" value=\"4\"/></th><th><input id=\"min-value\" type=\"number\" min=\"1\" value=\"1\"/></th><th><input id=\"run\" type=\"button\" value=\"Run\"/></th></tr></tbody>";
var output = document.body.appendChild(document.createElement('pre'));
output.style.overflowX = "scroll";
document.getElementById("run").onclick = function() {
splitXintoYComponentsBetweenMaxAndMin(parseInt(document.getElementById("number-to-split").value, 10), parseInt(document.getElementById("number-of-splits").value, 10), parseInt(document.getElementById("max-value").value, 10), parseInt(document.getElementById("min-value").value, 10))
.then(function(data) {
if (data !== false) {
output.textContent += data.join("\t") + '\n';
} else {
output.textContent += 'Invalid data\n';
}
});
};
EDIT 1 - Big calculations
Using requestAnimationFrame and Promises the code can now execute asynchronously, which allows for longer calculation time without bothering the user.
I also made the random function scale with the remaining range, greatly reducing the amount of calculations needed for big numbers.
A litte late to the show, but I found this a fun task to think about so here you go. My approach does not need to create all partitions, it also does not rely on pure luck of finding a random match, it is compact and it should be unbiased.
It works efficiently even when large values are used, as long as max is not too limiting.
const len = 4;
const total = 10;
const max = 4;
let arr = new Array(len);
let sum = 0;
do {
// get some random numbers
for (let i = 0; i < len; i++) {
arr[i] = Math.random();
}
// get the total of the random numbers
sum = arr.reduce((acc, val) => acc + val, 0);
// compute the scale to use on the numbers
const scale = (total - len) / sum;
// scale the array
arr = arr.map(val => Math.min(max, Math.round(val * scale) + 1));
// re-compute the sum
sum = arr.reduce((acc, val) => acc + val, 0);
// loop if the sum is not exactly the expected total due to scale rounding effects
} while (sum - total);
console.log(arr);
Basically you need the partitions (See https://en.wikipedia.org/wiki/Partition_(number_theory)) of 10 and apply your conditions on the resulting set.
// Partition generator taken from
// https://gist.github.com/k-hamada/8aa85ac9b334fb89ac4f
function* partitions(n) {
if (n <= 0) throw new Error('positive integer only');
yield [n];
var x = new Array(n);
x[0] = n;
for (var i = 1; i < n; i++) x[i] = 1;
var m = 0, h = 0, r, t;
while (x[0] != 1) {
if (x[h] == 2) {
m += 1;
x[h] = 1;
h -= 1;
} else {
r = x[h] - 1;
x[h] = r;
t = m - h + 1;
while (t >= r) {
h += 1;
x[h] = r;
t -= r;
}
m = h + (t !== 0 ? 1 : 0);
if (t > 1) {
h += 1;
x[h] = t;
}
}
yield x.slice(0, m + 1);
}
}
results = [];
// Get all possible partitions for your number
for (var partition of partitions(10)) {
// Apply your conditions (must be 4 numbers, none of them greater than 4)
if(partition.length != 4 || partition.some((x) => x > 4)) continue;
results.push(partition);
}
console.log(results);
Given that:
In a collection of n positive numbers that sum up to S, at least one of them will be less than S divided by n (S/n)
and that you want a result set of exactly 4 numbers,
you could use the following algorithm:
Get a random number from range [1, floor(S/n)], in this case floor(10/4) = 2, so get a random number in the range of [1,2]. Lets mark it as x1.
Get a random number from range [1, floor((S - x1)/(n - 1))]. Lets mark it as x2.
Get a random number from range [1, floor((S - x1 - x2)/(n - 2))].
Continue until you get x(n-1).
Get the last number by doing S - x1 - x2 .... - x(n-1).
Finally, extend the above algorithm with a condition to limit the upper limit of the random numbers.
In n steps, you can get a collection.
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function getRandomCollection(min, max, length, sum) {
var collection = [];
var leftSum = sum - (min - 1);
for(var i = 0; i < length - 1; i++) {
var number = getRandomInt(min, Math.min(Math.ceil(leftSum/(length - i)), max));
leftSum -= number;
collection.push(number);
}
leftSum += min - 1;
while(leftSum > max) {
var randomIndex = Math.floor(Math.random() * collection.length);
if(collection[randomIndex] < max) {
collection[randomIndex]++;
leftSum--;
}
}
collection.push(leftSum);
return collection;
}
console.log(getRandomCollection(1, 4, 4, 10).join(' + ') + ' = 10');
console.log(getRandomCollection(3, 20, 10, 100).join(' + ') + ' = 100');
Reference
My answer using the same algorithm for another question
Quick and simple but biased and nondeterministically terminating
function partition(sum, len, min, max) {
const a = Array(len).fill(min)
while (a.reduce((acc,val)=>acc+val) < sum) {
const i = Math.random()*len|0
if (a[i] < max) a[i]++
}
return a
}
console.log(Array(10).fill().map(_=>partition(10, 4, 1, 4).join(' ')))
.as-console-wrapper { max-height: 100% !important; top: 0; }
The while loop can loop forever with an infinitesimal probability. To prevent this, you can keep another array of "valid indexes" and delete keys of it when the value reaches max.
this calculates a random number from 1 to 4
wrap it on a function to your needs to generate the arrays
Math.floor(Math.random() * 4) + 1
var randomNumber = Math.floor(Math.random() * 4) + 1 ;
console.log(randomNumber);
It was too easy.
var values = null;
while(true) {
var currentSum = 0;
var expectedSum = 10;
values = [];
while(expectedSum !== currentSum) {
//var value = Math.floor(Math.random() * 9) + 1;
var value = Math.floor(Math.random() * 4) + 1;
if(value + currentSum > expectedSum) {
continue;
}
currentSum += value;
values.push(value);
}
if(values.length === 4) {
break;
} else {
console.log('false iteration')
}
}
console.log(values);

Counting the occurence of a string after a for loop

I am working on a personal project, with Javascript. I am at an intermediary level.
I have created a for loop that gives me a list of random numbers. For each "i", there is one pair of integers. The integers are always positive, different and within a certain range (ex. between 1 and 10).
Let's say after I run the loop, I get this:
1 vs 3
4 vs 7
5 vs 8
2 vs 3
7 vs 5
3 vs 4
1 vs 2
3 vs 5
3 vs 1
5 vs 7
... and so on...
Now, how do I count the occurence of each pair of occurence. For example, I would like to be able to have:
3 vs 1: occurred 2 times
7 vs 5: occurred 2 times
3 vs 5: occurred 1 time
And so on...
The order does not matter, so I consider that 3 vs 1 and 1 vs 3 is the same thing. I realize that may complicate things.
// Generate Pairs
var randomPairs = []
for (var i = 0; i < 10; ++i) {
var randomPair = [Math.floor(Math.random() * 4) + 1, Math.floor(Math.random() * 4) + 1]
randomPairs.push(randomPair)
}
// Count Pairs
var randomPairsCounted = []
for (var i = 0; i < randomPairs.length; ++i) {
var a = randomPairs[i][0]
var b = randomPairs[i][1]
if (a > b) {
var t = a
a = b
b = t
}
var doublicate = false
for (var j = 0; j < randomPairsCounted.length; ++j) {
if (randomPairsCounted[j][0] == a && randomPairsCounted[j][1] == b) {
randomPairsCounted[j][2]++
doublicate = true
break
}
}
if (doublicate == false) {
randomPairsCounted.push([a, b, 1])
}
}
// Demo Output
document.write("Pairs: " + JSON.stringify(randomPairs) + "<br><br>")
for (var i = 0; i < randomPairsCounted.length; ++i) {
document.write(randomPairsCounted[i][0] + " vs " + randomPairsCounted[i][1] +
": occured " + randomPairsCounted[i][2] + " time" +
(randomPairsCounted[i][2] == 1 ? "" : "s") + "<br>")
}

Categories

Resources