I want to build a regex expression that allows me to parse through text files with thousands of lines, and each line contains one number with a variable size of digits.
Each number only can contain either the digits 1 or 0 (zero).
The requirement is there MUST be at least 3 1's in the number, and at least one zero. Therefore, the minimum required size of each number is 4 and it has unlimited maximum.
For example, it has to match:
000000111 - has at least 1 zero and 3 ones
1110 - same thing
11111000 - same thing
111 - FAIL, because it's under 4 digits long
0000000011 - FAIL, needs at least 3 ones
Can anyone help me please? My problem is that I can't determine how to find 'at least 3 ones and one zero anywhere in the number', key word being anywhere.
You could match such numbers with:
(?=1*0)(?:0*1){3}[10]*
(?=1*0) make sure there is at least 1 0 with a lookahead (?=...)
(?:0*1){3} match the number with 3 1s
[10]* match the rest or the number
Unless this is strictly a regex exercise/practice, this would be more easily done by hand. (and since the regex would be complicated (im guessing), it would be way more efficient)
int ones = 0;
int zeroes = 0;
for(int i=0;i<str.length();++i)
{
if(str[i] = '0')
++zeroes;
else if(str[i] = '1')
++ones;
}
if(ones+zeroes >= 4 && ones >=3 && zeroes >= 1)
return true;
Related
I wanted to find out the number of 0's at end of integer.
Eg for 2020 it should count 1
for 2000 it should count 3
for 3010000 it should count 4
I have no idea to do it without counting all the zeros and not just the ending ones!
someone please help :)
Go to Power Query Editor and add a Custom Colum with this below code-
if Number.Mod([number],100000) = 0 then 5
else if Number.Mod([number],10000) = 0 then 4
else if Number.Mod([number],1000) = 0 then 3
else if Number.Mod([number],100) = 0 then 2
else if Number.Mod([number],10) = 0 then 1
else 0
Considered highst possibility of trailing 0 is 5. You can add more if/else case following the above logic if you predict more numbers of consecutive 0 at the end.
Here is sample output using above logic-
Take advantage of the fact, that text "00123" converted to number will be 2 characters shorter.
= let
TxtRev = Text.Reverse(Number.ToText([num]))&"1", /*convert to text and reverse, add 1 to handle num being 0*/
TxtNoZeroes = Number.ToText(Number.FromText(TxtRev)) /*convert to number to remove starting zeroes and then back to text*/
in
Text.Length(TxtRev)-Text.Length(TxtNoZeroes) /*compare length of original value with length without zeroes*/
This will work for any number of trailing zeroes (up to Int64 capacity of course, minus space for &"1"). Assuming that the column is of number type; if it's a text then just remove Number.ToText in TxtRev. If you have negative numbers or decimals, replace characters not being a digit after converting to text. For initial number being 0 it shows 1, but if it should show 0 just remove &"1".
You can do it as general string manipulation:
= Text.Length(Text.From([number])) - Text.Length(Text.TrimEnd(Text.From(number]), "0"))
We convert the column to string, strip of the zeroes, count take that away from the total length, giving you the amount of stripped zeroes.
Edit: I messed up my first answer, this one should in fact be correct
The gist of it is that every letter from a-z needs to be encrypted into a number.
For example a will turn to "1", b into "2" all the way to z="26". Then I have to guess the number of possible outcomes for every encryption. For example 25114 can be 6 different thing. It can be BEAN,BEAAD,YAAD,YAN,YKD,BEKD.
My question is "How do I do this" ?
I've tried using if but it keeps printing "1" as an output every time.
#include <iostream>
using namespace std;
int main()
{
int a1,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z;
cout<<"vnesi kod"<<endl;
cin>>a1;
if (a)
{
cout<<"1"<<endl;
}
else if (b)
{
cout<<"2"endl;
}
return 0;
}
Since this is a homework problem, I just give you some pseudo-code on how to solve this. You will still have to implement it yourself.
Let us assume you get a number as input existing out of n digits: a1a2a3 ... an
Since the alphabet contains 26 letters, we want to split this number into groups of 1 or 2 digits and if we have a group of two digits, you have to check if the number is smaller than 27. The quickest way to do this is to make use of a recursive function. It is not the cleanest, but the quickest. Let us assume the recursive function is called decode.
It is very easy to understand why a recursive function is needed. If we want to decode the number 25114. There are two paths we need to take, groups of 1 and groups of 2:
group of 1: translate the last digit 4 into "D", and decode the remaining number 2511
group of 2: check if the last two digits are smaller than 27, translate the last two digits 14 into N and decode the remaining number 251
In pseudo-code this looks like this:
# function decode
# input: the number n to decode
# a postfix string p representing the decoded part
function decode(n, p) {
# end condition: If the number is ZERO, I have decoded the full number
# only print and return
if (n == 0) { print p; return }
# group of 1: use integer division to extract the
# last digit as n%10 and
# remainder to decode is n/10
decode(n/10, concat(translate(n%10),p) )
# group of 2: use integer division to extract the
# last two digits as n%100 and
# remainder to decode is n/100
# This does not need to run if n < 10 or if n%100 > 26
if (n > 9 && n%100 <= 26) { decode(n/100, concat(translate(n%100),p) ) }
}
The function concat concatenates two strings: concat("AA","BB") returns "AABB"
The function translate(n) converts a number n into its corresponding alphabetic character. This can be written as sprintf("%c",64+n)
As is mentioned in the comments, this is not a very efficient method. This is because we do the same work over and over. If the input reads 25114, we will do the following steps in order:
step 1: translate(4), decode _2511_
step 1.1: translate(1), decode _251_
step 1.1.1: ...
step 1.2: translate(11), decode _25_
step 1.2.1: ...
step 2: translate(14), decode _251_
as you see, we have to decode 251 twice (in step 1.1 and step 2). This is very inefficient as we do everything more than ones.
To improve this, you can keep track of what you have done so far in a lookup table
Check out the ASCII table http://www.asciitable.com/ . I have had something like this similar for my homework as well. since 'a' = 97 and 'z' = 122, you could subtract the desired character from 96 to get the preferred value from the casted character.
For example:
int letterNum {(int)'a' - 97 + 1}; // 1
int letterNum {(int)'z' - 97 + 1}; // 26
I need to check whether a word has exactly 6 letters and 1 digit or not.
I've tried this answer and modified my regex to look like this:
^[a-zA-Z]{6}\d{1}$ (meaning any letter between a-z or A-Z 6 times and 1 digit)
but I get no matches for any string that I could imagine (examples: sixsix1, 1sixsix, six1six)
I am using regex101 to compile the regex
What did I do wrong?
You may try this:
^(?=^[^\d]*\d{1}[^\d]*$)[a-zA-Z0-9]{7}$
demo
Explanation:
(?=^[^\d]*\d{1}[^\d]*$) ensures to look for just a single number
in the entire string.
[a-zA-Z0-9]{7} ensures there is 7 character within a-zA-Z0-9. As
previously it was confirmed that there is no more than 1 number thus
it will only match when it has 6 chars and one number.
Borrowing from this answer since I've never written Java:
String s = "six1six";
if (s.length() != 7) return false;
int digits = 0;
int letters = 0;
for (int i = 0; i < 7; i++) {
if (Character.isDigit(s.charAt(i))) {
digits++;
}
else if (Character.isLetter(s.charAt(i))) {
letters++;
}
}
return digits == 1 && letters == 6;
Is there really no LINQ equivalent in Java? this is hideous.
I'm currently writing a validator where I need to check the formats of floats. My code reads in a format of (x,y) where x is the total possible digits in the float and y is the maximum digits out of x that can be after the decimal point. Apologies if this question has already been answered before, but I wasn't able to find anything similar.
For example, given a format of (5,3):
Valid values
55555
555.33
55.333
5555.3
.333
Invalid values
55555.5
555555
5.5555
.55555
This is my first time working with regex so if you guys have any tutorials that you recommend, please send it my way!
You can use a lookahead to ensure both conditions, like
^(?=(?:\.?\d){1,5}$)\d*(?:\.\d{1,3})?$
^ match from the start of the string
(?=(?:\.?\d){1,5}$) check for the presence of 1 up to 5 digits to the end of the string - not caring to much about the correct number of dots
\d* match any number of digits
(?:\.\d{1,3})? match up to 3 decimal places
$ ensure end of the string
See https://regex101.com/r/lrP56w/1
Assuming JS you can try
function validate(value, total, dec) {
let totalRegex = new RegExp(`\\d{0,${total}}$`);
let decimalRegex = new RegExp(`\\.\\d{0,${dec}}$`);
return totalRegex.test(value.replace(".","")) && (!(/\./.test(value)) || decimalRegex.test(value));
}
console.log(validate("555.55", 5, 2));
console.log(validate("55.555", 5, 2));
console.log(validate("5.5", 5, 2));
console.log(validate("55555", 5, 2));
console.log(validate("5.5555", 5, 2));
Drazil is playing a math game with Varda.
Let's define for positive integer x as a product of factorials of its
digits. For example, f(135) = 1! * 3! * 5! = 720.
First, they choose a decimal number a consisting of n digits that
contains at least one digit larger than 1. This number may possibly
start with leading zeroes. Then they should find maximum positive
number x satisfying following two conditions:
x doesn't contain neither digit 0 nor digit 1.
= f(x) = f(a)
Help friends find such number.
Input The first line contains an integer n (1 ≤ n ≤ 15) — the number
of digits in a.
The second line contains n digits of a. There is at least one digit in
a that is larger than 1. Number a may possibly contain leading zeroes.
Output Output a maximum possible integer satisfying the conditions
above. There should be no zeroes and ones in this number decimal
representation.
Examples
input
4
1234
output
33222
input
3
555
output
555
Here is the solution,
#include <bits/stdc++.h>
#include <algorithm>
using namespace std;
int main()
{
map<char, string> mp;
mp['0'] = mp['1'] = "";
mp['2'] = "2";
mp['3'] = "3";
mp['4'] = "223";
mp['5'] = "5";
mp['6'] = "35";
mp['7'] = "7";
mp['8'] = "2227";
mp['9'] = "2337";
int n;
string str;
cin>>n>>str;
string res;
for(int i = 0; i < str.size(); ++i)
res += mp[str[i]];
sort(res.rbegin(), res.rend());
cout<<res;
return 0;
}
I'd like if someone explains the reason why were the digits transformed into other form of digits rather than just with some way to compute the number with..sadly brute force would give a TLE(Time limit exceeded) in this question cause of the 15 digit thing so that's a big number to brute force to,so I kindly hope that someone can explain the "proof" below, cause idk what theory says that these numbers can be transformed to those numbers for example 4 to 223 and stuff.
Thanks in advance.
Picture: What the proof says
The theory behind these transformations is the following (Ill use 4 as an example):
4! = 3! * 2! * 2!
A longer sequence of digits will always produce a larger number than a shorter sequence (at least for positive integers). Thus this code produces a longer sequence where possible. With the above example we get:
4! = 3! * 4
We can't reduce the 3! any further, since 3 is a prime. 4 on the other hand is simply 2²:
4 = 2² = 2! * 2!
Thus we have found the optimal replacement for 4 in the number-sequence as "322". This can be done for all numbers, but prime-numbers aren't factorisable and will thus always be the best replacement available for them self.
And thanks to the fact that we're using prime factorization we also know that we have the only (and longest possible) string of digits that can replace a certain digit.