I need help condensing this IF statement [closed] - if-statement

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 9 years ago.
Improve this question
I would like to come up with more elegant code. I have this IF statement:
if (( !mainFlag and
form.opt1 == store.opt1 and form.opt2 == store.opt2 and
form.opt3 == store.opt3 and form.opt4 == store.opt4 ) or
( mainFlag and
form.opt1 == store.opt1 and form.opt2 == store.opt2 and
form.opt3 == store.opt3 and form.opt4 == store.opt4 and
form.opt5 == store.opt5 and form.opt6 == store.opt6 )){
dataHasBeenUpdated = False;
} else {
dataHasBeenUpdated = True;
}
Basically I have a number of fields that always need to checked to see if they have been updated (opt1 - 4), but when mainFlag = true I need to also check two other fields.
It kind of bothers me that I have the same checks two times for the first four opt# fields. What would be a more elegant/lest code way of writing this IF statement?

Let be:
A = mainFlag
B = form.opt1 == store.opt1 and form.opt2 == store.opt2 and
form.opt3 == store.opt3 and form.opt4 == store.opt4
C = form.opt5 == store.opt5 and form.opt6 == store.opt6
Where A,B,C are boolean variables. Then, your IF statement has the following boolean expression into it: !A*B + A*B*C
Once simplified, this expression leads us to:
!A*B + A*B*C = B*(!A+A*C) = B*((!A+A)*(!A+C)) = B*(!A+C)
(!,*,+ are boolean operators for NOT,AND and OR respectively)
Translating this back to your IF statement:
if (
( form.opt1 == store.opt1 and form.opt2 == store.opt2 and /* */
form.opt3 == store.opt3 and form.opt4 == store.opt4 /* B */
)
and
( /* !A */
!mainFlag or (
form.opt5 == store.opt5 and form.opt6 == store.opt6 /* C */
)
)
)
dataHasBeenUpdated = False;
else
dataHasBeenUpdated = True;

This should simplify it enough I think...
dataHasBeenUpdated = ! (form.opt1 == store.opt1
and form.opt2 == store.opt2
and form.opt3 == store.opt3
and form.opt4 == store.opt4
and ( !mainFlag
or ( mainFlag
and form.opt3 == store.opt3
and form.opt4 == store.opt4 ) ));

Related

Hello everyone, please help me check this IF statement in Google app script

I want to make a code to assign logic input for my sheet. I use IF to make it. My code ran successfully but the logic didn't work. I have checked it many times, but I couldn't find something wrong. Can you help me with this? I'm stuck. Please review my example sheet and my script for more information. Thank you! https://docs.google.com/spreadsheets/d/1eV2SZ45Gs6jISgh_p6RIx-rfOGlHUM6vF114Mgf6c58/edit#gid=0
function logic(){
var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var activeCell = ss.getActiveCell();
if (activeCell.getColumn() == 1 && activeCell.getRow() > 1 && ss.getSheetName() == "mama" && activeCell.getValue() == "Yes") {
activeCell.offset(0,1).clearContent();
activeCell.offset(0,1).setValue("1");
} if (activeCell.getColumn() == 1 && activeCell.getRow() > 1 && ss.getSheetName() == "mama" && activeCell.getValue() == "Hafl") {
activeCell.offset(0,1).clearContent();
activeCell.offset(0,1).setValue("1/2");
} if (activeCell.getColumn() == 1 && activeCell.getRow() > 1 && ss.getSheetName() == "mama" && activeCell.getValue() == "No") {
activeCell.offset(0,1).clearContent();
activeCell.offset(0,1).setValue(0);
}
}
You can simplify your code this way.
(Note that I use the const variable declaration instead of var (ES6 - V8 engine))
function logic() {
const ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
const activeCell = ss.getActiveCell();
const activeCellValue = activeCell.getValue();
if (activeCell.getColumn() === 1 && activeCell.getRow() > 1 && ss.getSheetName() == "mama") {
switch(activeCellValue) {
case 'Yes':
activeCell.offset(0, 1).clearContent();
activeCell.offset(0, 1).setValue('1');
break;
case 'Half':
activeCell.offset(0, 1).clearContent();
activeCell.offset(0, 1).setValue('1/2');
break;
case 'No':
activeCell.offset(0, 1).clearContent();
activeCell.offset(0, 1).setValue('0');
break;
}
}
}
This way you only have to test the common conditions once.
Using the Switch function clearly shows the behavior of the script depending on the input value 'ActiveCellValue'.
If you need that only one action resolve per run, you need to use else if to chain the statements:
if(statement){
Action
}else if (statement2){
Action2
}else if...

