validate numbers by limitation in length and true decimal format - validation

I want to validate numbers by regular expression.
My valid numbers are:
123456789012345.123
or
123.9
or
0.686`
Before decimal point must be 1 to maximum 15 numbers and after it must be maximum to 3 numbers; and negative numbers is optional.
invalid numbers are:
0.0
0.00
0.000
000
097654
05978.7
.657665
5857.
I found this regex but I can,t set numbers length limitation:
^-?(([1-9]\d*)|0)(\.0*[1-9](0*[0-9])*)?$

In place of * use {a,b} where a is the minimum number of preceding and b the maximum. Omit a or b for no minimum / maximum.

I found solution myself
^-?(([1-9])([0-9]{1,14})?|0)(\.[0-9]?[0-9]?[1-9])?$

Related

Number of occurrences of 2 as a digit in numbers from 0 to n , Not getting the O(n) solution?

This the GFG Link
In this link, I am not able to get anything intuition that how we are calculating the number of 2 as a digit in,
My doubt is if we are counting the 6000 digits in the range as explained in the below description then why we are simply dividing the number by 10 and returning it, If anyone can help me, please do post your answer with examples
Case digits < 2
Consider the value x = 61523 and digit at index d = 3 (here indexes are considered from right and rightmost index is 0). We observe that x[d] = 1. There are 2s at the 3rd digit in the ranges 2000 – 2999, 12000 – 12999, 22000 – 22999, 32000 32999, 42000 – 42999, and 52000 – 52999. So there are 6000 2’s total in the 3rd digit. This is the same amount as if we were just counting all the 2s in the 3rd digit between 1 and 60000.
In other words, we can round down to the nearest 10d+1, and then divide by 10, to compute the number of 2s in the d-th digit.
if x[d) < 2: count2sinRangeAtDigit(x, d) =
Compute y = round down to nearest 10d+1
return y/10
Case digit > 2
Now, let’s look at the case where d-th digit (from right) of x is greater than 2 (x[d] > 2). We can apply almost the exact same logic to see that there are the same number of 2s in the 3rd digit in the range 0 – 63525 as there as in the range 0 – 70000. So, rather than rounding down, we round up.
if x[d) > 2: count2sinRangeAtDigit(x, d) =
Compute y = round down to nearest 10d+1
return y / 10
Case digit = 2
The final case may be the trickiest, but it follows from the earlier logic. Consider x = 62523 and d = 3. We know that there are the same ranges of 2s from before (that is, the ranges 2000 – 2999, 12000 – 12999, … , 52000 – 52999). How many appear in the 3rd digit in the final, partial range from 62000 – 62523? Well, that should be pretty easy. It’s just 524 (62000, 62001, … , 62523).
if x[d] = 2: count2sinRangeAtDigit(x, d) =
Compute y = round down to nearest 10d+1
Compute z = right side of x (i.e., x% 10d)
return y/10 + z + 1**// here why we are doing it ,what is the logic behind this approach**
There is not complete clarity in the explantion given above that's why I am asking here Thank you
For me that explanation is strange too. Also note that true complexity is O(log(n)) because it depends on nummber length (digit count).
Consider the next example: we have number 6125.
At the first round we need to calculate how many 2's are met as the rightmost digit in all numbers from 0 to 6125. We round number down to 6120 and up to 6130. Last digit is 5>2, so we have 613 intervals, every interval contains one digit 2 as the last digit - here we count last 2's in numbers like 2,12,22,..1352,..,6122.
At the second round we need to calculate how many 2's are met as the second (from right) digit in all numbers from 0 to 6125. We round number down to 6100 and up to 6200. Also we have right=5. Digit is 2, so we have 61 intervals, every interval contains ten digits 2 at the second place (20..29, 120..129... 6020..6029). We add 61*10. Also we have to add 5+1 2's for values 6120..6125
At the third round we need to calculate how many 2's are met as the third (from right) digit in all numbers from 0 to 6125. We round number down to 6000 and up to 7000. Digit is 1, so we have 6 intervals, every interval contains one hundred of digit 2 at the third place (200.299.. 5200..5299). So add 6*100.
I think it is clear now that we add 1 interval with thousand of 2's (2000.2999) as the leftmost digit (6>2)

Find largest multiple for a number set

An array of digits(0-9) of size N is provided as input. A set of numbers(N1,...,Nm) of size m with the numbers separated by space is also as the input. The program has to print the largest number that can be formed using the digits in the array of size N that is divisible by the numbers N1,..,Nm
Example Input/Output1:
INPUT:
160
2 3 5
OUTPUT
60
Explanation
60 is the largest number that can be formed using the digits 1,6,0 which is divisible by 2,3,5
Example Input/Output2:
Input
91028
17 5 9
Output
9180
Boundary Conditions
1<=m<=5
2<=N<=50
Can somebody explain how to approach this problem.
Partial answer:
Try all permutations of all subsets of your digits, probably starting with the largest candidates.
If your factors contain 5 the last digit must be 0or 5
If your factors contain 3 or 6 or 9 the sum of all candidate digits muts be a multiple of 3
If your factors contain 2 or 4 or 6 or 8 the last digit must be even.
And so on.

An efficient algorithm to compute the number of '1' bit in a long decimal integer that is represented in string?

I came across this interesting question today. (Note that this is not for my homework or interview, etc.)
Given a decimal number that is represented in string, we want to compute the number of '1' bits for the large number in binary format. Here the string can have thousands of characters, and cannot be represented with one int or long long variable.
For example, countBits("10") = 2 as '10' in decimal can be represented as '1010' in binary format. Similarly, we have countBits("12") = 2, countBits("7") = 3
What is an efficiently algorithm for this? One possible solution is to convert the decimal string to another string in the binary format, and count the '1's. Can we do better?
When converting from a decimal representation to and integer, the *n*th digit from the end of the string represents the number of 1010n ( one zero base ten to the power of n ) that is added to total the integer value. If you then want to represent that integer in binary, you have to raise 1010 which is 10102 to that power and multiply that value by the digit's value.
Because one of the factors of the base you are translating from, 5, is relatively prime compared to 2, the powers of 1010 have increasing long representations in base 2 - 12, 10102, 11001002, 11111010002.
Note that these powers have trailing zeros ( 1010 = 2 × 5 and 2 is not relatively prime with the base we are translating into ), so will only effect 1, 3, 5, and 7 bits of the answer instead of all 1, 4, 7, 10 bits. But the number of bits they effect will still vary with O(N) where N is the length of the input, so to calculate the effected bits will take O(N2) operations.
If the base you were translating from did not have factors where were relatively prime to the base you are translating to - say translating base 16 to base 2 or base 9 to base 3 and counting non-zero digits, then there would be a O(N) algorithm as the sum of non-zero digits in the target base would equal the sum for each digit in the input translated individually, but since that is not the case then you are stuck at an O(N2) algorithm where you translate the decimal representation into binary and then count the bits in the binary representation.
You convert it to binary and use Hamming weight algorithm.
How it works? Suppose you have the number 8, which is 00001000.
The algorithm takes chunks of 2 bits, so it'll have 00 00 10 00.
Now it'll sum each two bits (by having a mask 10101010, multiplying and shifting), which will result: 00 00 01 00.
Now it does the same for each 4 bits (by having a mask 00110011..), so it'll have 0000 1000. After adding each side, you'll have 0000 00001.
The last stage is adding the two numbers, 0 + 1, which is 1 and that's the final result.

Does radix sort work with numbers who have different number of digits?

I know that radix sort works by comparing the digits of the numbers. My question is, assume we have different numbers with different number of digits. Does radix sort work here? We can simply assume that, for example, if we are comparing two numbers, one with 3 digits and one with 6 digits, the first 3 digits of the smaller number is 0. But how about the implementation? How can we make the program assume that if there are not enough digits, then those digits are zero?
Thank you.
You need to somehow add or simulate those nonexistent digits or sort the numbers in groups, each of which containing only numbers of the same length.
These 3 numbers
9912
999
123
can be transformed into
9912
0999
0123
and sorted using the regular radix sort or they can be sorted as 2 independent groups:
9912
and
999
123
The latter will give you (assuming ascending order)
123
999
and the former stays the same. Then you combine the sorted groups (from shorter numbers to longer numbers):
123
999
9912
That's all.
Assuming you have the number in an integer variable, then you can extract the digits like this (n = 0, 1, 2, ...):
digit = (number / radix ^ n) % radix

Min number of operations

Given a positive integer N, we are allowed to apply any of the following operations as many times as we want in any order:
First Operation: Add 1 the Given positive integer N; If N is 7, after that operation N becomes 8. If N is 999, after that operation it becomes 1000.
Second operation: choose any occurrence of any digit and replace it by another digit. (475->479, 101 -> 111, 299 -> 199 and so on)
Third operation: add any non-zero digit to the left of the decimal representation of N: 47 -> 247, 9999 -> 49999, 2474 -> 72474 and so on).
Find the minimum number of operations that is needed for changing N to the lucky number.(Lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.)
EXAMPLES:
N=25, answer=2
N=46, answer=1
N=99, answer=2
I found this problem while I was trying various problems on LUCKY NUMBER..
I am stuck at this problem...
Please help..
The "add 1 to the number" and "add any non-zero leading digit to the number" are red herrings.
The minimum number of operations is one per digit in N which is non-lucky. You just change each of the non-4, non-7 digits to either 4 or 7.
Adding a leading digit will never help you because there's no need to make the number longer. Adding 1 seems like it could help, but it will only do two things: either it does not carry (when you add to a digit less than 9), in which case a straight replacement can do the same thing, or it carries (when you add to a 9), in which case it's just created one or more non-lucky zeros you're now going to have to "fix" with digit replacement.
Given the rules, apparently, the answer is the number of digits minus the number of 4 or 7 occurrences. So for example, N=25 you replace each digit with either 4 or 7 taking only one at a time. for 46, you take 6 and replace it with 4 or 7 thus the answer 1.
You can try continuous modulo 10 evaluation to check if the digits are 4 or 7
$x = the number
$y = 0; #number of non 4 or 7
while($x>0){
if($x % 10 != 4 && $x % 10 != 7){
$y++;
}
if($x % 10 == 0){
$y +=4;
}
$x = floor($x/10);
}
Apparently 0 is not replaceable doing some edits
only second case is important. just take a string and count how many digits are not equal to 4 and 7
Just consider the second operation.........and find the number of digits different from 4 and 7....and thats the answer.....isn't it....:)
You can try a greedy solution:
Check all digits in the number and count how many are not 4 or 7
Take the count from the above operation, and see if there's a small count when adding only 1 to the number will get you to Lucky one.
Take the min from both - that's the solution
What's the point in adding leading digits to N ? This will not get you an optimal solution.

Resources