Javascript - String of a Byte with all combinations possible - javascript

i have a sting with a byte in it ("00001011") and now id like to get a array with all possible combinations of the 1 (acitve) "bits" in it also as a "byte string"
so from
var bString = "00001011"; //outgoing string
to a array with all string in it with all possible combinations of this "byte string" like - "00000001", "00000011", "00000010" and so on
is that possible?
thank you in advance

function combinations( input ){
var number = parseInt( input, 2 );
var combinations = [];
var zeroes = (new Array(input.length)).join(0);
for(var i=1;i<=number;i++){
if((i&number) == i){ combinations.push( i ) }
}
return combinations.map( function(dec){
return (zeroes + dec.toString(2)).substr( -zeroes.length-1 );
});
}
http://jsfiddle.net/jkf7pfxn/3/
console.log( combinations("00001011") );
// ["00000001", "00000010", "00000011", "00001000", "00001001", "00001010", "00001011"]
The idea goes as follows: iterate all numbers from 1 to the input number. If current number AND input number return the current number then both have 1 bits in the same place.
On a smaller number, "0101" (which is 5) it works as follows:
1 & 5 == 1, (0001 & 0101) push 1 to the matches.
2 & 5 == 0, (0010 & 0101) no match.
3 & 5 == 1, (0011 & 0101) no match.
4 & 5 == 4, (0100 & 0101) push 4 to the matches.
5 & 5 == 5, (0101 & 0101) push 5 to the matches.
So the combinations for 0101 are 1 (0001), 2 (0010), 4 (0100) and 5 (0101).
Then there's this little trick to pad numbers with zeroes:
var zeroes = (new Array(input.length)).join(0); // gives a long enough string of zeroes
then
// convert to base 2, add the zeroas at the beginning,
// then return the last n characters using negative value for substring
return (zeroes + dec.toString(2)).substr( -1 * zeroes.length);

