Regex for decimal numbers - regex

Could anybody provide a regular expression for a number that has to be between 1 and 17 in length, and could optionally contain a mantissa of up to 4 places? The length of 17 includes both the characteristic and the mantissa.
Edit:
The length of 17 excludes the decimal point.
Valid examples:
12345678901234567
1234567890123.4567
123456789012345.67
12.34
Invalid:
12345678901234.5678 (Length of numerals = 18)
Thanks.

^\d{17}$|^\d{13}(?=.{5}$)\d*\.\d*\d$
Regex explained:
^\d{17}$ //A string of 17 digits
| //or
^\d{13} //13 digits followed by
(?=.{5}$) //5 characters, of which
\d*\.\d* //one is a decimal point and others are digits
\d$ //and the last one is a digit

OK, this is the best I could do:
/^\d{1,17}$|(?=^.{1,18}$)^\d+\.\d{1,4}$/
Basically, match 1-17 digits, or strings of length 1-18 which consist of two sets of digits separated by a period. The right set can only contain between 1-4 digits.

Don't do this completely in regex. The problem becomes nearly trivial in most programming languages, and that way will be easier for you to write, verify, test, and maintain. You can still use regex for part of the solution, of course, but you don't have to. Pseudocode:
m = re.match(r"(?P<before>[0-9]+)(?P<after>\.[0-9]{1,4})?$", input_string)
if not m:
return "no match"
before, after = m.group("before", "after")
after = after[1:] if after else "" # remove period or set to empty string
if len(before) + len(after) > 17:
return "incorrect length"
return "valid"

It's not particularly pretty, but with so few possibilities (0,1,2,3,4 length mantissa) I would probably just list them all:
\d{17}|\d{16}\.\d{1}|\d{15}\.\d{2}|\d{14}\.\d{3}|\d{13}\.\d{4}

in your favourite language, you can do a couple of logical checks, eg Python
num="1234567890133.3456"
if "." in num and len(num)==17 :
n=num.split(".")
if len(n[1])>4:
print "cannot have more than 4 decimal places"
elif len(n)==2 and n[0].isdigit() and n[1].isdigit():
print "yes, decimal"
elif len(num)==17 and num.isdigit():
print "%s is number with no decimal and is exactly 17 digits." % num
else:
print "%s not ok, check length is 17" % num

I have created this regex from above great solutions. may it help any one. Please let me know if you find any bug in it.
String decimalRegex =""+
"^(?!0[\d,])\+?" + // ^ Start of Number
"(\d{0,"+size+"}|" + // Numeric value without group symbol | (OR)
"(\d{0,"+rem(size,groupSize)+"},)?"+
"(\d{0,"+groupSize+"},) {0,"+div(size,groupSize)+"}\d{"+groupSize+"})" + // Numeric value with group symbol
"((([a-zA-Z]{0,2}|\"|\')\s?\+?)|\.)"+
"(\d{0,"+scale+"})?" + // Decimal value without group symbol
"(\s?([a-zA-Z]{0,2}|\"|\'))$"; // Ends with
private int rem(int size,int groupSize ){
int rem = (size - groupSize)%groupSize;
return rem;
}
private int div(int size,int groupSize ){
int div = (size - groupSize)/groupSize;
return div;
}

Related

How can i make a number like 123 into a string with ascii in C++?

I want to make a number like 77 into a string but I can't use ascii because it's only from 0-9. Is there some way to make numbers become a string? So this is the result at the end: You input a number and it outputs the number but as a string. Example: input:123; output:"123".
Each digit can use ASCII. The number 123 uses a 1, a 2, and a 3, and the string that represents that value uses the characters '1', '2', and '3'.
The way to do the conversion yourself is to get each digit by itself and add the digit to '0'. Like this:
int value = 123
std::string result;
while (value != 0) {
int digit = value % 10;
char digit_as_character = digit + '0';
result.insert(0, 1, digit_as_character);
value = value / 10;
}
This is pretty much what you'd do if you were doing it by hand:
start with the value get the last digit of the value by dividing by
10 and looking at the remainder
write down a digit for the remainder
divide the value by 10 to remove the last digit, since you don't need
it any more.
Who said anything about ascii? The C++ standard doesn't.
Use the platform-independent std::to_string(77) instead.
Reference: http://en.cppreference.com/w/cpp/string/basic_string/to_string

C++: Doubling digits using recursion

