I use the programming language: J.
I want to put all of the digit of a number in a list.
From:
12345
to:
1 2 3 4 5
What can I do?
The way I'd write this is
10&#.^:_1
which we can see in use with this sentence:
(10&#.^:_1) 123456789
1 2 3 4 5 6 7 8 9
That program relies on the reshaping built in to Base. It uses the (built-in) obverse of Base as a synonym for Antibase.
I found the answer:
intToList =: (".#;"0#":)
Another approach:
intToList =: 3 : '((>. 10 ^. y)#10) #: y'
This doesn't convert to string and back, which can be potentially costly, but counts the digits with a base-10 log, then uses anti-base (#:) to get each digit.
EDIT:
Better, safer version based on Dan Bron's comment:
intToList =: 3 : '10 #.^:_1 y'
Related
Regex to categorize phone numbers. Numbers with 2 of the same digit in the last 4 not adjacent to each other are easier to remember and therefore more valuable. So given 10 digit number how can I match if 2 of the last 4 digits are the same non consecutively? Ex. 2155553747, 2158558284, 7034651215. Thanks in advance for the help.
If you want to use a regular expression for that, and you are okay with the condition, that at least 2 digits of the last 4 digits are the same, you could use the following regular expression:
^\d{6}(?:(\d)\d\d\1|(\d)\d\2\d|(\d)\3\d\d|\d(\d)\d\4|\d(\d)\5\d|\d\d(\d)\6)$
Here is a live example: https://regex101.com/r/t6n1uP/1
Masochistic approach:
/^\d{6}(\d?0[^0]{1,2}0|\d?1[^1]{1,2}1|\d?2[^2]{1,2}2|\d?3[^3]{1,2}3|\d?4[^4]{1,2}4|\d?5[^5]{1,2}5|\d?6[^6]{1,2}6|\d?7[^7]{1,2}7|\d?8[^8]{1,2}8|\d?9[^9]{1,2}9)/m
A test
Far from ideal, but something to start from
I have a set of age data, like below;
1
2
3
4
5
6
7
8
9
10
1,1
1,2
1,3
2,12
11,13,15
7,8,12
12,15
14,16,17
15,6
13,11,10,2
And so on... I am trying to use Regex in to target a 'mixed' range of childrens ages. The logic requires at least a combination of 2 childen (so requires one of the lines with a comma), with at least one aged under 10 (min is 1), and at least one aged equal or greater to 10 (max 17).
My expected results from the above would be to return these lines below, and nothing else;
2,12
7,8,12
15,6
13,11,10,2
Any advice would be appreciated on how to resolve? Thanks in advance, I am continuing to try to correct.
You can use this regex to meet your requirements:
^(?=.*\b[1-9]\b)(?=.*\b1[0-7]\b)[0-9]+(?:,[0-9]+)+$
RegEx Demo
There are 2 lookaheads to assert 2 numbers one between 1-9 and another between 10-17
([1-9]) matches a number that should be between 1 and 9
1[0-7] matches a number that should be between 10 and 17
[0-9]+(?:,[0-9]+)+ in the regex is for matching 1 or more comma separated numbers in the middle.
You can do it with
\b\d,1[0-7]\b
provided the ages always are sorted (youngest to oldest).
If the age of 0 isn't allowed, change to
\b[1-9],1[0-7]\b
It checks for a single digit followed by a comma and one followed by a single digit in the range 0-7.
See it here at regex101.
I want to write a regular expression for Binary Numbers Divisible by 5.
I have already done the regular expressions for Binary Numbers Divisible by 2 and for 3 but I couldn't find one for 5.
Any suggestions?
(0|1(10)*(0|11)(01*01|01*00(10)*(0|11))*1)*
Add ^$ to test it with regexp. See it working here.
You can build a DFA and convert it to regular expression. The DFA was already built in another answer. You can read it, it is very well explained.
The general idea is to remove nodes, adding edges.
Becomes:
Using this transformation and the DFA from the answer I linked, here are the steps to get the regular expression:
(EDIT: Note that the labels "Q3" and "Q4" have been mistakenly swapped in the diagram. These states represent the remainder after modulus 5.)
2^0 = 1 = 1 mod 5
2^1 = 2 = 2 mod 5
2^2 = 4 = -1 mod 5
2^3 = 8 = -2 mod 5
2^4 = 16 = 1 mod 5
2^5 = 32 = 2 mod 5
... -1 mod 5
... -2 mod 5
So we have a 1, 2, -1, -2 pattern. There are two subpatterns where only the sign of the number alternates: Let n is the digit number and the number of the least significant digit is 0; odd pattern is
(-1)^(n)
and even pattern is
2x((-1)^(n))
So, how to use this?
Let the original number be 100011, divide the numbers digits into two parts, even and odd. Sum each parts digits separately. Multiply sum of the odd digits by 2. Now, if the result is divisible by sum of the even digits, then the original number is divisible by 5, else it is not divisible. Example:
100011
1_0_1_ 1+0+1 = 2
_0_0_1 0+0+1 = 1; 1x2 = 2
2 mod(2) equals 0? Yes. Therefore, original number is divisible.
How to apply it within a regex? Using callout functions within a regex it can be applied. Callouts provide a means of temporarily passing control to the script in the middle of regular expression pattern matching.
However, ndn's answer is more appropriate and easier, therefore I recommend to use his answer.
However, "^(0|1(10)*(0|11)(01*01|01*00(10)*(0|11))1)$" matches empty string.
In Excel, I have rows like below:
1 2 3 4 5
6 7 8 9 0
9 8 7 6 5
...
I need to bring all of them to the first row:
1 2 3 4 5 6 7 8 9 0 9 8 7 6 5 ...
The numbers of rows and columns are fixed.
What is the fastest way I can achieve this?
Alternatively, can I solve this on a Textpad or Notepad++ using some REGEX grouping?
If you wanted to do it with an Excel formula, pasting the following, starting in column F, would produce it across the top row:
=INDIRECT("r"&CEILING(COLUMN()/5,1)&"c"&IF(MOD(COLUMN(),5)=0,5,MOD(COLUMN(),5)),FALSE)
If your table started from A2 and your row values are to be copied from A1 onwards, following should work:
=OFFSET($A$2, (COLUMN()-COLUMN($A$1))/5, MOD(COLUMN()-COLUMN($A$1), 5))
However, I think for just a small table of size 5X10, using '=' sign manually would be the fastest.
I just select and drag them up there. When there was a pattern of projects I wrote a VBA function to do the job, but for most small unique projects select and drag worked for me.
In Notepad++, for Find what : \r\n, Replace with : 'space' with Search Mode Extended, Replace All, then copy result into Excel.
With images :) here:
Replace Carriage Return and Line Feed in Notepad++.
Need regular expression which have:
Maximum 8 digits before decimal(.) point
Maximum 4 digits after decimal point
Decimal point is optional
Maximum valid decimal is 8 digits before decimal and 4 digits after decimal
So 99999999.9999
The regular rexpression I have tried ^\d{0,8}[.]?\d{1,4}$ is failing for 123456789
and more than this. means it is taking more than 8 digits if decimal point is not available.
Tested here : http://regexpal.com/
Many many thanks in advance!
^\d{0,8}(\.\d{1,4})?$
You can make the entire decimal optional
You can try this:
^\d{1,8}(?:\.\d{1,4})?$
or
^[1-9]\d{0,7}(?:\.\d{1,4})?$
If you don't want to have a zero as first digit.
You can allow this if you want: (.1234)
^[1-9]\d{0,7}(?:\.\d{1,4})?|\.\d{1,4}$
Any of the above did not work for me.
Only this works for me
^([0-9]{0,2}((.)[0-9]{0,2}))$
This regex is working for most cases even negative prices,
(\-?\d+\.?\d{0,2})
Tested with the following,
9
9.97
37.97
132.97
-125.55
12.2
1000.00
10000.00
100000.00
1000000.00
401395011
If there is a price of $9.97, £9.97 or €9.97 it will validate 9.97 removing the symbol.
1-(\$+.[1-9])
2-(\£+.[1-9])
You can use this expression for complete price digits.
I'm using this:
^[1-9]\d{0,7}(\.\d{1-4})$
^ = the start of the string
[1-9] = at least the string has to begin with 1 number between 1 and 9
\d{0,7} = optional or max 7 times d (digit: a number between 0 and 9)
() = create a group like a substring
. = need a .
\d{1-4} = digit repited max 4 time
$ end of the string
For price validation we can not allow inputs with leading repeating zeros like 0012 etc.
My solution check for any cases. Also it allows maximum 2 decimal point after the dot.
^(?:0\.[0-9]{1,2}|[1-9]{1}[0-9]*(\.[0-9]{1,2})?|0)$