How to use OR and AND operators in RPN (Reverse Polish Notation)? - rpn

How do I use OR and AND operators in RPN (Reverse Polish Notation) ?
I have something like this:
"type" valueOf "XY" ==
this means if the type is XY then something happens. Now I want to add another option to "XY", lets say "AB". From what I know it should look something like this
"type" valueOf "XY" "AB" || ==
but of course it isn't. Where is my problem?

What you've written amounts to
valueOf("type") == "XY" || "AB"
which does not express what I suspect you mean in any notation. What you meant was probably
valueOf("type") == "XY" || valueOf("type") == "AB"
so try to translate that to RPN.

Related

C++; While Loop Unable to Exit; Sentinel does not seem to be recognized. Nested Loops with Switch statement [duplicate]

I have a variable v in my program, and it may take any value from the set of values
"a", "b", "c", ..., "z"
And my goal is to execute some statement only when v is not "x", "y", or "z".
I have tried,
for C-like languages (where equality operators compare the actual string values; e.g. c#, javascript, php)
if (v != "x" || v != "y" || v != "z")
{
// the statements I want to be executed
// if v is neither "x", nor "y", nor "z"
}
for Pascal-like languages (e.g. plsql)
IF (v != 'x' OR v != 'y' OR v != 'z') THEN
-- the statements I want to be executed
-- if v is neither "x", nor "y", nor "z"
END IF;
The statements inside the if condition always get executed. Am I doing anything wrong?
Use &&/AND/and, not ||/OR/or:
v != "x" && v != "y" && v != "z"
Problem
If an if block is always executed, the condition for the if block always evaluates to true. The logical expression must be wrong.
Let us consider v != "x" || v != "y" || v != "z" for each value of v.
When v = "x",
v != "x" becomes "x" != "x", which is false.
v != "y" becomes "x" != "y", which is true.
v != "z" becomes "x" != "z", which is true.
The expression evaluates to false || true || true, which is true.
When v = "y", the expression becomes
"y" != "x" || "y" != "y" || "y" != "z"
or true || false || true, which is true.
When v = "z", the expression becomes
"z" != "x" || "z" != "y" || "z" != "z"
or true || true || false, which is true.
For any other value for v, the expression evaluates to true || true || true, which is true.
Alternatively, consider the truth-table:
│ A B C │
v │ v != "x" v != "y" v != "z" │ A || B || C
───────┼──────────────────────────────────┼──────────────
"x" │ false true true │ true
"y" │ true false true │ true
"z" │ true true false │ true
other │ true true true │ true
As you can see, your logical expression always evaluates to true.
Solution
What you want to do is, find a logical expression that evaluates to true when
(v is not "x")and(v is not "y")and(v is not "z").
The correct construction is,
for C-like languages (eg. c#, javascript-(may need the strict equality operator !==), php)
if (v != "x" && v != "y" && v != "z")
{
// the statements I want to be executed
// if v is neither "x", nor "y", nor "z"
}
for Pascal-like languages plsql
IF (v != 'x' AND v != 'y' AND v != 'z') THEN
-- the statements I want to be executed
-- if v is neither "x", nor "y", nor "z"
END IF;
De Morgan's law
By De Morgan's law, the expression can also be rewritten as (using C-like syntax)
!(v == "x" || v == "y" || v == "z")
meaning
not((v is "x")or(v is "y")or(v is "z")).
This makes the logic a bit more obvious.
Specific languages
Some languages have specific constructs for testing membership in sets, or you can use array/list operations.
sql: v NOT IN ('x', 'y', 'z')
javascript: ["x", "y", "z"].indexOf(v) == -1
python: v not in {"x", "y", "z"}
java: !Arrays.asList("x", "y", "z").contains(v)
java-9 (and above): !Set.of("x", "y", "z").contains(v)
I figured I'd contribute an answer for Bourne shell script, since the syntax is somewhat peculiar.
In traditional/POSIX sh the string equality test is a feature of the [ command (yes, that is a distinct command name!) which has some pesky requirements on quoting etc.
#### WRONG
if [ "$v" != 'x' ] || [ "$v" != 'y'] || [ "$v" != 'z' ]; then
: some code which should happen when $v is not 'x' or 'y' or 'z'
fi
Modern shells like Ksh, Bash, Zsh etc also have [[ which is somewhat less pesky.
#### STILL WRONG
if [[ $v != 'x' || $v != 'y' || $v != 'z' ]]; then
: some code which should happen when $v is not 'x' or 'y' or 'z'
fi
We should highlight the requirement to have spaces around each token, which is something many beginners overlook (i.e. you can't say if[[$v or $v!='y' without whitespace around the commands and operators), and the apparent optionality of quoting. Failing to quote a value is often not a syntax error, but it will lead to grave undesired semantical troubles if you fail to quote a value which needs to be quoted. (More on this elsewhere.)
The obvious fix here is to use && instead of || but you should also note that [[ typically sports support for regular expressions, so you can say something like
if [[ ! $v =~ ^(x|y|z)$ ]]; then
: yeah
fi
and don't forget the trusty old case statement which is quite natural for this, and portable back into the late 1970s:
case $v in
x | y | z)
;; # don't actually do anything in this switch
*) # anything else, we fall through to this switch
yeah
some more yeah
in fact, lots of yeah;;
esac
The trailing double semicolons cause aneurysms at first, but you quickly recover, and learn to appreciate, even love them. POSIX lets you put an opening parenthesis before the match expression so you don't have unpaired right parentheses, but this usage is rather uncommon.
(This is obviously not a suitable answer for Unix shells which are not from the Bourne family. The C family of shells -- including the still somewhat popular tcsh -- use a syntax which is supposedly "C-like" but that's like being unable to tell apart Alice Cooper from the girl who went to Wonderland; and the Fish shell has its own peculiarities which I'm not even competent to comment on.)
You could use something like this, for PHP:
if(strpos('xyz',$v[0])===false)//example 1
//strpos returns false when the letter isn't in the string
//returns the position (0 based) of the substring
//we must use a strict comparison to see if it isn't in the substring
if(!in_array($v[0],array('x','y','z')))//example 2
//example 3
$out=array('x'=>1,'y'=>1,'z'=>1); //create an array
if(!$out[$v[0]]) //check if it's not 1
if(!preg_match('/^[xyz]$/',$v))//example 4, using regex
if(str_replace(array('x','y','z'),'',$v[0]))//example 5
if(trim($v[0],'xyz'))//example 6
For Javascript:
if(~'xyz'.search(v[0]))//example 1(.indexOf() works too)
if(!(v[0] in {x:0,y:0,z:0}))//example 2
if(~['x','y','z'].indexOf(v[0]))//example 3, incompatible with older browsers.
if(!/^[xyz]$/.match(v))//example 4
if(v.replace(/^[xyz]$/))//example 5
For MySQL:
Select not locate(#v,'xyz'); -- example 1
select #v not in ('x','y','z'); -- example 2
-- repetition of the same pattern for the others
For C:
if(!strstr("xyz",v))//example 1, untested
There are more ways, I'm just too lazy.
Use your imagination and just write the one that you like more!

How to code a test a statement with three or more components

Excuse me for the use of the term "component", there probably is a better term to use in such context.
But moving on to my question, I want to use the else statement to execute a statement block based on the truth of the test statement. Here is the test statement I tried using:
else:
if ((reply != a) && (reply != c) && (reply =! e)):
I'm getting a syntax error, and the carrot is pointing at the first set of ampersands. I'm assuming now that I might be improperly using '&&'.
With this statement, my goal is to execute the statement block only if the test statement is true, meaning further, that 'reply' must not be equal to a, c, or e.
I know that I can use nested if's under the else statement, but I'm hoping StackExchange knows a better way. Thank you.
parenthesis check :
if (((reply != a) && (reply != c)) && (reply =! e))

Why does non-equality check of one variable against many values always return true?

I have a variable v in my program, and it may take any value from the set of values
"a", "b", "c", ..., "z"
And my goal is to execute some statement only when v is not "x", "y", or "z".
I have tried,
for C-like languages (where equality operators compare the actual string values; e.g. c#, javascript, php)
if (v != "x" || v != "y" || v != "z")
{
// the statements I want to be executed
// if v is neither "x", nor "y", nor "z"
}
for Pascal-like languages (e.g. plsql)
IF (v != 'x' OR v != 'y' OR v != 'z') THEN
-- the statements I want to be executed
-- if v is neither "x", nor "y", nor "z"
END IF;
The statements inside the if condition always get executed. Am I doing anything wrong?
Use &&/AND/and, not ||/OR/or:
v != "x" && v != "y" && v != "z"
Problem
If an if block is always executed, the condition for the if block always evaluates to true. The logical expression must be wrong.
Let us consider v != "x" || v != "y" || v != "z" for each value of v.
When v = "x",
v != "x" becomes "x" != "x", which is false.
v != "y" becomes "x" != "y", which is true.
v != "z" becomes "x" != "z", which is true.
The expression evaluates to false || true || true, which is true.
When v = "y", the expression becomes
"y" != "x" || "y" != "y" || "y" != "z"
or true || false || true, which is true.
When v = "z", the expression becomes
"z" != "x" || "z" != "y" || "z" != "z"
or true || true || false, which is true.
For any other value for v, the expression evaluates to true || true || true, which is true.
Alternatively, consider the truth-table:
│ A B C │
v │ v != "x" v != "y" v != "z" │ A || B || C
───────┼──────────────────────────────────┼──────────────
"x" │ false true true │ true
"y" │ true false true │ true
"z" │ true true false │ true
other │ true true true │ true
As you can see, your logical expression always evaluates to true.
Solution
What you want to do is, find a logical expression that evaluates to true when
(v is not "x")and(v is not "y")and(v is not "z").
The correct construction is,
for C-like languages (eg. c#, javascript-(may need the strict equality operator !==), php)
if (v != "x" && v != "y" && v != "z")
{
// the statements I want to be executed
// if v is neither "x", nor "y", nor "z"
}
for Pascal-like languages plsql
IF (v != 'x' AND v != 'y' AND v != 'z') THEN
-- the statements I want to be executed
-- if v is neither "x", nor "y", nor "z"
END IF;
De Morgan's law
By De Morgan's law, the expression can also be rewritten as (using C-like syntax)
!(v == "x" || v == "y" || v == "z")
meaning
not((v is "x")or(v is "y")or(v is "z")).
This makes the logic a bit more obvious.
Specific languages
Some languages have specific constructs for testing membership in sets, or you can use array/list operations.
sql: v NOT IN ('x', 'y', 'z')
javascript: ["x", "y", "z"].indexOf(v) == -1
python: v not in {"x", "y", "z"}
java: !Arrays.asList("x", "y", "z").contains(v)
java-9 (and above): !Set.of("x", "y", "z").contains(v)
I figured I'd contribute an answer for Bourne shell script, since the syntax is somewhat peculiar.
In traditional/POSIX sh the string equality test is a feature of the [ command (yes, that is a distinct command name!) which has some pesky requirements on quoting etc.
#### WRONG
if [ "$v" != 'x' ] || [ "$v" != 'y'] || [ "$v" != 'z' ]; then
: some code which should happen when $v is not 'x' or 'y' or 'z'
fi
Modern shells like Ksh, Bash, Zsh etc also have [[ which is somewhat less pesky.
#### STILL WRONG
if [[ $v != 'x' || $v != 'y' || $v != 'z' ]]; then
: some code which should happen when $v is not 'x' or 'y' or 'z'
fi
We should highlight the requirement to have spaces around each token, which is something many beginners overlook (i.e. you can't say if[[$v or $v!='y' without whitespace around the commands and operators), and the apparent optionality of quoting. Failing to quote a value is often not a syntax error, but it will lead to grave undesired semantical troubles if you fail to quote a value which needs to be quoted. (More on this elsewhere.)
The obvious fix here is to use && instead of || but you should also note that [[ typically sports support for regular expressions, so you can say something like
if [[ ! $v =~ ^(x|y|z)$ ]]; then
: yeah
fi
and don't forget the trusty old case statement which is quite natural for this, and portable back into the late 1970s:
case $v in
x | y | z)
;; # don't actually do anything in this switch
*) # anything else, we fall through to this switch
yeah
some more yeah
in fact, lots of yeah;;
esac
The trailing double semicolons cause aneurysms at first, but you quickly recover, and learn to appreciate, even love them. POSIX lets you put an opening parenthesis before the match expression so you don't have unpaired right parentheses, but this usage is rather uncommon.
(This is obviously not a suitable answer for Unix shells which are not from the Bourne family. The C family of shells -- including the still somewhat popular tcsh -- use a syntax which is supposedly "C-like" but that's like being unable to tell apart Alice Cooper from the girl who went to Wonderland; and the Fish shell has its own peculiarities which I'm not even competent to comment on.)
You could use something like this, for PHP:
if(strpos('xyz',$v[0])===false)//example 1
//strpos returns false when the letter isn't in the string
//returns the position (0 based) of the substring
//we must use a strict comparison to see if it isn't in the substring
if(!in_array($v[0],array('x','y','z')))//example 2
//example 3
$out=array('x'=>1,'y'=>1,'z'=>1); //create an array
if(!$out[$v[0]]) //check if it's not 1
if(!preg_match('/^[xyz]$/',$v))//example 4, using regex
if(str_replace(array('x','y','z'),'',$v[0]))//example 5
if(trim($v[0],'xyz'))//example 6
For Javascript:
if(~'xyz'.search(v[0]))//example 1(.indexOf() works too)
if(!(v[0] in {x:0,y:0,z:0}))//example 2
if(~['x','y','z'].indexOf(v[0]))//example 3, incompatible with older browsers.
if(!/^[xyz]$/.match(v))//example 4
if(v.replace(/^[xyz]$/))//example 5
For MySQL:
Select not locate(#v,'xyz'); -- example 1
select #v not in ('x','y','z'); -- example 2
-- repetition of the same pattern for the others
For C:
if(!strstr("xyz",v))//example 1, untested
There are more ways, I'm just too lazy.
Use your imagination and just write the one that you like more!

Vector comparison with string

I have a string vector of user-input data containing strings. Now I need to make sure program won't execute if strings are different than specified few. Vector contains 4 fields and every has different condition:
vector[0] can only be "1" or "0"
vector[1] can only be "red" or "green
vector[2] can only be "1", "2" or "3"
vector[3] can only be "1" or "0"
I tried writing if for every condition:
if(tokens[0]!="1" || tokens[0]!="0"){
decy = "error";
}
else if(tokens[1]!="red" || tokens[1]!="green"){
decy = "error";
}
else if(tokens[2]!="1" || tokens[2]!="2" || tokens[2]!="3"){
decy = "error";
}
else if(tokens[3]!="1" || tokens[3]!="0"){
decy = "error";
}
else{
switch(){} //working code
}
return decy;
It always enters first if and returns error. I tried with if instead of else if but it doesn't work either. I checked vector[i] contents and it returns correct strings. No " " at the end of it etc. Removing else and releasing switch just makes program check first condition and ignore rest of it.
I'm probably doing something terribly wrong, but I can't find an answer on internet so I decided to ask here.
This line:
if(tokens[0]!="1" || tokens[0]!="0")
should be:
if(tokens[0]!="1" && tokens[0]!="0")
^^
The same goes for the rest of the if statements as well.
The conditions are invalid.
Any distinct value can satisfy your conditions.
You should use && instead of ||.
For example:
if (tokens[0] != "1" || tokens[0] != "0") {
Consider this line. If tokens[0] is "1", which is valid input, it will not satisfy the first condition, but it will satisfy the second. You only want to throw an error when the value is neither of the valid possible inputs.
This means that your condition should be:
if (tokens[0] != "1" && tokens[0] != "0") {
Same goes for all the others.
You should turn those || into &&. If the input can only be X or Y, this means that it is illegal when it is not X and not Y:
if (tokens[0] != "1" && tokens [0] !="0")
// ^^
The first if:
if(tokens[0]!="1" || tokens[0]!="0")
ALWAYS evaluates to true.

CoffeeScript String Comparison

In CoffeeScript, is there a way to simplify the following:
if(value === "something" || value === "else" || value === "wow"){}
I've tried:
if value is "something" or "else" or "wow"
But this produces the literal output of this:
if(value === "something" || "else" || "wow){}
Is there a way to check if a string is one of multiple values (OR or AND) in CoffeeScript?
I think you probably want
if value in ['something', 'else', 'wow']