Working with digits [closed] - ruby

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I need an explanation for the pattern before the % mark in:
("%012d" % 10)
What is the role of % operator in this?

Need to look first Kernel#sprtintf :
The syntax of a format sequence is follows.
%[flags][width][.precision]type
A format sequence consists of a percent sign, followed by optional flags, width, and precision indicators, then terminated with a field type character. The field type controls how the corresponding sprintf argument is to be interpreted, while the flags modify that interpretation.
The field width is an optional integer, followed optionally by a period and a precision. The width specifies the minimum number of characters that will be written to the result for this field.
Now coming to your example : "%012d" % 10.
"%012d" called format string. The type d means - Convert argument as a decimal number.
012 means you are specifying 12 as a minimum number of characters that will be written to the result for this field.
Now look at the documentation of String#%
Format—Uses str as a format specification, and returns the result of applying it to arg. If the format specification contains more than one substitution, then arg must be an Array or Hash containing the values to be substituted. See Kernel::sprintf for details of the format string.

Related

K-consistent but not strongly K-consistent [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 1 year ago.
Improve this question
A CSP is k-consistent if, for any set of k - 1 variables and for any consistent assignment to those variables, a consistent value can always be assigned to any _k_th variable. A CSP is strongly k-consistent if it is k-consistent and is also (k - 1)-consistent, (k - 2)-consistent, ... all the way down to 1-consistent.
From the definition above, I do not understand how a CSP can just be k-consistent but not strongly k-consistent.
If the CSP is k-consistent, doesn't it necessarily have to be k-1-consistent too? If not, could you provide an example?
Consider, for example, the problem of completing a partially-filled-in Latin square.
Any consistent grid with just one blank cell can always be completed. Since only one cell is blank, the row that cell is in must be missing exactly one digit (if it's missing more than one, then some other digit must appear twice in that row by the pigeonhole principle, making the partial grid inconsistent). The same applies for the blank cell's column, and in fact it must be the same digit missing (proof is left as an exercise to the reader; hint: count the occurrences of each digit). It follows that this missing digit can be consistently assigned to that blank cell. So the CSP of n×n Latin squares is n2-consistent.
On the other hand, there are lots of consistent partial grids (i.e. grids whose filled-in digits haven't broken any of the rules so far) which cannot be filled in without breaking any rules, for example the following 2×2 grid cannot be made into a Latin square by filling in the blanks, because each of the blanks has no consistent assignment:
1 .
. 2
So this is a consistent set of assignments to two variables with no consistent assignment to a third variable, meaning that the CSP of 2×2 Latin squares is not 3-consistent; we already showed that it is 4-consistent, but now we have shown it is not strongly 4-consistent.

Create a List of string in ascending Order in lexicographically order [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I want to generate a algorithm in which I want to get the next string in lexicographically order.
Suppose I want to generate a list of length 26 then it is
['a','b'....'z']
Now suppose I want to generate a list of length 260 then it is
['a0','a1','a2'...'a9','b1'....'z0'....'z9']
This type of algorithm have max-limit. But I don't want such type of limitations. It may be 10000 or 1 millions.
Requirement
Algorithm should work in such a way that previously string is passed as argument generate by it. And it should produce the next string in lexicographically order. And I do not want to use timestamp (1503314045645)
Thanks
What about using base 36 formatted integers?
It looks like this in java:
String next(String prev) {
if(prev==null) {
return "0";
}
return Integer.toString(Integer.parseInt(prev, 36), 36);
}
Actually it's even better if you you use a simple integer for storing the value, and simply increase it every time you need the next value and format the integer using base 36 into a string:
Integer.toString(++value, 36);
In this solution the numbers are before the letters in the output, so you will get the following tokens:
a7,a8,a9,aa,ab, ... ax,ay,az,b0,b1 ... zx,zy,zz,100,101
If you want letters first or want any specific order or extra characters, then use the solution behind Matt Timmermans' link.

Send negative value OBX-5 segment in HL7

How to send negative measured numeric in OBX segment of ORU_R01 profile for PCD DEC profile using HL7 2.6 ??
and also where i can find that requirement defined by IHE..
-Thanks-
Since you have to define a data type for each Observation Value (OBX.5), your question is actually how to use HL7 data types to code negative values. For example, the HL7v2 standard defined NM as:
A number represented as a series of ASCII numeric characters consisting of an optional leading sign (+ or -), the digits and an optional decimal point.
Check HL7v2.6 Chapter 2A for other data types.
Here is a link to the IHE Pathology and Laboratory Medicine Technical Framework which contains their requirements.

How to read dale-chall mathematical notation? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
How do I calculate this dale-chall mathematical notation? or how is it converted to easier pseudo code?
I am trying to understand this concept to implement a text readability analyzer.
Is it like the following? altough ... how is what comes after 0.1579 and 0.0496 calculated?
0.1579 ( (difficult words - words) * 100 ) + 0.0496 (words - sentences)
The given formula will be written as follows in the most common programming languages:
(0.1579 * ((difficultWords / words) * 100)) + (0.0496 * (words / sentences))
The above expression will work in Python, Ruby, Javascript, Java, C, C++, C#, etc. Notice that we use * for multiplication (you can't omit the operator) and / for division, and we add as many parentheses as needed to eliminate any ambiguities in evaluation order.
When you're actually implementing the above code you'll have to be careful with divisions - some languages (for example: Java, Python 2.x) will truncate decimals if both operands are integer values. To get around this problem you can either declare the variables difficultWords, words and sentences using a data type that allows for decimals (say, double) or you can explicitly convert the variables to a decimal data type at the time of performing the division. For example, the formula will look like this in Java:
(0.1579 * (((double) difficultWords / words) * 100)) + (0.0496 * ((double) words / sentences))

What does WriteLN in this case do?

I was just wondering what the following command in a pascal program does:
WRITELN(MaxTab[index,1]:7:5,' ',
MaxTab[index,2]:8:3,' ',
MaxTab[index,3]:5:1);
MaxTab is defined as ARRAY[1..200,1..3] OF REAL, and index is a counter. Usually WRITELN simply prints the text which is written in the brackets or the variables, but I do not understand what the numbers behind ] are for (e.g. ]:7:5).
This is a Pascal construct similar to sprintf("%07.5f") in C-like languages. From the FreePascal documentation:
For real values, you can use the aforementioned syntax to display scientific notation in a specified field width, or you can convert to fixed decimal-point notation with:
Value : field_width : decimal_field_width
The field width is the total field width, including the decimal part. The whole number part is always displayed fully, so if you have not allocated enough space, it will be displayed anyway.
However, if the number of decimal digits exceeds the specified decimal field width, the output will be displayed rounded to the specified number of places (though the variable itself is not changed).
write (573549.56792:20:2);
would look like (with 11 spaces in front):
573549.57
The value after the first colon determines the width of the field in characters, the second value determines the number of digits to show following the decimal point.

Resources