Flipped switch statements? - switch-statement

Consider you have 10 boolean variables of which only one can be true at a time, and each time any one is 'switched on', all others must be 'turned off'. One of the problems that immediately arises is;
How can you quickly test which variable is true without necessarily
having to linearly check all the variable states each time?
For this, I was thinking if it was possible to have something like:
switch(true)
{
case boolean1:
//do stuff
...
//other variables
}
This looks like a bad way of testing for 10 different states of an object, but I think there're cases where this kind of feature may prove useful and would like to know if there's any programming language that supports this kind of feature?

There isn't a language feature that offers this behavior. But as an alternative, you could use the Command Pattern, in conjunction with a Priority Queue. This assumes that you would be able to prioritize what checks should be done.

Traditionally, when you have such radio button boolean values you use an integer to represent them:
+------------+---------+--------------------+
| BINARY | DECIMAL | BINARY-LOGARITHMIC |
+------------+---------+--------------------+
| 0000000001 | 1 | 0 |
| 0000000010 | 2 | 1 |
| 0000000100 | 4 | 2 |
| 0000001000 | 8 | 3 |
| 0000010000 | 16 | 4 |
| 0000100000 | 32 | 5 |
| 0001000000 | 64 | 6 |
| 0010000000 | 128 | 7 |
| 0100000000 | 256 | 8 |
| 1000000000 | 512 | 9 |
+------------+---------+--------------------+
Let's call the variable holding this boolean value flag. We can quickly jump to some code based on the flag by indexing a random access array of functions:
var functions = [ function0
, function1
, function2
, function3
, function4
, function5
, function6
, function7
, function8
, function9
];
functions[flag](); // quick jump
However, you will have to pay for the function call overhead.

Related

In Azure Application Gateway, is it possible to do math operations when rewriting URLs/Query Strings?

I am designing a service that needs to route requests based on the remainder from the division of a path or query string parameter by a constant value. For example, let's say the incoming address looks like abc.com/1234 and that the constant's value is 5.
Here is a list of sample incoming requests paths, underlying expression, and desired routes:
| Incoming | Expression | Route |
|:---------|:----------:| -----:|
| abc.com/10 | 10 % 5 = 0 | xyz-0.com |
| abc.com/11 | 11 % 5 = 1 | xyz-1.com |
| abc.com/12 | 12 % 5 = 2 | xyz-2.com |
| abc.com/17 | 17 % 5 = 2 | xyz-2.com |
This question mentions the same kind of rewrite/redirect, but regex replace seems to not be supported.
Would it be possible to achieve this with Azure Application Gateway rewrites (or Azure Front Door; looks to have the same capabilities as AG as far as rewrites go)?

priority in Decision Table rows in IBM ODM

If i have 10 rows in my decision table, out of which i want to execute only row 6 and row 4 without disabling the remaining row.
How can we achieve this functionality?
However, in Action Rule we can set the priority, but we cannot set priority for each row in Decision Table.
I've tried searching any option available in Decision Table properties but there's non. Please help with this.
You may need to implement a logic like this : this is a called rule chaining.
a rule(row) makes a modification, and then another rule(row) gets activated, and so on.
This needs the RETE mode to work, please look at ODM documentation for RETE, and keep in mind to set the method "SET NEXT" to update the RETE, in the BOM settings.
This example shows a chaining mechanism with different options based on the initial value of NEXT.
| NEXT ? | CONDITION 2| ACTIVE ? | ACTION 1 | SET NEXT |
| aa1 | bb1 | true | do this1 | aa2 |
| aa2 | bb2 | true | do this2 | aa3 |
| aa3 | bb3 | true | do this3 | aa7 |
| aa4 | bb4 | false | do this4 | aa6 |
| aa5 | bb5 | true | do this5 | aa1 |
| aa6 | bb6 | false | do this6 | xxx |
| aa7 | bb7 | true | do this7 | aa8 |
| aa8 | bb8 | true | do this8 | aa9 |
| aa9 | bb9 | true | do this9 | aa10 |
| aa10 | bb10 | true | do this10 | xxx |
But this is last measure solution!
I don't know your model, the conditions, etc... out of the blue, you may want to rework your conditions because selecting rule in a DT with a priority like is not needed in usual cases, that is to say 99.9% of the time!
However, if the DT conditions columns are all exclusive as a partition, you may have only one rule(row) among the 10 rows that matches provided data at once.Then no need to order the rule execution.
But its not always the case, sometimes some rules(row) in a DT may match, then few row may execute on the same data, this because some condition may overlap.
For example: lets consider this DT with two conditions columns and two rows(rules) and one actions columns.(there is no verbalization, just the logic)
| <= age >= | Date after | action |
| 18 , 21 | 07-MAY-2020 | do this |
| 16 , 20 | 12-MAY-2020 | do that |
In this case, rule condition overlaps and for data with age = 19 and date = 12-JUNE-2020, this two rules(rows) will match, and "do this" and "do that" will be executed.
If the 10 rules(rows) of your DT, are matching with provided data at runtime, the conditions are not exclusive enough, and you may need to change/rework the DT conditions's design:
add new condition columns for example, to discriminate rules(rows).
Or correct overlapping conditions
Let's take the previous sample and add a new discriminating condition column: GENDER for example
| <= age >= | Date after | GENDER | action |
| 18 , 21 | 07-MAY-2020 | M | do this |
| 16 , 20 | 12-MAY-2020 | F | do that |
In this case, the new column will allow to have only one rule to be matched at once.
Or rework the initial conditions
| <= age >= | Date after | action |
| 18 , 21 | 07-MAY-2020 | do this |
| 22 , 24 | 12-MAY-2020 | do that |
Now, even though second condition overlaps,for data with age = 19 and date = 12-JUNE-2020, only first rule(row) will match, and "do this" will be executed.
I am quite puzzled by the fact you want to order things with priorities.
Priorities may help in some peculiar cases(again), but its always a problem if it is used all over the place(e.g. more than 5 priorities in rules) and it becomes a nightmare to maintain because priorities will never convey why a rule should apply before another.
Instead, as a best practice, use a workflow to group rules in ruletask with a sequential tasks ruleflow, whereas a ruletask name conveys the semantic of grouping/separating.
e.g. Init data, data check, data validation, data transformation, etc...
Whenever you need to order rules, use ruletasks in a ruleflow! Then if you need to do so, you may split your DT into 2 decision tables, so that some rule in the first table may apply before other rule in the second decision table.