Since 11111111 is 255 so just loop all values and convert them to binary
$(document).ready(function() {
for (var i = 0; i < 256; i++) {
$('#core').append('<div>' + dec2bin(i) + '</div>');
}
function dec2bin(dec) {
return ('00000000' + (dec >>> 0).toString(2)).slice(-8);
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id='core'></div>

If you want to enumerate all combinations of binary numbers where 1 can only be in the place of your pattern, you can write a simple recursive function:
var input = "00010111";
var current = [];
function combinations()
{
if (input.length === current.length)
{
var output = current.join('');
if (parseInt(output, 2) !== 0) // exclude all-zeroes case
document.body.innerHTML += output + "<br/>";
return;
}
current.push('0');
combinations();
current.pop();
if (input[current.length - 1] === '1')
{
current.push('1');
combinations();
current.pop();
}
}
combinations();
This algorithm works well for input of any length.
Although it is a recursion, it has a linear time complexity.

Related

Codewars division Kata using Javascript is producing results that are not divisble by 6

I am trying to solve this Kata from Codewars: https://www.codewars.com/kata/simple-fun-number-258-is-divisible-by-6/train/javascript
The idea is that a number (expressed as a string) with one digit replaced with *, such as "1047*66", will be inserted into a function. You must return an array in which the values are the original number with the * replaced with any digit that will produce a number divisive by 6. So given "1*0", the correct resulting array should be [120, 150, 180].
I have some code that is producing some correct results but erroring for others, and I can't figure out why. Here's the code:
function isDivisibleBy6(s) {
var results = [];
for(i=0;i<10;i++) {
var string = i.toString(); // Convert i to string, ready to be inserted into s
var array = Array.from(s); // Make an array from s
var index = array.indexOf("*"); // Find where * is in the array of s
array[index] = string; // Replace * with the string of i
var number = array.join(""); // Join all indexes of the s array back together. Now we should have
// a single number expressed as a string, with * replaced with i
parseInt(number, 10); // Convert the string to an integer
if((number % 6) == 0) {
results.push(number);
} // If the integer is divisible by 6, add the integer into the results array
}
return(results);
};
This code works with the above example and generally with all smaller numbers. But it is producing errors for larger numbers. For example, when s is "29070521868839*57", the output should be []. However, I am getting ['29070521868839257', '29070521868839557', '29070521868839857']. I can't figure out where this would be going wrong. Is anyone able to help?
The problem is that these numbers are larger than the Number.MAX_SAFE_INTEGER - the point when JavaScript numbers break down in terms of reliability:
var num = 29070521868839257;
console.log(num > Number.MAX_SAFE_INTEGER);
console.log(num % 6);
console.log(num)
The last log shows that the num actually has a different value than what we gave it. This is because 29070521868839257 simply cannot be represented by a JavaScript number, hence you get the closest possible value that can be represented and that's 29070521868839256.
So, after some point in numbers, all mathematical operations become unreliable as the very numbers are imprecise.
What you can do instead is ignore treating this whole as a number - treat it as a string and only apply the principles of divisibility. This makes the task vastly easier.
For a number to be divisible by 6 it has to cover two criteria:
it has to be divisible by 2.
to verify this, you can just get the very smallest digit and check if it's divisible by 2. For example in 29070521868839257 if we take 7, and check 7 % 2, we get 1 which means that it's odd. We don't need to consider the whole number.
it has to be divisible by 3.
to verify this, you can sum each of the digits and see if that sum is divisible by 3. If we sum all the digits in 29070521868839257 we get 2 + 9 + 0 + 7 + 0 + 5 + 2 + 1 + 8 + 6 + 8 + 8 + 3 + 9 + 2 + 5 + 7 = 82 which is not divisible by 3. If in doubt, we can sum the digits again, since the rule can be applied to any number with more than two digits: 8 + 2 = 10 and 1 + 0 = 1. That is still not divisible by 3.
So, if we apply these we can get something like:
function isDivisibleBy6(s) {
return isDivisibleBy2(s) && isDivisibleBy3(s);
};
function isDivisibleBy2(s) {
var lastDigit = Number(s.slice(-1));
return (lastDigit % 2) === 0;
}
function isDivisibleBy3(s) {
var digits = s.split("")
.map(Number);
var sum = digits.reduce(function(a, b) {
return a + b
});
return (sum % 3) === 0;
}
console.log(isDivisibleBy6("29070521868839257"));
console.log(isDivisibleBy6("29070521868839256"));
These can even be recursively defined true to the nature of these rules:
function isDivisibleBy6(s) {
return isDivisibleBy2(s) && isDivisibleBy3(s);
};
function isDivisibleBy2(s) {
if (s.length === 0) {
return false;
}
if (s.length > 1) {
return isDivisibleBy2(s.slice(-1));
}
var lastDigit = Number(s);
return (lastDigit % 2) === 0;
}
function isDivisibleBy3(s) {
if (s.length === 0) {
return false;
}
if (s.length > 1) {
var digits = s.split("")
.map(Number);
var sum = digits.reduce(function(a, b) {
return a + b
});
return isDivisibleBy3(String(sum));
}
var num = Number(s);
return (num % 3) === 0;
}
console.log(isDivisibleBy6("29070521868839257"));
console.log(isDivisibleBy6("29070521868839256"));
This is purely to demonstrate the rules of division and how they can be applied to strings. You have to create numbers that will be divisible by 6 and to do that, you have to replace an asterisk. The easiest way to do it is like you did - generate all possibilities (e.g., 1*0 will be 100, 110, 120, 130, 140, 150, 160, 170, 180, 190) and then filter out whatever is not divisible by 6:
function isDivisibleBy6(s) {
var allDigits = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
var allPossibleNumbers = allDigits.map(function(digit) {
return s.replace("*", digit);
});
var numbersDibisibleBySix = allPossibleNumbers.filter(function(s) {
return isDivisibleBy2(s) && isDivisibleBy3(s);
})
return numbersDibisibleBySix;
};
function isDivisibleBy2(s) {
var lastDigit = Number(s.slice(-1));
return (lastDigit % 2) === 0;
}
function isDivisibleBy3(s) {
var digits = s.split("")
.map(Number);
var sum = digits.reduce(function(a, b) {
return a + b
});
return (sum % 3) === 0;
}
console.log(isDivisibleBy6("29070521868839*57"));
console.log(isDivisibleBy6("29070521868839*56"));
As a last note, this can be written more concisely by removing intermediate values and using arrow functions:
function isDivisibleBy6(s) {
return [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
.map(digit => s.replace("*", digit))
.filter(s => isDivisibleBy2(s) && isDivisibleBy3(s));
};
const isDivisibleBy2 = s => Number(s.slice(-1)) % 2 === 0;
const isDivisibleBy3 = s => s.split("")
.map(Number)
.reduce((a, b) => a + b) % 3 === 0
console.log(isDivisibleBy6("29070521868839*57"));
console.log(isDivisibleBy6("29070521868839*56"));
Sum of all digits is divisible by three and the last digit is divisible by two.
An approach:
Get the index of the star.
Get left and right string beside of the star.
Return early if the last digit is not divisible by two.
Take the sum of all digits.
Finally create an array with missing digits:
Start loop from either zero (sum has no rest with three) or take the delta of three and the rest (because you want a number which is divisible by three).
Go while value is smaller then ten.
Increase the value either by 3 or by 6, if the index of the star is the last character.
Take left, value and right part for pushing to the result set.
Return result.
function get6(s) {
var index = s.indexOf('*'),
left = s.slice(0, index),
right = s.slice(index + 1),
result = [],
sum = 0,
i, step;
if (s[s.length - 1] % 2) return [];
for (i = 0; i < s.length; i++) if (i !== index) sum += +s[i];
i = sum % 3 && 3 - sum % 3;
step = s.length - 1 === index ? 6 : 3;
for (; i < 10; i += step) result.push(left + i + right);
return result;
}
console.log(get6("*")); // ["0", "6"]
console.log(get6("10*")); // ["102", "108"]
console.log(get6("1*0")); // ["120", "150", "180"]
console.log(get6("*1")); // []
console.log(get6("1234567890123456789012345678*0")); // ["123456789012345678901234567800","123456789012345678901234567830","123456789012345678901234567860","123456789012345678901234567890"]
.as-console-wrapper { max-height: 100% !important; top: 0; }
The problem is with:
parseInt(number, 10);
You can check and see that when number is large enough, this result converted back to string is not equal to the original value of number, due to the limit on floating point precision.
This challenge can be solved without having to convert the given string to number. Instead use a property of numbers that are multiples of 6. They are multiples of 3 and even. Multiples of 3 have the property that the sum of the digits (in decimal representation) are also multiples of 3.
So start by checking that the last digit is not 1, 3, 5, 7, or 9, because in that case there is no solution.
Otherwise, sum up the digits (ignore the asterisk). Determine which value you still need to add to that sum to get to a multiple of 3. This will be 0, 1 or 2. If the asterisk is not at the far right, produce solutions with this digit, and 3, 6, 9 added to it (until you get double digits).
If the asterisk is at the far right, you can do the same, but you must make sure that you exclude odd digits in that position.
If you are desperate, here is a solution. But I hope you can make it work yourself.
function isDivisibleBy6(s) {
// If last digit is odd, it can never be divisable by 6
if ("13579".includes(s[s.length-1])) return [];
let [left, right] = s.split("*");
// Calculate the sum of the digits (ignore the asterisk)
let sum = 0;
for (let ch of s) sum += +ch || 0;
// What value remains to be added to make the digit-sum a multiple of 3?
sum = (3 - sum%3) % 3;
// When asterisk in last position, then solution digit are 6 apart, otherwise 3
let mod = right.length ? 3 : 6;
if (mod === 6 && sum % 2) sum += 3; // Don't allow odd digit at last position
// Build the solutions, by injecting the found digit values
let result = [];
for (; sum < 10; sum += mod) result.push(left + sum + right);
return result;
}
// Demo
console.log(isDivisibleBy6("1234567890123456789012345678*0"));
BigInt
There is also another way to get around the floating point precision problem: use BigInt instead of floating point. However, BigInt is not supported on CodeWars, at least not in that specific Kata, where the available version of Node goes up to 8.1.3, while BigInt was introduced only in Node 10.
function isDivisibleBy6(s) {
let [left, right] = s.split("*");
let result = [];
for (let i = 0; i < 10; i++) {
let k = BigInt(left + i + right);
if (k % 6n === 0n) result.push(k.toString());
}
return result;
}
// Demo
console.log(isDivisibleBy6("1234567890123456789012345678*0"));
This would anyway feel like "cheating" (if it were accepted), as it's clearly not the purpose of the Kata.
As mentioned, the values you are using are above the maximum integer value and therefore unsafe, please see the docmentation about this over here Number.MAX_SAFE_INTEGER. You can use BigInt(string) to use larger values.
Thanks for all the responses. I have now created successful code!
function isDivisibleBy6(s) {
var results = [];
for(i=0;i<10;i++) {
var string = i.toString();
var array = Array.from(s);
var index = array.indexOf("*");
array[index] = string;
var div2 = 0;
var div3 = 0;
if(parseInt((array[array.length-1]),10) % 2 == 0) {
div2 = 1;
}
var numarray = array.map((x) => parseInt(x));
if(numarray.reduce(function myFunc(acc, value) {return acc+value}) % 3 == 0) {
div3 = 1;
}
if(div2 == 1 && div3 == 1) {
results.push(array.join(""));
}
}
return(results);
};
I know this could be factored down quite a bit by merging the if expressions together, but I like to see things split out so that when I look back over previous solutions my thought process is clearer.
Thanks again for all the help!

jQuery to validate serial number in form

I am using a forms plugin in WordPress that allows for JavaScript queries.
I was told to use something like this: jQuery("#{{elemnt_id}}").val()
In the forms plugin there is a section to input code before submit which is as follows:
// Occurs just before submitting the form
function before_submit() {
// IMPORTANT! If you want to interrupt (stop) the submitting of the form,
this function should return true. You don't need to return any value if you
don't want to stop the submission.
}
I need to validate a serial number based on some minor mathematical equations.
The serial number is in the format of: abcd-efghij (in the form of numbers but I am using letters in the format here so that I can explain easier what happens
So the serial number is valid if:
a is always either the numbers 1 or 2
bcd is generated further along in step 11
e is then multiplied by 1
f is then multiplied by 2
g is then multiplied by 3
h is then multiplied by 4
i is then multiplied by 5
j is then multiplied by 6
efghij is then all added up together and multiplied by 3
a is multiplied by 11 and added to the result of previous step (total of efghij muliplied by 3)
3 is then added to the result of that and the result then equals what bcd should be
So a valid number would be something like 1287-123456 because
From second set of digits:
5th digit multiplied by 1:- 1x1=1
6th digit multiplied by 2:- 2x2=4
7th digit multiplied by 3:- 3x3=9
8th digit multiplied by 4:- 4x4=16
9th digit multiplied by 5:- 5x5=25
10th digit multiplied by 6:- 6x6=36
results added all up = 91 (1+4+9+16+25+36)
then multiply by 3:- 91x3=273
Then from first set of digits:
1st digit multiplied by 11:- 1x11=11
Then add result of second set to result of first set:
273 + 11 = 284
and finally add 3 to that:
284 + 3 = 287
giving you 2nd 3rd and 4th digits
I have tried this but its probably totally wrong..
Dim strID
Dim ColCSum3
Dim ChkVal
Dim InitVal
strID = "element_id"
If strID = "" Then
''''' return false
'''' Return "Invalid"
End If
If Mid(strID, 5, 1) <> "-" Or Len(strID) <> 11 Then
'''' return false
'''' Return "Invalid"
End If
InitVal = CLng(Left(strID, 1))
ChkVal = CLng(Mid(strID, 2, 3))
ColCSum3 = (1 * CLng(Mid(strID, 6, 1)) + 2 * CLng(Mid(strID, 7, 1)) + 3 * CLng(Mid(strID, 8, 1)) + 4 * CLng(Mid(strID, 9, 1)) + 5 * CLng(Mid(strID, 10, 1)) + 6 * CLng(Mid(strID, 11, 1))) * 3
If 11 * InitVal + ColCSum3 + 3 = ChkVal Then
Return "Validated"
Else
Return "Invalid"
End If
Any help please for the correct code to use in the form plugin section?
Im not familiar with WordPress, however: say you have an input like
<input type="text" id="serial"/>
then you can indeed retrieve the value using jquery:
var inputVal = $('#serial').val();
Then you would have to do your logic on the val. However, it looks like the code you posted is visual basic code? Are you looking for the described implementation in javascript?
I would start by
if(inputVal.indexOf('-')!=4)return false;
var seqs = inputVal.split('-');
var seq1= parseInt(seqs[0]);
var seq2= parseInt(seqs[1]);
if(isNaN(seq1) || isNaN(seq2))return false;
etc... The rest should be easy to find here and there
You can add a jQuery listner before the form is submitted (see this answer or the jQuery.submit docs), and then split the characters from the text field and do your magic. Here is a fiddle: https://jsfiddle.net/strauman/qef7rsxc/.
So your before_submit function becomes
function before_submit(){
// If you write return false; in here, then
// the form will not get sent.
// if you write return true; then the form will
// get sent:
// Get the value of the textbox
inputVal = $("#serial").val();
// From #user1515791 answer
// Split it at the dash (-)
console.log(inputVal.indexOf('-'));
if (inputVal.indexOf('-') != 4) {
$("#errmsg").html("Wrong format. Needs four numbers before dash");
return false;
}
var seqs = inputVal.split('-');
var seq1 = seqs[0];
var seq2 = seqs[1];
// seq1 now contains abcd
// and sec2 contains efghij
if (isNaN(parseInt(seq1)) ||isNaN(parseInt(seq2))) {
$("#errmsg").html("Got non-numbers in the sequence");
return false;
}
// You can extract the numbers to variables
// like in your question like this
a = seq1[0];
b = seq1[1];
c = seq1[2];
d = seq1[3];
e = seq2[0];
f = seq2[1];
//...
j = seq2[5];
if (a != 1 && a != 2) {
$("#errmsg").html("the first number is not 1 or 2");
return false;
}
f = f * 2;
// ...
return false;
})

Check if a string starts with a number in range

I have a string that can be of random length and I need to check if it satisfies the following conditions:
starts with 4 digits
the first 4 digits form a number in range 2221-2720 (credit card number)
Right now I have the following code:
var isCardNumber = function (myString)
{
var num = parseInt(myString.substr(0, 4));
if(num) {
return num >= 2221 && num <= 2720;
};
return false;
};
Can this be done simpler/shorter? Probably with a regexp?
var isCardNumber = function ( cardNumber ) {
var n = +cardNumber.substr( 0, 4 )
return n && n > 2220 && n < 2721
}
Here is a regex for your general culture. As other said, I would not recommend using it. You could do performance tests but I'd expect it to be slower than the integer comparison test.
^2(2[2-9]\d|[3-6]\d\d|7([0-1]\d|2[0-1]))
To construct this kind of regex you need to break your range into easily reprensentable strings ; 2221-2720 is :
2220 to 2299 (22[2-9]\d)
2300 to 2699 (2[3-6]\d\d)
2700 to 2719 (27[0-1]\d)
2720 to 2721 (272[0-1])
Then you can factorise it as I did or just use it as is, with each fragment separated by a |.

Algorithm for splitting words randomly into defined length groups

I am writing a program (in JavaScript) which needs to randomly split a string (a single word) into groups of letters, with each groups length (character count) being either 2,3 or 4 characters long. For example, australia could return:
aus
tral
ia
or
au
str
alia
Currently I am doing it "manually", with if statements for each string length, eg:
if (word.length == 4){ //split
sections.push(word.substr(0,2));
sections.push(word.substr(2,4));
}
if (word.length == 5){ //either 2/3 or 3/2
if (randomBetween(1,2) == 1){
sections.push(word.substr(0,2));
sections.push(word.substr(2,5));
} else {
sections.push(word.substr(0,3));
sections.push(word.substr(3,5));
}
}
etc...
// randomBetween(x,y) randomly returns one of the arguments
Does anyone have a more algorithmic solution?
Choose a random length from 2 to 4 iteratively to form a list of groups. Handle the edge cases when the remaining string is too small to have all of these options available.
Note that not every possible combination will be chosen with uniform probability. I don't think there's a trivial way to do so efficiently.
I'll leave it to you to choose what happens if a word is passed in with a length less than 2.
function randomlySplit(word) {
var groups = [],
tail = word;
while (tail.length) {
var availableLengths = [2, 3, 4];
if (tail.length <= 3) availableLengths = [tail.length];
if (tail.length === 4) availableLengths = [2];
if (tail.length === 5) availableLengths = [2, 3];
var length = availableLengths[(Math.random() * availableLengths.length) | 0];
groups.push(tail.slice(0, length));
tail = tail.slice(length);
}
return groups;
}
alert(randomlySplit("australia"));
You can see this in action on jsFiddle.
I made a commented function for you, I hope it'll help.
function randomlySplit(word) {
var parts = [];
// Loop as long as word exists
while(word.length) {
// Get an integer which is 2,3 or 4
var partLength = Math.floor(Math.random() * 3 + 2);
// See if only 1 char would be left
if(word.length - partLength === 1) {
// Either add or subtract 1 to partLength
if(partLength < 4) partLength++;
else partLength--;
}
// No issue that partLength > word.length
parts.push(word.substring(0,partLength));
word = word.substring(partLength);
}
return parts;
}
alert(randomlySplit("australia"));

get the number of n digit in a 2+ digit number

For example, getting "5" in "256". The closest I've gotten is Math.floor(256/10)), but that'll still return the numbers in front. Is there any simple way to get what I want or would I have to make a big function for it? Also, for clarity: "n digit" would be defined. Example, getDigit(2,256) would return 5 (second digit)
Math.floor((256 / 10) % 10)
or more generally:
Math.floor(N / (Math.pow(10, n)) % 10)
where N is the number to be extracted, and n is the position of the digit. Note that this counts from 0 starting from the right (i.e., the least significant digit = 0), and doesn't account for invalid values of n.
how about
(12345 + "")[3]
or
(12345 + "").charAt(3)
to count from the other end
[length of string - digit you want] so if you want the 2 it's:
5 - 4 = 1
(12345 + "")[1] = "2"
function getNumber (var num, var pos){
var sNum = num + "";
if(pos > sNum.length || pos <= 0){return "";}
return sNum[sNum.length - pos];
}
First, you need to cast the number to a string, then you can access the character as normal:
var num = 256;
var char = num.toString()[1]; // get the 2nd (0-based index) character from the stringified version of num
Edit: Note also that, if you want to access it without setting the number as a variable first, you need a double dot .. to access the function:
var char = 256..toString()[1];
The first dot tells the interpreter "this is a number"; the second accesses the function.
Convert to string and substring(2,2)?
This should do it:
function getDigit ( position, number ) {
number = number + ""; // convert number to string
return number.substr ( position + 1, 1 ); // I'm adding 1 to position, since 0 is the position of the first character and so on
}
Try this, last line is key:
var number = 12345;
var n = 2;
var nDigit = parseInt((number + '').substr(1,1));
If you want to try to do everything mathematically:
var number = 256;
var digitNum = 2;
var digit = ((int)(number/(Math.pow(10,digitNum-1))%10;
This code counts the digit from the right starting with 1, not 0. If you wish to change it to start at 0, delete the -1 portion in the call.
If you wish to count from the left, it gets more complicated and similar to other solutions:
var number = 256;
var digitNum = 2;
var digit = ((int)(number/(Math.pow(10,number.tostring().length-digitNum))%10;
edit:
Also, this assumes you want base 10 for your number system, but both of those will work with other bases. All you need to do is change instances of 10 in the final line of code to the number representing the base for the number system you'd like to use. (ie. hexadecimal =16, binary = 2)
// You do not say if you allow decimal fractions or negative numbers-
// the strings of those need adjusting.
Number.prototype.nthDigit= function(n){
var s= String(this).replace(/\D+/g,'');
if(s.length<=n) return null;
return Number(s.charAt(n))
}
use variable "count" to control loop
var count = 1; //starting 1
for(i=0; i<100; i++){
console.log(count);
if(i%10 == 0) count++;
}
output will fill
1
2
3
4
5
6
7
8
9

Categories

Resources