Efficient way to code a number within a range? - logic

If I have something like:
value is between 1-1000
And if value is within 1-100, output A
within 101-200, output B
within 201-300, output C
within 301-400, output D
within 401-500, output E
else, output F
Can this be done more "efficiently" or better than having if statements for each one?

You could use a mapping between value and output:
outputs = [ A, B, C, D, E, F, F, F, F, F]
output = outputs[(int)((value - 1)/ 100)]

Related

FILTER has mismatched range sizes. Expected row count:

I have been using these 2 queries to fetch data in 2 different sheets
=query('Raw Data'!A3:P62481,"select B, D, E, F, G, H, I, J, K, L, M, N, O, P where B = date '"&text($B$1,"yyyy-mm-dd")&"'",1)
=sort(unique(FILTER('Raw Data'!$A:$P,'Raw Data'!$G:$G=$C$1,'Raw Data'!$B:$B=$F$1)))
Looking for a way to combine both. And get output in one single sheet, get data for specific DATE and C1.
Please help!
Already tried this:
=sort(unique(FILTER('Raw Data'!$A:$P,'Raw Data'!$G:$G=$C$1, QUERY(query('Raw Data'!A3:P62481,"select B, D, E, F, G, H, I, J, K, L, M, N, O, P where B = date '"&text($B$1,"yyyy-mm-dd")&"'",1)))))
=sort(unique(FILTER(QUERY('Raw Data'!$A:$P,"select B, D, E, F, G, H, I, J, K, L, M, N, O, P where B = date '"&text($F$1,"yyyy-mm-dd")&"'",1,'Raw Data'!$G:$G=$C$1))))
Error FILTER has mismatched range sizes. Expected row count: 2745.
column count: 1. Actual row count: 62481, column count: 1.
Link to the sheet: https://drive.google.com/file/d/1ymFGf9eNzCoWHLwLjolQliOMBwswgaPP/view?usp=sharing
in your first formula, the output is 14 columns while in your 2nd formula the output is 16 columns. there is no way how to combine it unless you have equal column count in the matrix. one of the ways would be adding two fake columns in the query like:
={QUERY('Raw Data'!A3:P62481,
"select B,D,E,F,G,H,I,J,K,L,M,N,O,P,' ',' '
where B = date '"&TEXT($B$1, "yyyy-mm-dd")&"'
label ' ''',' '''", 1);
SORT(UNIQUE(FILTER('Raw Data'!$A:$P, 'Raw Data'!$G:$G=$C$1, 'Raw Data'!$B:$B=$F$1)))}

Sort nested array by content

I have a data structude ST that contains an array of entities of ST.
struct ST {
ST[];
}
I need to sort it by deep-value.
For example i have an array of ST: [A { B, C, D }, B { C, D }, C, D { E }, E, F]
And i want to get result like this: [E, D, C, B, E, F, A]
Can somebody help me?
The solution depends on what language you are using however the basic idea remains the same. The idea is that each struct can have a value assigned which can then be used for sorting.
A similar question was asked here: Sort ArrayList of custom Objects by property
I hope this helps, but generally you should try asking questions in greater detail

How to exchange lines in the same file?

I have a text like this (in rows):
A
B
C
D
E
F
and I'd like to change line B by line D, and line C to by line E, obtaining (in rows):
A
D
E
B
C
F
is it any simple way to do it with bash?
You can use the mapfile builtin to read the entire file into an array of lines. Then in that array reorder however you want and write the array back out to a file.

Hadoop Pig comparing two values and sort them

I'm currently learning the hadoop framework and the pig latin language.
Now I've a problem.
I've got a data-set with the following format:
"long a, long b, char c, char d"
Now I want to read this data-sets with pig. That's no problem with the load and PigStoarage funktion..
bla = load 'data/examples/test' as (a:long, b:long, c:chararray, d:chararray);
My next step is, that I want to compare a with b on each line. If a is greater than b it's okay. If b is greater than a, I wan't to switch a with b, so that the higher value is always the first value of my data set...
Is this possible? In Java I can do this with a simple "compareTo"...
sorry for my bad english :-)
blb = FOREACH bla GENERATE ((a < b) ? b : a), ((a < b) ? a : b), c, d;
This operator in Pig is called bincond. The first one says, if a is less than b, then output b. The second one says, if a is less than b, then output a. Notice that when a is greater than b, it outputs the opposite.

ctags regex for multiple declarations in one line