Does RandomForestsClassifier have a `decision_function`?

SVC in sklearn has a decision_function function, but why doesn't random forests? Secondly, how can I emulate the decision_function for a RandomForestsClassifier?
Yes, it has a decision function.
The function is coded directly into the Forest of Randomised Trees.
Simply put RandomForest learners use this trivial decision function strategy:
Rule a): The Forest ( the ensemble ) votes on result.
Rule b): Each Randomised Tree contains a series of decisions ( represented by Node(s).
Rule c): Each Node contains one or more simple conditions, based on which the decision making process moves farther from the root-Node towards the terminal-Leaf, according to the specified values of the Example FeatureVECTOR-input values.
Rule d): Terminal-Leaf contains a value, that a particular Randomised Tree presents to the ensemble voting ( Ad-a) ).
While this is not an exact RandomForestClassifier Tree-representation ( a Node can have more than one condition there - check the Scikit documentation on parameters ) but a fairly well illustrating the principles:
VOTE[12] = IN[0] < 6.85417 ? 1 : 2
| |
| 2:[IN[5]<183]
| |
| IN[5] < 183 ? 5 : 6
| | |
| | 6:[IN[10]<1.00118]
| | |
| | IN[10] < 1.00118 ? 13 : 14
| | | |
| | | 14:
| | | |
| | |
| | |
| |
| 5:[IN[6]<187]
| |
| IN[6] < 187 ? 11 : 12
| | |
1:
IN[10] < 1.00054 ? 3 : 4
| |
| 4:
| |
|
3:
voter[12]:
0:[inp0<6.85417] yes=1,no=2,missing=1
1:[inp10<1.00054] yes=3,no=4,missing=3
3:[inp21<0.974632] yes=7,no=8,missing=7
7:[inp22<1.01021] yes=15,no=16,missing=15
15:[inp15<0.994931] yes=31,no=32,missing=31
31:[inp12<0.999151] yes=63,no=64,missing=63
63:[inp23<0.957624] yes=111,no=112,missing=111
111:leaf=0.163636
112:leaf=-0.36
64:leaf=0.323077
32:[inp19<0.993949] yes=65,no=66,missing=65
65:[inp23<0.931146] yes=113,no=114,missing=113
113:leaf=-0
114:[inp23<0.972193] yes=161,no=162,missing=161
161:leaf=-0.421782
162:leaf=-0.133333
66:[inp2<61] yes=115,no=116,missing=115
115:leaf=0.381818
116:leaf=-0.388235
16:[inp17<0.985065] yes=33,no=34,missing=33
How to emulate decision function?
One can build another RandomForest-interpreter to traverse all the Trees and vote on resulting "ensemble compromise".
That will mimick the decision function ( there is no other there ) and would provide the same results as the RandomForest Trees were constructed to work this very way.

Extending the shunting-yard algorithm to support the conditional ternary operator

