Setting random number generator seed to a "random" number - random

Could you, please, let me know how I could set the random number generator seed in KDB to a more or less "random" number?
I am trying to do the following:
\S .z.i
But somehow it does not work. \S seems to expect an explicit integer, not a variable.
Thank you very much!

Just like with any \x command, when you need to pass a non-literal as an argument you should use system:
system"S ",string .z.i / no backslash before S!

Related

currency validation sholdn't allow zero or a number starts with zero

I have written a regex for currency which shouldn't accept 0 or a number starts with 0
/^\$?(?:\d+|\d{1,3}(?:,\d{3})*)(?:\.\d{1,2}){0,1}$/
But it still accepts 0 and numbers starts with 0.
In general, matching negatives with regular expressions is not the easiest thing to do. One option that would probably make your code more human readable is using multiple regular expressions, e.g., first
if (not /^\$0/)
if (/whatever else you do want it to match/)
// whatever
I think you want /^\$?(?:[1-9][0-9]*|[1-9][0-9]{0,2}(?:,[0-9]{3})*)(?:\.[0-9]{2})?$/. Not sure if that's exactly what you need, but the main point is to match [1-9] for leading digit.
Edit: doesn't allow $.99, but your example doesn't either so not sure if you want that.

How to implement Siri/Cortana like functionality in commandline?

I would like to implement a small subset of siri/cortana like features in command line.
For e.g.
$ What is the sum of 100 and 1000
> Response: 1100
$ What is the product of 10 and 12
> Response: 120
The questions are predefined regular expressions. It needs to call the matching function in ruby.
Pattern: What is the sum of (\d)+ and (\d)+
Ruby method to call: sum(a,b)
Any pointers/suggestion is appreciated.
That sounds exactly like cucumber, maybe take a look and see if you can just use their classes to hack something together :) ?
You could do something like the following:
question = gets.chomp
/\A.*(sum |product |quotient |difference )\D+([0-9]+)\D+([0-9]+).*\z/.match question
send($1, $2.to_i, $3.to_i)
Quick explanation for anyone that may be new to matching in Ruby:
This gets a line of input from the command line and scans it for a function name (i.e. sum, product, etc) followed by a space and potentially some non-digit characters. Then, it looks for a first number (similarly followed by a space and 0 or more non-digit characters) and a second number followed by nothing or anything. The parentheses determine what gets assigned to the variables preceded by a $, i.e. the substring that matches the contents of the first set of parentheses gets assigned to $1.
Next, it calls the method whose name is the value of $1 with the arguments (casted to integers) found in $2 and $3.
Obviously, this isn't generalized at all--you're putting the method names in the regex, and it's taking a fixed number of arguments--but it'll hopefully be useful for getting you on the right track.

Jmeter : Removing Spaces using RegEx

Jmeter :
I am having a JSON from which I have to fetch value of "ci".
I am using the following RegEx : ci:\s*(.*?)\" and getting the following result RegEx tester:
Match count: 1
Match1[0]=ci: 434547"
Match1=434547
Issue is Match1[0] is having spaces because of which while running the load test it says
: Server Error - Could not convert JSON to Object
Need help is correcting this RegEx.
Basically, your RegEx is fine. This is the way I would look for it too, the first group (Match[1]) would give you 434613, which is the value you are looking for. As I don't know that piece of software you are using, I have no idea why using just that match doesn't work.
Here is an idea to work around that: if the value will always be the only numeric value in the string, you could simplify the RegEx to:
\d+
This will give you a numeric value that is at least 1 digit long. If there are other numeric values in the string though, but these have different lengths, try this:
\d{m,n} --> between m and n digits long
\d{n,} --> at least n digits long
\d{0,n} --> not more than n digits long
This is not as secure / reliable as the original RegEx (since it assumes some certain conditions), but it might work in your case, because you don't have to look for groups but just use the whole matched text. Tell me if it helped!

Fortran FORMAT statement

I am trying to read in the following in line:
110134458.602 7 20957861.900
My format line is currently as follows:
READ(7,110,END=999) L1,C1,D1
write(*,*) L1,C1,D1
110 FORMAT(F14.3,1x,F1.0,2x,F14.3)
However the output I am receiving is:
110134458.60200000 7.0000000000000000 20957861.899999999
Why do I have so many decimal places and why is the final value not match?
Thank you!
It looks that you are reading the values correctly. With list-directed IO for the output the compiler will typically use the maximum number of digits for the numeric type. Not all decimal values will have exact finite precision binary equivalents -- that is probably what you are seeing for the 3rd number. If you use a format statement for the output, specifying fewer digits, the value will get rounded and appear correct.

How do you detect a "word" on a line that contains both letters and numbers?

I need a regex that detects a "word" on a line that contains both letters and numbers and is a certain length, e.g. 812d555c726d10c77b05cd164705665a. The goal is to detect lines that contain sha1 digests.
If a regex-only is too hard, a Ruby solution is OK.
This should do it:
valid_sha1 = !((str =~ /\b[a-f0-9]{32,}\b/i).nil?)
You can use the following regular expression.
s =~ /^[a-f0-9]{32,32}$/i
Both the other answers require the line begin with the SHA1. If the sha1 can be anywhere in the line, remove the ^
/[a-f0-9]{32}/i
You also don't need the second 32 in the curly braces, as if you only specify one number, it must match exactly that many.
If you really want to require that the word consist of BOTH letters and numbers (as you ask above), then you can use positive lookahead:
sha_array = line.scan(/(?=.{0,31}\d)(?=.{0,31}[a-f])\b[\da-f]{32}\b/i)
The (?= checks a condition in advance, without consuming anything.

Resources