Issue with function to check if tree is a BST

So I have a function to check if a tree is a bst(if every node only has smaller values on its left and larger values on its right). Every other function works and the problem is with this one (second one just calls helper). I think the issue has something to do with the recursive call root-left hitting null but I am not sure and even if it is not sure how to fix. Can add more code as needed. Any help is appreciated.
visual studio error i get is : R00t -> left was nullptr
other compiler: segmentation fault core dumped.
bool isBSThelper(TreeNode* R00t)
{
if (R00t == NULL)
return true;
//if (R00t->left != NULL && (R00t->info < R00t->left->info))
if (R00t->info < R00t->left->info)
return false;
//if (R00t->right != NULL && (R00t->info < R00t->right->info))
if (R00t->info > R00t->right->info)
return false;
return isBSThelper(R00t->left) && isBSThelper(R00t->right);
}
bool TreeType::isBST() const
{
return isBSThelper(root);
}
According to your comment
even with if (R00t->left != NULL || R00t->right != NULL) error persists
the problem would persist. Let me rewrite and iterate from there (since I can't comment to ask for clarification -new user). If your code was like
bool isBSThelper(TreeNode* R00t) {
if ( R00t == NULL )
return true;
if ( R00t->left != NULL || R00t->right != NULL ) {
if ( R00t->info < R00t->left->info )
return false;
if ( R00t->info < R00t->right->info )
return false;
}
return isBSThelper(R00t->left) && isBSThelper(R00t->right);
}
then you would potentially still encounter the same problem, since the expression
if (R00t->left != NULL || R00t->right != NULL) {
would still make this expression with values
R00t->left != NULL
but
R00t->right == NULL
evaluate to true.
One solution might be
To make sure R00T->left and R00t->right are either valid (pointing to a node) or NULL (preferably nullptr if you are using C++)
And code like this:
bool isBSThelper( TreeNode* R00t ) {
if ( R00t == NULL )
return true;
if ( R00t->left != NULL && R00t->info > R00t->left->info )
return false;
if ( R00t->right!= NULL && R00t->info > R00t->right->info )
return false;
return isBSThelper( R00t->left ) && isBSThelper( R00t->right );
}
And the problem would be no more. (1) is critical.
An additional note: Your code does not check
if (R00t->left != NULL && R00t->right!= NULL && R00t->left->info < R00t->right->info)
return false;
which is another property of a Binary Search Tree (or obviously using ">" instead of "<" here as you see fit).

what is the opposite of this logical statement

I would like to check for the else condition only. Is there a way that I can simplify the code below?
if (_endCurrentGoal && _startCurrentGoal && _startCurrentGoal === _startEffectiveDate && _endCurrentGoal === _endEffectiveDate) { //no statement );
}
else {
console.log("check for this code only");
}
Is below code correct? is there a way to simplify it?
if(!(_endCurrentGoal && _startCurrentGoal && _startCurrentGoal === _startEffectiveDate && _endCurrentGoal === _endEffectiveDate) {
console.log("check for this code only");
}
You can use De Morgan's Law to distribute the ! through the &&s.
That will look like this:
if(!_endCurrentGoal || !_startCurrentGoal || _startCurrentGoal !== _startEffectiveDate || _endCurrentGoal !== _endEffectiveDate) {
console.log("check for this code only");
}

Change method from return to void

I am asked to make a maze game in c++ (using codeblocks). I figured out most of it, but stuck in one method of Maze class. I have this function to say that travel in anyone direction (up, down, left, right) where you dont get the wall.
int Maze::mazeTraversal(int a, int b)
{
// If a,b is outside maze, return false.
if ( a < 0 || a > MCOLS - 1 || b < 0 || b > NROWS - 1 ) return FALSE;
// If a,b is the goal, return true.
if ( maze[b][a] == 'G' ) return TRUE;
// If a,b is not open, return false.
if ( maze[b][a] != '0' && maze[b][a] != 'S' ) return FALSE;
// Mark a,b part of solution path.
maze[b][a] = 'x';
// If find_path North of a,b is true, return true.
if ( mazeTraversal(a, b - 1) == TRUE ) return TRUE;
// If find_path East of a,b is true, return true.
if ( mazeTraversal(a + 1, b) == TRUE ) return TRUE;
// If find_path South of a,b is true, return true.
if ( mazeTraversal(a, b + 1) == TRUE ) return TRUE;
// If find_path West of a,b is true, return true.
if ( mazeTraversal(a - 1, b) == TRUE ) return TRUE;
// Unmark a,b as part of solution path.
maze[b][a] = '0';
return FALSE;
}
I am calling this function as:
Maze mo(maze,12); //creating maze game with 12/12 array
mo. mazeTraversal(0,2) // because the entry point is in 0,2 position of the game.
I just realised that I am asked to have this mazeTraversal as void. No any return. My mind is blowing up. Excepting some creative ideas please.
Use:
void Maze::mazeTraversal(int a, int b, bool& Status)
and then instead of return inside function use :-
Status = false; // or true
or use a data member bool Status inside class and update its value
class Maze{
public :
bool Status;
//..
void Maze::mazeTraversal(int a, int b);
//...
};
If your request states that the signature of the function must not be modified, the signature does not include the return type, therefore, you can change the return type.
If the assignment does not tell anything about the signature there are the solutions already posted upstream:
add a by reference param (not a pointer, that is cumbersome and really not needed)
add an internal state that you maintain and return in some "Status" function.
These are the solution I would investigate.
Either way you'd have to adapt the implementation of the traverse function.
As it seems that your Maze object is stateful (i.e.: does maintain some internal state as to what position it arrived to), the second makes more sense.
Here is another solution that does not change your function signature yet it does not return anything at all:
bool g_Status = true;
void Maze::mazeTraversal(int a, int b)
{
if ( a < 0 || a > MCOLS - 1 || b < 0 || b > NROWS - 1 ) // If a,b is outside maze, return false.
g_Status=false;
else if ( maze[b][a] == 'G' ) // If a,b is the goal, return true.
g_Status=true;
else if ( maze[b][a] != '0' && maze[b][a] != 'S' ) // If a,b is not open, return false.
g_Status=false;
else{
maze[b][a] = 'x'; // Mark a,b part of solution path.
mazeTraversal(a, b - 1);
if (!g_Status) // If find_path North of a,b is true, return true.
mazeTraversal(a + 1, b); // If find_path East of a,b is true, return true.
if (!g_Status)
mazeTraversal(a, b + 1); // If find_path South of a,b is true, return true.
if (!g_Status)
mazeTraversal(a - 1, b); // If find_path West of a,b is true, return true.
if (!g_Status)
maze[b][a] = '0'; // Unmark a,b as part of solution path.
}
}
You may write your code like this:
void Maze::mazeTraversal(int a, int b,bool* retValue)
{
// If a,b is outside maze, *retValue = false.
if ( a < 0 || a > MCOLS - 1 || b < 0 || b > NROWS - 1 ) {
*retValue= FALSE;
return;
}
// If a,b is the goal, return true.
if ( maze[b][a] == 'G' ) {
*retValue= TRUE;
return;
}
// If a,b is not open, return false.
if ( maze[b][a] != '0' && maze[b][a] != 'S' ) {
*retValue= FALSE;
return;
}
// Mark a,b part of solution path.
maze[b][a] = 'x';
// If find_path North of a,b is true, return true.
bool* ret1;
mazeTraversal(a + 1, b,ret1)
if ( *ret1 == TRUE ) {
*retValue= TRUE;
return;
}
// If find_path East of a,b is true, return true.
bool* ret2;
mazeTraversal(a + 1, b,ret2);
if ( *ret2 == TRUE ) {
*retValue= TRUE;
return;
}
// If find_path South of a,b is true, return true.
bool* ret3;
mazeTraversal(a, b + 1,ret3);
if ( *ret3 == TRUE ) {
*retValue= TRUE;
return;
}
// If find_path West of a,b is true, *retValue= true.
if ( mazeTraversal(a - 1, b) == TRUE ) {
*retValue= TRUE;
return;
}
// Unmark a,b as part of solution path.
maze[b][a] = '0';
*retValue= FALSE;
}
Now create a global variable:
bool* retvalue;
This variable will always hold the return value, you dont need to return from function

Hook filtering Keyboards HKEYS issue in c

I am filtering the HKEYS by using Hook filtering function, I use the following code to disable Alt+Tab, Alt+Esc, Ctrl+Esc, Windows Key
if (((lParam.vkCode == 9) && (lParam.flags == 32))||
((lParam.vkCode == 27) && (lParam.flags == 32))||
((lParam.vkCode == 27) && (lParam.flags == 0)) ||
((lParam.vkCode == 91) && (lParam.flags == 1)) ||
((lParam.vkCode == 92) && (lParam.flags == 1)) ||
((true) && (lParam.flags == 32)))
return 1;
Can any one tell me how can I disable Ctrl+Alt+Del? I have tried a lot to filter this but unsuccessful for me.
I have got answer of my own question, ctrl+alt+del can't filter out from hook, the only way to disable ctrl+alt+del is to modify the registry form your code..