How can I output an integer in jinja2 as a string with a leading zero? So a variable containing 4 (integer) becomes "04"?
You can use the following to zero-pad numeric strings to be at least 2 characters long:
{{ '%02d' % your_variable }}
Some examples of input and output:
9 --> 09
10 --> 10
123 --> 123
x --> TypeError: %d format: a number is required, not str
Changing the 2 yields the results you would expect and things get a little more interesting when you make room for decimals. Using '%05d' gives the following results:
9 --> 00009
10 --> 00010
123 --> 00123
123456 --> 123456
34.2 --> 00034
34.8 --> 00034
Thanks to askaroni's comment for pointing me in the right direction
Related
I am trying to identify Spanish ID numbers using REGEX on MySQL. I am took this regex to adapt it to my dataset, as the items are not isolated and might not start/end with those characters. The expressions are:
Original: ^(x?\d{8}|[xyz]\d{7})[trwagmyfpdxbnjzsqvhlcke]$
Mine:[0-9]{8,8}[A-Za-z]{1}
When I run the search using my REGEX, this is a sample of what I get:
GOOD --> 47099085T
GOOD --> D73654109H
NOT OK --> 8.30781719e-05
NOT OK --> 0113:11:19%2000:54:17.042828927Z
How can I modify [0-9]{8,8}[A-Za-z]{1} to exclude the "NOT OK" items?
Spanish ID syntax:
The number of the National Identity Document includes 8 digits and one letter for security. The letter is found by taking all 8 digits as a number and dividing it by 23. The remainder of this digit, which is between 0 and 22, gives the letter used for security. The letters I, Ñ, O, U are not used. The letters I and O are not used – to avoid confusions with the numbers 0 and 1. The Ñ is not used to avoid confusions with N.
Remainder: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
Letter: T R W A G M Y F P D X B N J Z S Q V H L C K E
-- EDIT II --
After running a test on a bigger data set, I have found other matches that should be excluded.
How can I modify (^|[^0-9.])([0-9]{8}[TRWAGMYFPDXBNJZSQVHLCKEtrwagmyfpdxbnjzsqvhlcke]) to DO NOT match:
70ce4827ce88530583ed5a1a40245f24
BE4-SGS-V2-00199982a5aa
2945a6bf-86b6-4ea0-94d9-aec84980762d
0x01010083B5627CCA663946A282DE573804AA85
xmp.iid:FE7F11740720681189A59382544B2855
Ok, according to documentation the Spanish ID system (DNI) is structured thus:
The number of the National Identity Document includes 8 digits and one letter for security. The letter is found by taking all 8 digits as a number and dividing it by 23. The remainder of this digit, which is between 0 and 22, gives the letter used for security. The letters I, Ñ, O, U are not used. The letters I and O are not used – to avoid confusions with the numbers 0 and 1. The Ñ is not used to avoid confusions with N.
Remainder: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
Letter: T R W A G M Y F P D X B N J Z S Q V H L C K E
After some exploration with Negative Lookaheads and completely failing to get them to work, we can use a more manual approach to a solution, by manually checking that the found "block" of 8 integers is not preceeded by an integer or a decimal point:
/[^\.\d][\d]{8}[TRWAGMYFPDXBNJZSQVHLCKE]/gmi
MySQL safe/syntax version:
(^|[^0-9.])([0-9]{8}[TRWAGMYFPDXBNJZSQVHLCKEtrwagmyfpdxbnjzsqvhlcke])
Example usage using REGEX_REPLACE to return rows where the id_column matches the ID syntax and returns those syntax strings:
SELECT REGEXP_REPLACE(`id_column`,
'(^|[^\\d.])(\\d{8}[TRWAGMYFPDXBNJZSQVHLCKEtrwagmyfpdxbnjzsqvhlcke])', '$2') as id_output
FROM `table_name`
WHERE id_column REGEXP '(^|[^\\d.])(\\d{8}[TRWAGMYFPDXBNJZSQVHLCKEtrwagmyfpdxbnjzsqvhlcke])'
NOTE: Prior to MySQL 8.0.17, the result returned by this function used the UTF-16 character set; in MySQL 8.0.17 and later, the character set and collation of the expression searched for matches is used. (Bug #94203, Bug #29308212)
This matches the two correct matches on your example as well as checking that only one of the valid letters comes after the numerical match.
It is important to note that using the max value in the qualifier {min,max} is pretty irrelevant because it does not mean no more than max should exist in the source string. Please see here for further reading.
What does my Regex do:
Checks that a set of 8 integers is not preceeded by either another integer or a decimal point (so 9 integers are never "captured").
Checks that the set of 8 found integers is immediately followed by one of the valid letters of either case.
You can see my Regex in action here and the corresponding MySQL demo here.
47099085T // matches
D73654109H // matches
8.30781719e-05 // unmatched
0113:11:19%2000:54:17.042828927Z // unmatched
I am looking to find a pattern to recursively split an array to odd and even elements. I will try to describe the problem in the following:
suppose we have an array of length 16 as:
a=[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
First iteration: splitting in odd and even
[0,2,4,6,8,10,12,14]
[1,3,5,7,9,11,13,15]
which basically are
a[2i] for i=0:7
a[2i+1] for i=0:7
splitting each of these arrays into odd and even elements again we have
[0,4,8,12]
[2,6,10,14]
[1,5,9,13]
[3,7,11,15]
that similarly are
4i for i=0:3
4i+2
4i+1
4i+3
splitting again the array elements would be
[0,8]
[4,12]
.
.
[1,9]
or
8i for i=0:1
8i+4
8i+2
8i+6
8+1
8i+5
8i+3
8i+1
Arrays needed to split recursively until each array has only two elements.
One things that I noticed that the bottom half is similar to the top one and we just need to add "1" to the index terms
I was wondering how Can I find the pattern for an array with an "n" elements?
Thank you very much for your time.
assuming your number n is a power of 2 (aka 2^k):
then you will have m = n/2 = 2^(k-1) arrays with following numbers for i in {0,1}:
0: m*i+f(0)
1: m*i+f(1)
...
j: m*i+f(j)
...
m-1: m*i+f(m-1)
where f(x) is a function which takes an integer (x), transforms it into an k-1-bit binary number (b), reverses it (rb) and returns its decimal value (y).
Example for k=4 (which looks a lot like your values):
x
b
rb
f(x)=y
0
000
000
0
1
001
100
4
2
010
010
2
3
011
110
6
4
100
001
1
5
101
101
5
6
110
011
3
7
111
111
7
How do you represent (decimal) integer 50 in binary?
How many bits must be "flipped" in order to capitalize a lowercase 'a' that is represented in ASC11?
How do you represent the (decimal) integer 50 in, oh, "hexadecimal," otherwise known as base-16? Recall that decimal is simply base-10, and binary is simply base-2. Infer from those base systems how to represent this one?
Please answer these questions for me.HELP.
To help you some:
Binary is only made up of 1's and 0's.This may help you understand binary conversion
Decimal is 0-9
Hexadecimal is 0-9, then A-F (so A would represent 10, B would be 11, etc up to F which is 15)
Converting from decimal to another base
Here some tips for you regarding conversion to binary:
What is 50 mod 2? What about 25 mod 2 and then 12 mod 2? What are your results if you continue this?
What does any number mod 2 (always) return as result? - 1 or 0
Do you realise any patterns? - You get the reversed binary number as result
Test case 50:
50 mod 2 = 0 - 6th digit
25 mod 2 = 1 - 5th digit
12 mod 2 = 0 - 4th digit
6 mod 2 = 0 - 3rd digit
3 mod 2 = 1 - 2nd digit
1 mod 2 = 1 - 1st digit
The remainders of the divisions concatenated and reverses are: 110010, which is 50 in binary.
Can this be also transformed to further bases? - Yes, as we see with trying to convert 50 to hexadecimal:
50 mod 16 = 2 - 2nd digit
3 mod 16 = 3 - 1st digit
The remainders again concatenated and reversed are 32, which conveniently is 50 in hexadecimal.
In general we can say to convert a number to an arbitrary base you must take the remainder of the number and the base and then divide the number by the base and do the same thing again. In a program this would look something like:
while the number is greater 0 do:
result = (number mod base) + result;
number = number div base;
Converting from any base to decimal
How do you convert a number from an arbitrary base into base 10? First let us do a test case with binary. Lets take the 50 from the previous example: 110010
The method to convert from binary is multiplying every digit with the base to the power of the position of it in the number and adding up the result. The enumeration of the positions begins with 0 at the least significant digit. Our previous number would then look something like this:
1 *2^5 + 1 *2^4 + 0 *2^3 + 0 *2^2 + 1 *2^1 + 0 *2^0
What simplifies to:
32 + 16 + 2 = 50
It also works with any other base, like our 32 from the previous example:
3 *16^1 + 2*16^0 = 48 + 2 = 50
In program this would look something like this:
from end of number to beginning do:
result = result + digit * (base ^ position)
Is there a way that an Octave Matrix would hold Strings and numbers together?
I want to have a matrix of the fallowing type:
A=["A","B","C","D";1,2,3,4;2,3,4,5;3,4,5,6;4,5,6,7];
So that the matrix will look like:
A B C D
1 2 3 4
2 3 4 5
3 4 5 6
4 5 6 7
But when I try this I get:
ABCD
empty line
empty line
empty line
empty line
*empty line represents an empty line
And if I try to put strings that are more than 1 character in length, I get a number of columns mismatch error.
Is there a way to create a "mixed" octave matrix?
It sounds like you may be looking for a cell array.
Does anyone know how I can solve this problem? Any help would be great...... I cant seem to get my head around it.
As you know binary digits can only be either 1 or 0.
Say you had a 8 digit Binary number like a byte >>>>>> 0001 1000.
I'm trying to figure out an equation for the number of combinations you could get from an 8 digit binary number.
For example, if you had a two digit binary number, the binary combinations that you could have are:
00
01
10
11
Therefore the total combinations from a 2 digit binary number is 4.
Example 2
If you had a 3 digit number, the combinations would be:
000
001
010
100
101
111
110
011
Therefore the number of binary combinations from a 3 digit number is 8.
Example 3
If it were a 4 digit number, maximum binary combinations that you could have are either
0000
0001
0010
0100
1000
0111
0110
1111
1110
1101
1011
1001 Total maximum combination = 12
I Guess in a nutshell what im asking is .... if i had any number 6,7,15,8 or any number... how could i calculate the total maximum Binary combinations is there an equation to it ... I cant figure it out..ive tried for days now ;(
The number of numbers composed by d digits in base b is
b^d
n - number of digits
b - base
^ - power
b^n
So your base is 2 (binary), and u want to check combinations for 8 digit number
2^8 = 256