I'm writing a .ctags file for a custom language... Like most languages, it allows for multiple variable declarations in one line.. i.e.:
int a, b, c;
I have a basic regex which recognizes 'a':
--regex-mylang=/^[ \t]*int[ \t]*([a-zA-Z_0-9]+)/\1/v,variable/
How do I modify this to have it match 'b' and 'c', as well? I can't find anything in ctags documentation that deals with multiple matches in a single line.
The latest Universal-ctags can capture them.
[jet#localhost]/tmp% cat input.x
int a, b, c;
[jet#localhost]/tmp% cat x.ctags
--langdef=X
--map-X=.x
--kinddef-X=v,var,variables
--_tabledef-X=main
--_tabledef-X=vardef
--_mtable-regex-X=main/int[ \t]+//{tenter=vardef}
--_mtable-regex-X=main/.//
--_mtable-regex-X=vardef/([a-zA-Z0-9]+)/\1/v/
--_mtable-regex-X=vardef/;//{tleave}
--_mtable-regex-X=vardef/.//
[jet#localhost]/tmp% u-ctags --options=x.ctags -o - ./input.x
a ./input.x /^int a, b, c;$/;" v
b ./input.x /^int a, b, c;$/;" v
c ./input.x /^int a, b, c;$/;" v
See https://docs.ctags.io/en/latest/optlib.html#advanced-pattern-matching-with-multiple-regex-tables for more details.
After going through this for a few hours, I'm convinced it can't be done. No matter what, the regular expression will only expand to one tag per line. Even if you put \1 \2 \3 ... as the expansion, that would just cause a tag consisting of multiple matches, instead of one tag per match.
It parses the C example correctly because inside the ctags source code it uses an actual code parser, not a regexp.
You're trying to do parsing with a regex, which is not generally possible. Parsing requires the equivalent of storing information on a stack, but a regular expression can embody only a finite number of different states.
it can be partialy done with the Universal Ctags and with the help of {_multiline=N} and {scope} flag. The N is group number which position is saved in generated tags file.
For more information look here: docs/optlib.rst
Configuration: mylang.ctags
--langmap=mylang:.txt
--regex-mylang=/^[[:blank:]]*(int)[[:blank:]]/\1/{placeholder}{scope=set}{_multiline=1}
--regex-mylang=/(;)/\1/{placeholder}{scope=clear}
--regex-mylang=/[[:blank:]]*([[:alnum:]]+)[[:blank:]]*,?/\1/v,variable/{_multiline=1}{scope=ref}
Test file: test.txt
void main() {
int a, b, c, d;
}
Generate tags with: ctags --options=mylang.ctags test.txt
Generated tags file:
!_TAG_FILE_FORMAT 2 /extended format; --format=1 will not append ;" to lines/
!_TAG_FILE_SORTED 1 /0=unsorted, 1=sorted, 2=foldcase/
!_TAG_OUTPUT_MODE u-ctags /u-ctags or e-ctags/
!_TAG_PROGRAM_AUTHOR Universal Ctags Team //
!_TAG_PROGRAM_NAME Universal Ctags /Derived from Exuberant Ctags/
!_TAG_PROGRAM_URL https://ctags.io/ /official site/
!_TAG_PROGRAM_VERSION 0.0.0 /cb4476eb/
a test.txt /^ int a, b, c, d;$/;" v
b test.txt /^ int a, b, c, d;$/;" v
c test.txt /^ int a, b, c, d;$/;" v
d test.txt /^ int a, b, c, d;$/;" v
int test.txt /^ int a, b, c, d;$/;" v
main test.txt /^void main() {$/;" v
void test.txt /^void main() {$/;" v
--regex-perl=/^\s*?use\s+(\w+[\w\:]*?\w*?)/\1/u,use,uses/
--regex-perl=/^\s*?require\s+(\w+[\w\:]*?\w*?)/\1/r,require,requires/
--regex-perl=/^\s*?has\s+['"]?(\w+)['"]?/\1/a,attribute,attributes/
--regex-perl=/^\s*?\*(\w+)\s*?=/\1/a,aliase,aliases/
--regex-perl=/->helper\(\s?['"]?(\w+)['"]?/\1/h,helper,helpers/
--regex-perl=/^\s*?our\s*?[\$#%](\w+)/\1/o,our,ours/
--regex-perl=/^=head1\s+(.+)/\1/p,pod,Plain Old Documentation/
--regex-perl=/^=head2\s+(.+)/-- \1/p,pod,Plain Old Documentation/
--regex-perl=/^=head[3-5]\s+(.+)/---- \1/p,pod,Plain Old Documentation/

Resources