Problem: I want to set a counter variable (a numeric value) in environment variable, so in "Test" we can control the flow.
My experiment:
I wrote a simple API call with following-
Prescript sets the counter variable-
postman.setEnvironmentVariable("mycounter", 1);
Test verifies the counter variable, if its value equals to 1, increment it by one-
if (postman.getEnvironmentVariable("mycounter") == 1 ) {
postman.setEnvironmentVariable("result", "YES");
postman.setEnvironmentVariable("mycounter",
1+postman.getEnvironmentVariable("mycounter"));
}
else {
postman.setEnvironmentVariable("result", "NO");
}
But when i check the value for "mycounter"-
Actual value: 11
Expected value: 2
Can anybody point out how to set numeric value in environment variable?
I got workaround. By using Number function convert string to integer.
So
if (postman.getEnvironmentVariable("mycounter") == 1 ) {
postman.setEnvironmentVariable("result", "YES");
postman.setEnvironmentVariable("mycounter", 1+Number(postman.getEnvironmentVariable("mycounter")));
} else {
postman.setEnvironmentVariable("result", "NO");
}
I think the sam's answer works but here is a cleaner way I use in one of my pre-request scripts
let myCounter = +environment["mycounter"]; // '+' Convert String into Integer
if (myCounter == 1) {
myCounter++;
postman.setEnvironmentVariable("result", "YES");
postman.setEnvironmentVariable("mycounter", myCounter);
} else {
postman.setEnvironmentVariable("result", "NO");
}
More informations about converting ==> https://stackoverflow.com/a/1133814/1646479
With the new Postman API pm:
let myCounter = pm.environment.get("mycounter");
if (myCounter == 1 ) {
pm.environment.set("result", "YES");
pm.environment.set("mycounter", 1+Number(myCounter));
} else {
pm.environment.set("result", "NO");
}
the old postman. API is going to be deprecated and will be removed in future.
Related
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...
I have a doubt with the solution of this question which is stated below -
Given an arbitrary ransom note string and another string containing letters from all the magazines, write a function that will return true if the ransom note can be constructed from the magazines ; otherwise, it will return false.
Each letter in the magazine string can only be used once in your ransom note.
Strings["aa", "ab"] should return false and strings["aa", "aab"] should return true according to question.
Here is the code which I have attempted in the first place and I'm not getting a required output as mentioned above.
unordered_map<char,int>umap;
for(char m:magazine)
{
umap[m]++;
}
for(char r:ransomNote)
{
if(umap.count(r)<=1)
{
return false;
}
else{
umap[r]--;
}
}
return true;
}
In the above code, I have used umap.count(r)<=1 to return false if there is no key present.
For the strings ["aa","aab"], it is returning true but for strings ["aa","ab"], it is also returning true but it should return false.
Then I used another way to solve this problem by using just umap[r]<=0 in the place of umap.count(r)<=1 and it is working just fine and else all code is same.
bool canConstruct(string ransomNote, string magazine) {
unordered_map<char,int>umap;
for(char m:magazine)
{
umap[m]++;
}
for(char r:ransomNote)
{
if(umap[r]<=0)
{
return false;
}
else{
umap[r]--;
}
}
return true;
}
I'm not able to get what i'm missing in the if condition of first code. Can anyone help me to state what I'm doing wrong in first code. Any help is appreciated.
unordered_map::count returns the number of items with specified key.
As you don't use multi_map version, you only have 0 or 1.
Associated value doesn't change presence of key in map.
To use count, you should remove key when value reaches 0:
for (char r : ransomNote) {
if (umap.count(r) == 0) {
return false;
} else {
if (--umap[r] == 0) {
umap.erase(r);
}
}
}
return true;
I a newbie C++ programmer trying to test aruments/parameters passed to a program.
Multiple arguments can be passed to the program, however I want to test that if certain arguments are passed then other arguments become invalid.
e.g. PGM accepts arg(1) arg(2) arg(3) arg(4) arg(5) etc...
if arg(1) and arg(2) are supplied then arg(3), arg(4) and arg(5) etc... are invalid and the program should terminate with an error message if they are also supplied along with arg(1) and arg(2).
I've thought that using boolean IF tests would be a good way to check if certain values are true/false.
I searched on stackoverflow but not found an answer that encompasses exactly what i'm trying to do. If someone can point me in the right direction or suggest a far more efficient way of doing this I would be very grateful.
My code currently looks like this:
bool opt1 = false;
bool opt2 = false;
bool opt3 = false;
bool opt4 = false;
bool opt5 = false;
for(int i=1; i<argc; i++) {
char *str = argv[i];
if (strcmp (str, "-opt1:")==0) {opt1 = true;}
else if (strcmp (str, "-opt2:")==0) {opt2 = true;}
else if (strcmp (str, "-opt3:")==0) {opt3 = true;}
else if (strcmp (str, "-opt4:")==0) {opt4 = true;}
else if (strcmp (str, "-opt5:")==0) {opt5 = true;}
}
if((opt1) && (opt2) && (~(opt3)) && (~(opt4)) && (~(opt5)) {
** DO SOMETHING **
} else {
** DISPLAY ERROR MESSAGE AND USAGE TEXT **
}
A good solution would be using operands ! and &&
! denotes "not" (or in such case "not true") while && combines two different logical comparisons (in such case, "logic test 1" and "logic test 2")
Here's an example to do it:
if((opt1 && opt2)&&(!(opt3||opt4||opt5))){
/*
Do something if opt1 and opt2 are true and others are false
*/
}
This is practically the same as #Fareanor's solution above (first solution)
A possible fix could be (if I have well understood your problem):
if(opt1 && opt2) // opt3, opt4 and opt5 are invalid
{
if(!(opt3 || opt4 || opt5))
{
// Do something
}
else
{
// Display error message because at least opt3 or opt4 or opt5 is provided and not requested
}
}
else // opt3, opt4 and opt5 are valid
{
// Do something
}
But I think it could be better to just ignore the obsolete parameters instead of display an error while you can still run your process with only opt1 and opt2. Which could lead us to the simpler code:
if(opt1 && opt2)
{
// Do something without using opt3, opt4 and opt5
}
else
{
// Do something taking into account opt3, opt4 and opt5
}
I hope it is what you was looking for.
public MyTime nextSecond()
{
if(getSecond()>=0||getSecond()<=58)
return new MyTime(getHour(),getMinute(),getSecond()+1);
else if(getSecond()==59)
return new MyTime(getHour(),getMinute(),0);
else
throw new IllegalArgumentException("Invalid Second!");
}
public MyTime nextMinute()
{
if(getMinute()>=0||getMinute()<=58)
return new MyTime(getHour(),getMinute()+1,0);
else if(getMinute()==59)
return new MyTime(getHour()+1,0,0);
else
throw new IllegalArgumentException("Invalid Minute!");
}
public MyTime nextHour()
{
if(getHour()>=0||getHour()<=22)
return new MyTime(getHour()+1,0,0);
else if(getHour()==23)
return new MyTime(0,0,0);
else
throw new IllegalArgumentException("Invalid Hour!");
}
}
I am a new programmer and this is my code, it doesn't have any errors but if statements are not executing!
Does anyone know why it isn't working?
In if condition if the first statement is correct and the extra conditional is combined with OR then it return true although the second condition is false
it shouldn't be OR statement between conditions it should be AND
You are using logical OR where you should be using logical AND.
For example this:
if (getSecond() >= 0 || getSecond() <= 58)
should be
if (getSecond() >= 0 && getSecond() <= 58)
In your version if the value returned by getSecond() is 59 it will never reach the else if because the first if statement will evaluate getSecond() > 0 to true and because it is logical OR, it will not evaluate the second logical expression in that condition (getSecond() <= 58).
The same goes for minutes and hours.
I am finally getting around to writing stuff in cfscript, and so I am starting with writing some needed formatting functions. Here is an example:
Function FormatBoolean(MyBool, Format) {
Switch(Format){
Case "YES/NO":{
If (MyBool eq 1)
Return "YES";
Else
Return "NO";
Break;
}
Default:{
If (MyBool eq 1)
Return "Yes";
Else
Return "";
Break;
}
}
}
What I would like to do is make Format an optional argument. If you don't include the argument, the function will currently still run, but it won't find format, and it seems that cfparam did not get translated to cfscript.
Will I just have to check if Format is defined and give it a value? Or is there a nicer way of doing this?
Thanks
Personally I prefer to set defaults to this kind of arguments. Also I've refactored function a bit... But not tested :)
function FormatBoolean(required any MyBool, string Format = "") {
switch(arguments.Format) {
case "YES/NO":
return YesNoFormat(arguments.MyBool EQ 1);
default:
return (arguments.MyBool eq 1) ? "Yes" : "";
}
}
Please note that (arguments.MyBool EQ 1) may be replaced with (arguments.MyBool), so it covers all boolean values. You may be interested to make it more reliable, something like this (isValid("boolean", arguments.MyBool) AND arguments.MyBool) -- this should allow to check any value at all.
All variables passed into a function are available to access programmatically via the ARGUMENTS scope. You can refer to it as if it were an array (because it is), as well as standard struct key access (which I've done for you below for the MyBool parameter):
<cfscript>
Function FormatBoolean(MyBool) {
var theFormat = '';
if (ArrayLen(ARGUMENTS) GT 1)
theFormat = ARGUMENTS[2];
Switch(theFormat){
Case "YES/NO":{
If (ARGUMENTS.MyBool eq 1)
Return "YES";
Else
Return "NO";
Break;
}
Default:{
If (ARGUMENTS.MyBool eq 1)
Return "Yes";
Else
Return "";
Break;
}
}
}
</cfscript>
Add your preferred additional levels of data validation as necessary.