i see a sql including "format Z(19)9" in terdata environment, and i checked in teradata doc,
the following is for "Z":
Zero‑suppressed decimal digit.
Translates to blank if the digit is zero and preceding digits are also zero.
A FORMAT phrase containing Z characters (including Z(I) and Z(F)), a combination of commas,
dots, G, or D, and no other formatting characters means “blank when zero.”
For example, ZZZZZ, ZZ.Z, GZ(I)DZ(F), GZZZZZZDZZ and Z,ZZZ.ZZ print only blanks if the number
is zero.
A Z cannot follow a 9.
Repeated Z characters must appear to the left of any combination of the radix and any 9
formatting characters.
The characters to the right of the radix cannot be a combination of 9 and Z characters; they
must be all 9s or all Zs. If they are all Zs, then the characters to the left of the radix
must also be all Zs.
If a group of repeated Z characters appears in a FORMAT phrase with a group of repeated sign
characters, the group of Z characters must immediately follow the group of sign characters.
For example, --ZZZ.
and the following is for 9:
Decimal digit (no zero suppress).
but i am still confused, what's the meaning of format Z(19)9 ? for example, the data is 0000.234, then the result will be .234000000 ?
Related
I want to match binomials, that is, bisyllabic words, sometimes hyphenated, with slightly varied syllable reduplication; the variation always concerns the first (and, possibly, second) letter in the reduplicated syllable:
x <- c("pow-wow", "pickwick", "easy-peasy", "nitty-gritty", "bzzzzzzz", "mmmmmm", "shish", "wedged", "yaaaaaa")
Here, we have said syllable reduplication in pow-wow, pickwick, easy-peasy, and nitty-gritty (which are then the expected output) but not in bzzzzzzz, mmmmmm, shish, wedged and yaaaaa.
This regex does at least manage to get rid of wedged(which is pronounced as one syllable) as well as monosyllabic words by requiring the presence of a vowel in the capturing group:
grep("\\b\\w?((?!ed)(?=[aeiou])\\w{2,})-?\\w\\w?\\1\\b$", x, value = T, perl = T)
[1] "pow-wow" "pickwick" "easy-peasy" "nitty-gritty" "yaaaaa"
However, yaaaaa is getting matched too. To not match it my feeling is that the capturing group should be disallowed to contain two identical vowels in immediate succession but I don't know how to implement that restriction.
Any ideas?
It looks as though you want to match words that cannot contain ed after the initial chars and 2 or more repeated chars if the same chunk is not found farther in the string. Also, the allowed "difference" window at the start and middle is 0 to 2 characters.
You may use
\b\w{0,2}(?!((.)\2+)(?!.*\1)|ed)([aeiou]\w+)-?\w\w?\3\b
See the regex demo
Details
\b - a word boundary (you may use ^ if your "words" are equal to whole string)
\w{0,2} - two or more word chars (replace with \p{L} to only match letters)
(?!((.)\2+)(?!.*\1)|ed) - no ed or two or more identical chars that do not repeat later in the string are allowed immediately to the right of the current location
([aeiou]\w+) - a vowel (captured in Group 3) and 1+ word chars (replace with \p{L} to only match letters)
-? - an optional hyphen
\w\w? - 1 or 2 word charsd
\3 - same value as captured in Group 3
\b - a word boundary (you may use $ if your "words" are equal to whole string)
I need to remove ending zeros from binary bit sequences.
The length of the bit sequence is fixed, say 52. i.e.,
0101111.....01100000 (52-bit),
10111010..1010110011 (52-bit),
10111010..1010110100 (52-bit).
From converting decimal number to normalized double precision, significand is 52 bit, and hence zeros are populated to the right hand side even if significand is less than 52 bit at first step. I am reversing the process: i.e., I am trying to convert a normalized double precision in memory to decimal number, hence, I have to remove zeros (at the end) that are used to populate 52 bits for significand.
It is not guaranteed that the sequence in hand necessarily have 0s in the end (like the 2nd example above). If there is, all ending zeros must be truncated:
f(0101111.....01100000) # 0101111.....011; leading 0 must be kept
f(10111010..1010110011) # 10111010..1010110011; no truncation
f(10111010..1010110100) # 10111010..10101101
Unfortunately, the number of truncated 0s at the end differs. (5 in the 1st example; 2 in the 3rd example).
It is OK for me if input and output class are string:
f("0101111.....01100000") # "0101111.....011"; leading 0 must be kept
f("10111010..1010110011") # "10111010..1010110011"; no truncation
f("10111010..1010110100") # "10111010..10101101"
Any help is greatly appreciated.
This is a simple regular expression.
f <- function(x) sub('0+$', '', x)
Explanation:
0 - matches the character 0.
0+ - the character zero repeated at least one time, meaning, one or more times.
$ matches the end of the string.
0+$ the character 0 repeated one or more times and nothing else until the end of the string.
Replace the sub-string matched by the pattern with the empty string, ''.
Now test the function.
f("010111101100000")
#[1] "0101111011"
f("0100000001010101100010000000000000000000000000000000000000000000")
#[1] "010000000101010110001"
f("010000000101010110001000000")
#[1] "010000000101010110001"
f("00010000000101010110001000000")
#[1] "00010000000101010110001"
I have a field which contains two charecters, some digits and potentially a single letter. For example
QU1Y
ZL002
FX16
TD8
BF007P
VV1395
HM18743
JK0001
I would like to consistently return all letters in their original position, but digits as follows.
for 1 to 3 digits :
return all digits OR the digits left padded with zeros
For 4 or more digits :
it must not begin with a zero and return the 4 first digits OR if the first is a zero then truncate to three digits
example from the data above
QU001Y
ZL002
FX016
TD008
BF007P
VV1395
HM1874
JK001
The implementation will be in R but I'm interested in a straight regex solution, I'll work out the R side of things. It may not be possible in straight regex which is why I can't get my head round it.
This identifies the correct ones, but I'm hoping to correct those which are not
right.
"[A-Z]{2}[1-9]{0,1}[0-9]{1,3}[F,Y,P]{0,1}"
For the curious, they are flight numbers but entered by a human. Hence the variety...
You may use
> library(gsubfn)
> l <- c("QU1Y", "ZL002", "FX16", "TD8", "BF007P", "VV1395", "HM18743", "JK0001")
> gsubfn('^[A-Z]{2}\\K0*(\\d{1,4})\\d*', ~ sprintf("%03d",as.numeric(x)), l, perl=TRUE)
[1] "QU001Y" "ZL002" "FX016" "TD008" "BF007P" "VV1395" "HM1874" "JK001"
The pattern matches
^ - start of string
[A-Z]{2} - two uppercase letters
\\K - the text matched so far is removed from the match
0* - 0 or more zeros
(\\d{1,4}) - Capturing group 1: one to four digits
\\d* - 0+ digits.
Group 1 is passed to the callback function where sprintf("%03d",as.numeric(x)) pads the value with the necessary amount of digits.
I have some 16 character hex strings like this:
B5A43BC5BDCEEFC6
2C7C27F05A488897
1514F4EC47C2EBF6
D91ED66BC999EB64
I want to shorten them and have the shortened string only contain upper case letters.
DeflateStream and GZipStream just increase the length.
Anyone can help me shorten these 16 characters hex string to 6 characters or fewer?
Alternatively, shortening a 32 character hex string to 12 characters or fewer is okay.
Unless there is some redundancy in your 16 hexadecimal character input, what you are asking is mathematically impossible. You can prove this by examining the entropy of your inputs.
You have 16 hexadecimal characters.
16^16 = 18446744073709551616 ≈ 1.84x10^19 possible values.
You want the string to be 6 upper (or lower - the maths is the same) case characters or fewer. In English (I assume you want English) there are 26 uppercase characters.
26^6 = 308915776 ≈ 3.09x10^8 possible values.
To guarantee that you can represent every one of your 16 hexadecimal characters, you need 14 upper (or lower) case letters.
13 characters isn't enough:
26^13 = 2481152873203736575 ≈ 2.48x10^18 possible values.
14 characters will suffice:
26^14 = 64509974703297150976 ≈ 6.45x10^19 possible values.
The only way you could possibly do it (assuming no redundancy) in six (or fewer) characters is to have some base where each character has 1626 possible values.
1626^6 = 18480905552168525376 ≈ 1.849x10^19 possible values.
Shortening 32 hexadecimal characters to 12 or fewer upper (or lower) case characters is impossible by the same logic. Without redundancy, you can't guarantee that you can shorten any arbitrary 16 (or 32) hexadecimal characters into 6 (or 12) upper (or lower) case characters.
You can convert hexadecimal number to a higher base like sexagesimal:
Quickest way to convert a base 10 number to any base in .NET?
If you have a randomly generated password, consisting of only alphanumeric characters, of length 12, and the comparison is case insensitive (i.e. 'A' == 'a'), what is the probability that one specific string of length 3 (e.g. 'ABC') will appear in that password?
I know the number of total possible combinations is (26+10)^12, but beyond that, I'm a little lost. An explanation of the math would also be most helpful.
The string "abc" can appear in the first position, making the string look like this:
abcXXXXXXXXX
...where the X's can be any letter or number. There are (26 + 10)^9 such strings.
It can appear in the second position, making the string look like:
XabcXXXXXXXX
And there are (26 + 10)^9 such strings also.
Since "abc" can appear at anywhere from the first through 10th positions, there are 10*36^9 such strings.
But this overcounts, because it counts (for instance) strings like this twice:
abcXXXabcXXX
So we need to count all of the strings like this and subtract them off of our total.
Since there are 6 X's in this pattern, there are 36^6 strings that match this pattern.
I get 7+6+5+4+3+2+1 = 28 patterns like this. (If the first "abc" is at the beginning, the second can be in any of 7 places. If the first "abc" is in the second place, the second can be in any of 6 places. And so on.)
So subtract off 28*36^6.
...but that subtracts off too much, because it subtracted off strings like this three times instead of just once:
abcXabcXabcX
So we have to add back in the strings like this, twice. I get 4+3+2+1 + 3+2+1 + 2+1 + 1 = 20 of these patterns, meaning we have to add back in 2*20*(36^3).
But that math counted this string four times:
abcabcabcabc
...so we have to subtract off 3.
Final answer:
10*36^9 - 28*36^6 + 2*20*(36^3) - 3
Divide that by 36^12 to get your probability.
See also the Inclusion-Exclusion Principle. And let me know if I made an error in my counting.
If A is not equal to C, the probability P(n) of ABC occuring in a string of length n (assuming every alphanumeric symbol is equally likely) is
P(n)=P(n-1)+P(3)[1-P(n-3)]
where
P(0)=P(1)=P(2)=0 and P(3)=1/(36)^3
To expand on Paul R's answer. Probability (for equally likely outcomes) is the number of possible outcomes of your event divided by the total number of possible outcomes.
There are 10 possible places where a string of length 3 can be found in a string of length 12. And there are 9 more spots that can be filled with any other alphanumeric characters, which leads to 36^9 possibilities. So the number of possible outcomes of your event is 10 * 36^9.
Divide that by your total number of outcomes 36^12. And your answer is 10 * 36^-3 = 0.000214
EDIT: This is not completely correct. In this solution, some cases are double counted. However they only form a very small contribution to the probability so this answer is still correct up to 11 decimal places. If you want the full answer, see Nemo's answer.