This is for my "Intro to C++" course. I need to
Write a program that uses a recursive function double_all_digit that doubles all digit from an integer. For example, double_all_digits(101) will return 110011
My code below works for only one digit; I have no idea how to proceed:
int double_all_digit(int x)
{
if(x < 10)
return (x*10) + x;
}
You have the base case; now for the recursion.
split the number into the 1's digit (use modulus) and the rest.
recur on the rest; your result is that number with all the digits doubled.
multiply that result by 100; add 11 times the 1's digit.
return this value up one level.
Here's a strategy:
convert a digit to a string. (use std::to_string)
iterate over characters in the string and append 2 characters in a new string for each character in the original. (See std::string::append)
convert the resulting string to an integer.
Since it's homework, you'll have to do the coding bit. :)

what does that mean, C programm for RLE

I am new to C so I do not understand what is happening in this line:
out[counter++] = recurring_count + '0';
What does +'0' mean?
Additionally, can you please help me by writing comments for most of the code? I don't understand it well, so I hope you can help me. Thank you.
#include "stdafx.h"
#include "stdafx.h"
#include<iostream>
void encode(char mass[], char* out, int size)
{
int counter = 0;
int recurring_count = 0;
for (int i = 0; i < size - 1; i++)
{
if (mass[i] != mass[i + 1])
{
recurring_count++;
out[counter++] = mass[i];
out[counter++] = recurring_count + '0';
recurring_count = 0;
}
else
{
recurring_count++;
}
}
}
int main()
{
char data[] = "yyyyyyttttt";
int size = sizeof(data) / sizeof(data[0]);
char * out = new char[size + 1]();
encode(data, out, size);
std::cout << out;
delete[] out;
std::cin.get();
return 0;
}
It adds the character encoding value of '0' to the value in recurring_count. If we assume ASCII encoded characters, that means adding 48.
This is common practice for making a "readable" digit from a integer value in the range 0..9 - in other words, convert a single digit number to an actual digit representation in a character form. And as long as all digits are "in sequence" (only digits between 0 and 9), it works for any encoding, not just ASCII - so a computer using EBCDIC encoding would still have the same effect.
recurring_count + '0' is a simple way of converting the int recurring_count value into an ascii character.
As you can see over on wikipedia the ascii character code of 0 is 48. Adding the value to that takes you to the corresponding character code for that value.
You see, computers may not really know about letters, digits, symbols; like the letter a, or the digit 1, or the symbol ?. All they know is zeroes and ones. True or not. To exist or not.
Here's one bit: 1
Here's another one: 0
These two are only things that a bit can be, existence or absence.
Yet computers can know about, say, 5. How? Well, 5 is 5 only in base 10; in base 4, it would be a 11, and in base 2, it would be 101. You don't have to know about the base 4, but let's examine the base 2 one, to make sure you know about that:
How would you represent 0 if you had only 0s and 1s? 0, right? You probably would also represent the 1 as 1. Then for 2? Well, you'd write 2 if you could, but you can't... So you write 10 instead.
This is exactly analogous to what you do while advancing from 9 to 10 in base 10. You cannot write 10 inside a single digit, so you rather reset the last digit to zero, and increase the next digit by one. Same thing while advancing from 19 to 20, you attempt to increase 9 by one, but you can't, because there is no single digit representation of 10 in base 10, so you rather reset that digit, and increase the next digit.
This is how you represent numbers with just 0s and 1s.
Now that you have numbers, how would you represent letters and symbols and character-digits, like the 4 and 3 inside the silly string L4M3 for example? You could map them; map them so, for example, that the number 1 would from then on represent the character A, and then 2 would represent B.
Of course, it would be a little problematic; because when you do that the number 1 would represent both the number 1 and the character A. This is exactly the reason why if you write...
printf( "%d %c", 65, 65 );
You will have the output "65 A", provided that the environment you're on is using ASCII encoding, because in ASCII 65 has been mapped to represent A when interpreted as a character. A full list can be found over there.
In short
'A' with single quotes around delivers the message that, "Hey, this A over here is to receive whatever the representative integer value of A is", and in most environments it will just be 65. Same for '0', which evaluates to 48 with ASCII encoding.

How to calculate nth n-digit palindrome efficiently?

