I have to check whether a variable is less than a constant value in Cypress. How can we achieve this?
e.g: check whether a given variable is less than 30.
I'm making a few assumptions here, but, something like the following should work.
// define constant
const maxValue = 30;
cy.get('foo') // get element with variable text
.then(($el) => { // use the yielded element in a .then()
// get the text of the element using JQuery's `.text()`
// and compare the value using Chai's `lessThan`.
expect($el.text()).to.be.lessThan(maxValue);
});
References for inline comments:
JQuery's text()
Chai's lessThan (alias for below)
I have a map with some values. Then I have a function that returns some string, and this string will be a member of the keys in map. I need to retrieve the value based on the key and pass it to another function, which takes it as argument.
map<string,int> SymbolTable;
SymbolTable["R0"]=0;
SymbolTable["R1"]=1;
SymbolTable["R2"]=2;
SymbolTable["R3"]=3;
string value=getValue(); //this one will return something from R0 to R3
nextFunction(SymbolTable[value]); // this part is wrong
If I give value=="R0" or some static value, this is working as expected. But whenever I pass this dynamic value, it returns 0 always, so my nextFunction is taking 0 as argument.
I tried to output the return value from getValue() to check what it is returning, and it is correct. I have tried this and similar ways, but all gives me the same issue. Can someone guide me on what am I doing wrong here? TIY
If I give value=="R0" or some static value, this is working as
expected. But whenever I pass this dynamic value, it returns 0 always
It simply means the "dynamic value" you obtained does not exist as a key in the map. std::map's operator [] inserts a default constructed value if the associated key does not exist.
To check for the existence of value in your map, you can do:
string value=getValue();
if(SymbolTable.count(value)){
//key exists....
nextFunction(SymbolTable[value]); // this part should be correct now
}
or you can equally use std::map::find
Just wondering, but assuming you had maybe 10 different values to compare to X, would it be better to write one giant long if condition, or a for loop with an if-statement that returns true if x == value, and then false if the for loop finishes without returning true?
The long if statement is going to be hard to read for humans and space consuming. It will be slightly faster however than the for loop, but that should't bother you.
A better way would be to make the for loop in a separate function that returns boolean value or storing the comparison values in an array and checking something like:
var array = [1,2,3,4,5]
if(array.contains(x))
{
// x is one of the values
}
else
{
// x is not in the values
}
Which programming language you are using? For Php Lets try this by using in_array() function of php.
$arr_value = array("value1", "value2", "value3", "value4"); //set matching values
if(in_array($original_value, $arr_value))
{
//true block
}
else
{
//false block
}
Giant long if condition is not advisable because you may encounter a conflict on it. Instead use if else condition to solve your problem.
I have
const boost::property_tree::ptree& v
and I want to get <xmlattr>.Value, if it exists, otherwise the value.
I tried this code:
if(v.find("<xmlattr>.Value") != v.not_found())
value = v.get<std::string>("<xmlattr>.Value");
else
value = v.get_value<std::string>();
However, it doesn't work as expected. Even if the value is there, find() returns not_found().
This code works:
auto inValue = v.get_optional<std::string>("<xmlattr>.Value");
if(inValue.is_initialized())
value = inValue.get();
else
value = v.get_value<std::string>();
I guess I understood find() wrong. What exactly does it do? Is there another function I should use instead?
According to the documentation, find() (see here) finds a child with the given key (not path), or not_found() if there is none.
<xmlattr>.Value is a path (that works with get and get_optional).
I need to understand how this recursion work, I understand simple recursion examples but more advanced ones is hard. Even thought there are just two lines of code I got problem with... the return statement itself. I just draw a blank on how this works, especially the and/or operator. Any insight is very welcome.
bool subsetSumExists(Set<int> & set, int target) {
if (set.isEmpty()) {
return target == 0;
} else {
int element = set.first();
Set<int> rest = set - element;
return subsetSumExists(rest, target)
|| subsetSumExists(rest, target - element);
}
}
Recursive code is normally coupled with the concept of reduction. In general, reduction is a means to reduce an unknown problem to a known one via some transformation.
Let's take a look at your code. You need to find whether a given target sum can be constructed from an elements of the input data set.
If the data set is empty, there is nothing to do besides comparing the target sum to 0.
Otherwise, let's apply the reduction. If we choose a number from the set, there can actually be 2 possibilities - the chosen number participates in the sum you're seeking or it doesn't. No other possibilities here (it's very important to cover the full spectrum of possibilities!). In fact, it doesn't really matter which data element is chosen as long as you can cover all the possibilities for the remaining data.
First case: the number doesn't participate in the sum. We can reduce the problem to a smaller one, with data set without the inspected element and the same target sum.
Second case: the number participates in the sum. We can reduce the problem to a smaller one, with data set without the inspected element and the requested sum decreased by the value of the number.
Note, you don't know at this point whether any of these cases is true. You just continue reducing them until you get to the trivial empty case where you can know for sure the answer.
The answer to the original question would be true if it's true for any of these 2 cases. That's exactly what operator || does - it will yield true if any of its operands (the outcome of the 2 cases) are true.
|| is logical OR. It's evaluated left-to-right and short-circuited.
This means that in an expression A || B, A is evaluated first. If it's true, the entire expression is true and no further evaluation is done. If A is false, B is evaluated and the expression gets the value of B.
In your example, A is "try getting the same sum without using the 1st element from the set". B is "use the 1st element from the set, which decreases the total left to sum, and try to get that with the rest of the element."
Lets first look at algorithm..
The base case(i.e the case in which recursion terminates) is when the set is empty.
Otherwise the program takes the first elements subtracts it from the set.
Now it will call subsetSumExists(rest, target) and check if its true,
if it is it will return true otherwise it will call
subsetSumExists(rest, target - element) and return whatever it
returns.
In simple terms, it will this call subsetSumExists(rest, target - element) only if first one subsetSumExists(rest, target) returns false.
Now lets try to dry run this code with a small sample set of {3,5} and a sum of 8. I'll call the function sSE from now on
sSE({3,5}, 8) => "sSE({5}, 8) || sSE({5},(8-3))"
sSE({5}, 8) => sSE({}, 8) || sSE({}, (8-5))
sSE({}, 8) => false.. now will call sSE({}, (8-5))
sSE({}, 3) => false.. now will call sSE({5}, (8-3))
sSE({5}, 5) => sSE({}, 5} || sSE({}, (5-5))
sSE({}, 5) => false.. now will call sSE({}, (5-5))
sSE({}, 0) => true.. ends here and return true
To understand recursion, you need to understrand recursion.
To do that, you need to think recusively.
In this particular case.
For any: subsetSum(set, target)
If set is empty AND target is 0, then subsetSum exists
Otherwise, remove first element of the set. check if subdetSum(set, target) exists OR subdetSum(set, target - removed_element) exists (using step 0)
The set subtraction looks a strange syntax but I will assume it means pop() on the element.
It "works" through finding every possible combination although it is exponential.
In the || statement, the LHS is the sum including the current element and the RHS is the sum excluding it. So you will get, down the exponential tree, every combination of each element either switched on or off.
Exponential, by the way, means that if you have 30 elements it will produce 2 to the power of 30, i.e. 0x40000000 or close to a billion combinations.
Of course you may well run out of memory.
If it finds the solution it might not run through all 2^N cases. If there is no solution it will always visit them all.
If I speak for myself, difficulty in understanding of the problem stems from || operator. Let's glance at bottom return statement of same code with another way,
if (subsetSumExists(rest, target - element))
return true;
if (subsetSumExists(rest, target))
return true;
return false;