How to extend the shunting yard algorithm, that's originally meant for binary operators to support the conditional ternary operator ("a ? b : c") ?
I haven't seen an answer to this here and I have one, so I'm posting it.
The way I did it was to add three new operators:
"?" ternary-open-if
":" ternary-else
ternary-closed-if
Only the first two will be created directly when reading the initial expression.
However, only the third one will exists in the output (the RPN of the initial expression).
The ternary-open-if is put on the operators stack whenever a "?" is seen.
The ternary-else is never put on the stack. Rather, the stack is poped until a ternary-open-if is found, then the ternary-open-if is replaced with the ternary-closed-if (thus indicating that we're in the else part of the conditional operator).
All three operators have higher precedence than all other operators (higher meaning they're evaluated AFTER other operators).
The ternary-if operators have the same precedence and right associativity (like in C), meaning that a ternary-if will never cause a pop of another ternary-if.
The ternary-else has a precedence higher than the ternary-ifs, and its associativity is irrelevant (since it is never put on the stack). So, when encountering a ternary-open-if it will convert it to a closed one as mentioned before.
When encountering a ternary-closed-if it will pop it.
Examples (ternary-closed-if notated as "?:"):
"a ? b : c" -->
"a b c ?:"
"a ? b : x ? y : z" -->
"a b x y z ?: ?:"
"a ? x ? y : z : b" -->
"a x y z ?: b ?:"
This method is more hard to explain than to implement, and it does make a slight change to the algorithm, so if anyone has a simpler solution, please post it.
For anyone still searching here, the conditional ternary operator can also be implemented as an IF function with three arguments:
IF([boolean], [expr_if_true], [expr_if_false])
For example, to convert IF(5 > 4, 9, 8) to Reverse Polish Notation by extending the shunting yard algorithm, do:
+-------+---------------------------------------------------+--------------+----------------+
| Token | Action | RPN Output | Operator stack |
+-------+---------------------------------------------------+--------------+----------------+
| IF | Push token to stack | | IF |
| ( | Push token to stack | | IF ( |
| 5 | Add token to output | 5 | IF ( |
| > | Push token to stack | 5 | 15 ( > |
| 4 | Add token to output | 5 4 | IF ( > |
| , | Pop from stack onto output until left parenthesis | 5 4 > | IF ( |
| 9 | Add token to output | 5 4 > 9 | IF ( |
| , | Pop from stack onto output until left parenthesis | 5 4 > 9 | IF ( |
| 8 | Add token to output | 5 4 > 9 8 | IF ( |
| ) | Pop from stack onto output until left parenthesis | 5 4 > 9 8 | IF |
| end | Pop entire stack onto output | 5 4 > 9 8 IF | |
+-------+---------------------------------------------------+--------------+----------------+
The postfix is evaluated as:
+-------+------------------------------------------------------------------------------------------+-------+
| Token | Action | Stack |
+-------+------------------------------------------------------------------------------------------+-------+
| 5 | Push to stack | 5 |
| 4 | Push to stack | 4 |
| > | Pop from stack twice (5, 4), evaluate (5 > 4) and push onto stack | TRUE |
| 9 | Push onto stack | 9 |
| 8 | Push onto stack | 8 |
| IF | Pop from stack thrice (TRUE, 9, 8), evaluate (IF TRUE THEN 9 ELSE 8) and push onto stack | 9 |
+-------+------------------------------------------------------------------------------------------+-------+

Excel Combining Multiple Rows

I feel like I am missing something simple I would like to do with Excel but I am asking the question incorrectly on Google...here it goes.
I'm taking a look at some Excel sheets for a friend who runs a race timing company. At the end of a race he has an excel sheet with the following format for a series of races
Name | Gender | Age | Race 1 | Race 2 | Race 3
Bob | M | 20 | 1 | |
Al | M | 24 | 2 | |
Bob | M | 20 | | 2 |
Al | M | 24 | | 1 |
::Assume we don't care about time right now, just place::
I would like to do "something" (again I'm not sure what the proper term is, merge in Excel actually merges two adjecent cells together), where I can get the final output such that
Name | Gender | Age | Race 1 | Race 2 | Race 3
Bob | M | 20 | 1 | 2 |
Al | M | 24 | 2 | 1 |
I'm not sure how to collapse the data for the like rows together.
I'm not opposed to writing a little VBA, but I am thinking this is a built in Excel function but I'm not sure what it is called or how to make it "dance".
Thanks!
PivotTable.
The data format is making life a bit more difficult than it needs to be. Rather than having individual columns for race #1, race #2, race #3 etc, it would make life easier to have a column called "Race Number" and arrange the data like this:
Name | Gender | Age | Race Number | Place
Bob | M | 20 | 1 | 1
Al | M | 24 | 1 | 2
Bob | M | 20 | 2 | 2
Al | M | 24 | 2 | 1
This would make things like PivotTable (as suggested by Jason) a lot easier to work with

Resources