I think the question is simple enough to understand.For more clarity I'm giving example :
In the list of 2 digit palindromes, the 7th palindrome is 77 (1st being 11, 2nd being 22 and so on).
Obviously a brute force solution exists but it's not efficient.
Can anyone suggest me some better solution to solve the problem ?
First, we can simplify the problem because we only need to look at the first half of the digits (rounding up if there are an odd number of digits). I will call the first set of digits significant digits and the rest non-significant digits.
This is because the non-significant digits must match the significant digits (in reverse). It is not possible to have another palindrome number with the same leading significant digits and different non-significant digits. The significant digits determine the entire palindrome number.
Now, we just need to come up with an algorithm to generate the nth valid significant digits. This would be easier if we allowed for leading zeros, so we'll come up with the algorithm that allows for leading zeros, then tweak the algorithm.
The first few palindromes (significant digits) would be:
1: 0000
2: 0001
3: 0002
...
100: 0099
So we can find the significant digits of the nth number by finding the decimal representation of (n-1).
To tweak the algorithm to work when not allowing leading zeros, we would start with a one as the leading digit:
1: 1000
2: 1001
3: 1002
...
100: 1099
This boils down to finding the decimal representation of (n-1) + 1000 = n + 999 and expanding into a full palindrome:
Example: Find the 113th palindrome of length 9.
Determine number of digits to look at: Round up(9 / 2) = 5 --> only look at first 5 digits.
Find number to add to get rid of leading zeros: 10^(5-1) = 10000
Use formula: (113 - 1) + 10000 = 10112
Expanded into palindrome: 101121101
On a side note, this algorithm could also be generalized to finding the nth palindrome of any ordered set of symbols (or alphabet).
Generalized algorithm:
Given: finding palindrome number n , palindrome has m symbols as digits , there are p symbols (10 symbols in decimal)
Let q = ceiling(m / 2)
Let offset = p ^ (q - 1)
Let number = (n - 1) + offset
Let answer be number expanded as a palindrome
The first few 7-digit palindrome are:
1000001
1001001
1002001
1003001
...
1009001
1010101
1011101
...
I think it's very easy to see from the pattern of what is the nth m-digit palindrome...
When the number of digits is even, just take the nth number with half as many digits starting from 100..0, where the length is half the number of digits. The palindrome is just this number followed by its mirror.
For an odd number of digits, just take the ceiling of half that number, and count from 100...0 the same way. Then the palindrome is this number followed by its mirror with the first digit removed.
The 65th 10 digit number:
65 + 9999 = 10064
1006446001
The 10298th 13 digit number:
10298 + 999999 = 1010297
1010297920101
For two digit palindromes the difference between two consecutive palindromes is 11.
something like:
#include<iostream>
#include<cmath>
#include<cstring>
#include<cstdio>
using namespace std;
void reverseString(char *dest,char *src){
char *iter=src;
while (*iter) iter++;
while (iter!=src){
iter--;
*dest=*iter;
dest++;
}
*dest=0;
}
char *pal(int n,int len){
char *tmp=new char[len/2+2];
char *out=new char[len+1];
sprintf(out,"%i",n+int(pow(10.0,(len+1)/2-1))-1);
reverseString(tmp,out); //copy reversed out into tmp
strcat(out,tmp+len%2);
delete []tmp;
return out;
}
int main(){
cout<<pal(4,7)<<endl;
}

Explanation for a line of code in c++ related to bitshifts

I don't want someone to explain how the following code works (it checks whether an int is pandigital) as I should be doing that myself. I just need help understanding line 8 specifically. I don't know what the | is doing.
private bool isPandigital(long n) {
int digits = 0;
int count = 0;
int tmp;
while (n > 0) {
tmp = digits;
digits = digits | 1 << (int)((n % 10) - 1);
if (tmp == digits) {
return false;
}
count++;
n /= 10;
}
return digits == (1 << count) - 1;
}
I know others have already explained that it's a bitwise OR, but I'd like to give my own interpretation.
digits = digits | X will copy all the 1 bits from X into digits.
digits = digits | 1 << Y will "set" a single bit in digits - it will set the Yth bit.
So, each loop sets a bit in digits.
| is bitwise or. But the code checks whether an int of length n has all digits 1..n. This is different from palindrome check. That line sets's (i-1)'th bit of digits to 1 if the last digit of n is i. [BTW, the code is wrong: if n contains a zero-digit, that line will trigger "undefined behavior": shifting an integer by a negative amount gives an undefined result.]
The code uses an integer digits to represent a set of digits. You can learn more about the technique by searching for bit sets.
It appears to be performing a Bitwise Or.
| is a bitwise OR
A bitwise OR takes two bit patterns of equal length and performs the logical inclusive OR operation on each pair of corresponding bits. The result in each position is 1 if the first bit is 1 OR the second bit is 1 OR both bits are 1; otherwise, the result is 0.
Example:
10010000
01010000
--------
11010000
http://en.wikipedia.org/wiki/Bitwise_operation
| is a bitwise or.
So the line is doing digits = digits | (1 << (int)((n % 10) - 1));