How to calculate all possible results in a 4x3 array? [closed] - javascript

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
assume that the array looks like this.
var res = [2,4,1,6,
1,1,2,8,
5,6,7,1];
all possible four quadrants combinations of this array are 81.
in this example we have only one combination on index : 4,5,2,11 which is four 1.
my question is how to calculate them.
thanks.

I'm not completely clear on what you're trying to achieve. Do you want to find every subset of length exactly 4 which contains all the same value? If so, you can do this in N^2 time with the following naive algorithm:
let quadrants = [];
res.forEach(checkElement => {
let possibility = [];
res.forEach((element, index) => {
if (element === checkElement) {
possibility.push(index);
}
});
if (possibility.length === 4) {
quadrants.push(possibility);
}
});
If you want to account for the possibility of the original array having more than 4 of the same number and include all subset quadrants, you'll need to change the length check to >=4 and add one more step at the end of this: calculate the power set of all listed quadrants with length greater than 4, filter out the ones that aren't length 4, and then concatenate those to the quadrants array. (You'll want to remove each quadrant with length >4 from the quadrants array before calculating its power set, so it won't be in the final result.)
If you do that, you may be able to optimize the last step by only calculating the subsets of length 4 of the longer quadrants; try using this as a guide for that if you need it: https://www.geeksforgeeks.org/print-subsets-given-size-set/

Related

JS Function that checks a dynamic number of index positions and returns array element if it satisfies the condition [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I've been looking for a way to return elements in an array based on a dynamic number. This dynamic number will be the number of required index positions for each array element, if each element can satisfy the required number of index positions (its own position counts towards the requirement) then that element should be returned.
const arr = [1,2,3,4,5,6]
const requiredIndexes = 3
So with the variables listed above I should return [1,2,3,4] because 5 and 6 will not be able to return three index positions.
The Array.prototype.slice might give you some help. Please refer to https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice
const arr = [1,2,3,4,5,6]
const requiredIndexes = 3
console.log(arr.slice(0, arr.length - requiredIndexes + 1))
I am not an expert but I think you would want to iterate up to the position. Are you having the dynamic number be a random number? You may want to also clone your array and use the clone as the worker array. Not sure exactly what you are wanting to do with the array, but you can then slice from the array and the parent array stays unefected.
function pickposition() {
var randIndex = Math.floor(Math.random() * clone.length);
rand = clone[randIndex];
clone.splice(randIndex,1);
}

Split integer to array based on size using javascript [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
It may be silly but i need to write an logic to get integer to split to array based on size.
For example if integer is 13 i need to convert to array as [5,5,3] if my size is 5.
if size is 10 then array should be [10,3].
Can any one help me writing the logic in javascript?.
You may build up an array of size N/n if there's no remainder from division (N%n), or N/n+1 if there is one.
Then you may simply Array.prototype.map() through that array and populate the result with n or use remainder as the last element if one exists:
const num = 13,
size = 5,
splitNumber = (N, n) =>
[...Array(0|N/n+(N%n ? 1 : 0))]
.map((_,i,s) =>
N%n && i == s.length-1 ? N%n : n)
console.log(splitNumber(13,5))
console.log(splitNumber(13,10))
.as-console-wrapper{min-height:100%;}

How to extract number from backwards in javascript in optimised way [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have to extract a string from backwards upto 10 digits.
Use case is when we select a mobile number it is prefixed with country code sometimes and sometimes we get only the mobile number.
Let's say the number is : +91-84040355236545
I have to extract the number from the end say from 5 to last 10 degits so the end result would be 0355236545
I have a solution of using string.substring method
Here's a simple solution using substrings:
var number = "+91-84040355236545";
var lastTenDigitsNumber = number.substr(number.length - 10);
console.log(lastTenDigitsNumber);
A simpler solution is using slice:
var number = "+91-84040355236545";
console.log(number.slice(-10));
Another solution using a function and RegEX:
function extractTenDigits(number) {
var rx = /(\d{10}$)/g;
var arr = rx.exec(number);
return arr[1];
}
var number = "+91-84040355236545";
console.log(extractTenDigits(number));
I did a simple benchmark and the best solution for small quantity of numbers is the one using slice.
If you provide more details I can provide a more tailored suggestion.
You can try the following using substring :
let str = "+91-84040355236545";
str.substr(str.length - 10 ,10);

Javascript - Show all telephone numbers in a range [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
If I have phone number ranges such as 5555555555-5599 5555550000-0003 5555550005-0007, my mission is to have it return all results without using a server. Is it possible for it to return without a server a result that would look like:
5555555555
5555555556
5555555557
/* etc */
My previous post about javascript has helped me up to this point but I wanted to rehaul the whole site.
Javascript dashes in phone number
If you could point me in the right direction, I would really appreciate it. I'm just having a mind block right now if this is even possible.
Given a single phone range in the form of "xxxxxxyyyy-zzzz", split the whole string on the dash and the first part of the string at the 6th index. This yields three strings "xxxxxx", "yyyy", and "zzzz". Using a for loop, you can create an array of phone numbers by concatenating the prefix "xxxxxx" onto the range "yyyy"-"zzzz":
// Get an array from a given range "xxxxxxyyyy-zzzz"
function fromRange(range) {
var phoneNumArray = [];
var prefix = range.substring(0,5);
var suffixRange = range.split("-");
for (var suffix = suffixRange[0].substring(4, -1);suffix < suffixRange[1];suffix++) {
phoneNumArray.push(prefix + suffix);
}
return phoneNumArray;
}
Try it in JSFiddle.

Using a while loop to print random numbers until a certain number is reached [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
How to use a while loop to generate random numbers until a certain number is reached? Help please
This code will generate integer numbers from 1 until 50.
var number = 50;
var x = 0;
while(x !== number){
x = Math.floor((Math.random() * 50) + 1);
console.log(x);
}
Considering there is a random number generator random you will just do as shown below
do
{
x=Math.random();
x=x*range;
if(x is desired number )
break;
else
{
print the number.
}
}while(1)
print -( desired number found).
NOTE: As you are beginner you can try the following when designing these codes..
First find what is the input and what is the output.
Then try to draw a flow chart of the whole thing ( You can try running it yourself as if you are the computer)
Then all the branch and loop are converted to if-else or for-while loop.
Check the code and match input and output.
This is a basic program structure..Instead of asking in forums about common structures try reading some books.
Hint 1: There is a function in javascript called Math.random() return a random number between 0 (inclusive) and 1 (exclusive):
Hint 2: check whether the values are equal. Use === in javascript.
Hint 3: check for if else, break and do while, while loop.
Hint 4: If you are given a random number 0<=x<1 then how do you generate a random number using in range [a,b) using the abobe geerated number? Ans: Use b*x.
hope this helps.

Categories