So I have seen something like the code below in a React-Redux application
return StartTime <= currentTime && currentTime <= EndTime;
What exactly does this line returns to? What does the <= mean?
This is "less than or equal to" operator.
Example:
return 3 <= 3 && 4 <= 5 //true&&true - true
return 3 <= 2 && 4 <= 5 //false&&true - false
It's the less than or equal to operator.
1 <= 2 // true
1 <= 1 // true
1 <= 0 // false
Related
On codecademy there exists a course on C, which includes a project on how to make a calendar. This project includes a boolean function which decides if a given year is a leap year or not. Code:
bool is_leap_year(int year) {
return (year % 4 == 0 && (year % 100 || year % 400 == 0));
}
Given my beginner understanding of operators and return statements, my reading of this code would be: "A given year will be a leap year if it is divisible by 4 AND 100 OR 400."
But this would mean that 1992 wouldn't be a leap year, and 1900 would be, which is plainly wrong.
How come then, that when I run the code and input these years, it does return a correct answer?
You appear to think
x || y == 0
means
x == 0 || y == 0
But it doesn't.
x || y == 0 doesn't mean "x or y is equal to zero".
x || y == 0 means "x, or y is equal to zero".
Put more clearly,
x || y == 0 means "(x) is true or (y is equal to zero) is true".
Since true simply means non-zero in C,
x || y == 0
is equivalent to
x != 0 || ( y == 0 ) != 0
That means the formula checks if the year isn't divisible by 100.
year % 4 == 0 Year is divisible by 4.
year % 100 Year isn't divisible by 100.
year % 400 == 0 Year is divisible by 400.
(Year is divisible by 4) and ( (Year isn't divisible by 100) or (Year is divisible by 400) )
How this would normally be stated in English:
It's a leap year if it's divisible by 4, but not by 100. Except years divisible by 400 are leap years.
And here's how things are calculated:
year % 4 == 0 && (year % 100 || year % 400 == 0)
1992 % 4 == 0 && (year % 100 || year % 400 == 0)
0 == 0 && (year % 100 || year % 400 == 0)
1 && (year % 100 || year % 400 == 0)
1 && (year % 100 || year % 400 == 0)
1 && (1992 % 100 || year % 400 == 0)
1 && ( 92 || year % 400 == 0)
1 && 1
1
The right-hand side of || isn't evaluated because its left-hand is true.
This return statement
return (year % 4 == 0 && (year % 100 || year % 400 == 0));
can be equivalently rewritten like
return (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
or like
return (year % 4 == 0 && year % 100 != 0 ) || (year % 4 == 0 && year % 400 == 0));
The condition means that a leap year is divisible by 4 and either not divisible by 100 or divisible by 400. So 1992 is a leap year because it is divisible by 4 but not divisible by 100. And 1900 is not a leap year because though it is divisible by 4 but it also divisible by 100 and not divisible by 400. That is neither this condition (year % 4 == 0 && year % 100 != 0 ) nor this condition (year % 4 == 0 && year % 400 == 0)) is satisfied.
How to loop through all bars I have on the graphic and check its RSI?
Code:
sum = 0
if ta.rsi(close, 7) >= 80
sum += 5
else if ta.rsi(close, 7) <= 60
sum -= 5
plot(sum, color=color.green)
It's not working correctly, because on each new bar the sum value sets back to 0. My goal is to see on my graphic the line which increases or decreases in the result of the if statement.
Use the var keyword.
var is the keyword used for assigning and one-time initializing of the
variable.
var sum = 0
if ta.rsi(close, 7) >= 80
sum := sum + 5
else if ta.rsi(close, 7) <= 60
sum := sum - 5
plot(sum, color=color.green)
I am stuck trying to figure this out. I have an array:
a = [ 1 1 1 2 1 1 1 3 2 1 1 2 1 1 1]
I want to add the values in the array so that it equal to 10. Once the added value reaches 10, I want the array to start adding the value again until it reaches 10. There is two problem that I face here,
1) How can I add the array so that the sum = 10 everytime. Notice that in the array, there is 3. If I add all the value before 3, I get 8 and I only need 2 from 3. I need to make sure that the remainder, which is 1 is added to the next array to get the sum 10.
2) How do I break the loop once it reaches 10 and ask it continue the summation to next value to get another 10?
I created a loop but it only works for the first part of the array. I have no idea how to make it continue. The code is as follow:
a = [ 1 1 1 2 1 1 1 3 2 1 1 2 1 1 1];
c = 0;
for i = 1:length(a)
while c < 10
c = c + a(i);
break
end
end
Please help. Thank you
This can be done using cumsum, mod, diff and find as follows:
temp = cumsum(a);
required = find([0 diff(mod(temp,10))] <0)
cumsum returns the cumulative sum which then is rescaled using mod. diff determines where the sum gets greater than or equal to 10 and finally find determines those indexes.
Edit: Above solution works if a doesn't have negative elements. If a can have negative elements then:
temp1=cumsum(a); %Commulative Sum
temp2=[0 diff(mod(temp1,10))];%Indexes where sum >=10 (indicated by negative values)
temp2(temp1<0)=0; %Removing false indexes which may come if `a` has -ve values
required = find(temp2 <0) %Required indexes
This should do what you are trying. It displays the index at which each time the sum equals 10. Check this with your testcases. rem stores the residual sum in each iteration which is carried forward in the next iteration. The rest of the code is similar to what you were doing.
a = [ 1 1 1 2 1 1 1 3 2 1 1 2 1 1 1];
c = 0;
rem = 0;
i = 1;
length(a);
while(i <= length(a))
c = rem;
while (c < 10 && i <= length(a))
c = c + a(i);
i = i + 1;
if(c >= 10)
rem = c - 10;
break
end
end
if(c >= 10)
disp(i-1)
end
use cumsum instead of your while loop:
a = [ 1 1 1 2 1 1 1 3 2 1 1 2 1 1 1];
a_ = a;
endidxlist = false(size(a));
startidxlist = false(size(a));
startidxlist(1) = true;
while any(a_) && (sum(a_) >= 10)
b = cumsum(a_);
idx = find(b >= 10,1);
endidxlist(idx) = true;
% move residual to the next sequence
a_(idx) = b(idx) - 10;
if a_(idx) > 0
startidxlist(idx) = idx;
elseif (idx+1) <= numel(a)
startidxlist(idx+1) = true;
end
a_(1:idx-1) = 0;
end
if (idx+1) <= numel(a)
startidxlist(idx+1) = false;
end
endidxlist gives you the end-indexes of each sequence and startidxlist the start-indexes
I have the image:
A = [3 1 1 2 2
0 0 0 3 2
0 0 3 3 2
1 1 1 1 2
1 1 1 2 2];
From the image I obtained the following matrix:
B = [1,1; 3,3; 2,4; 3,4];
Now, I want to test the distances between each pixel in 'B' to see which ones are greater than 1 when compared to the immediate pixel in the next row. For pixels that have distances <= 1 between them, I would want to replace both locations in ‘A’ with a NaN, otherwise, I would leave them as they are.
My expected output would be:
A = [3 1 1 2 2
0 0 0 NaN 2
0 0 NaN NaN 2
1 1 1 1 2
1 1 1 2 2];
I have tried the following code, but i can’t quite understand what exactly i am doing wrong.
[row, col] = find(A==3);
B = [row col]
for k = size(B, 1)-1
if sqrt( (row(k,:) - (row(k+1,:)))^.2 + (col(k,:) - (col(k+1,:)))^.2 ) <= 1
A(B(k, :)) = NaN
end
end
Please any help on this is greatly appreciated. Many thanks!
If I understood your question correctly, you want to change neighboring 3 cells to NaN. The easiest way is to check all 3 cells and check if one of its neighbors are 3, as done in the code below:
for k = 1:length(row)
if safeCheck(A, row(k)+1, col(k)) || safeCheck(A, row(k), col(k)+1) || ...
safeCheck(A, row(k)-1, col(k)) || safeCheck(A, row(k), col(k)-1)
A(row(k), col(k)) = NaN;
end
end
function b = safeCheck(A, row, col)
if (1 <= row && row <= size(A, 1) && 1 <= col && col <= size(A, 2))
b = A(row, col) == 3 || isnan(A(row, col));
else
b = false;
end
end
I'm trying to calculate percentage change from 2 columns. Since you will get an error when you divide by 0, I wanted to create a CASE that would automatically handle 0s, then handle non-zeros.
The behavior I am trying to implement is:
If StartValue is 0, then the possible outcomes are 0 or 1 (since the current value is either positive or stayed the same).
If the StartValue is not 0, then get the percentage change, but limit the result to between -1 and +1.
UPDATE #Temp
SET ValueChange =
CASE
WHEN StartValue = 0 THEN (CASE
WHEN CurrentValue = 0 THEN 0
WHEN CurrentValue > 1 THEN 1
ELSE 0
END)
ELSE
WHEN ((CurrentValue - StartValue)/StartValue) > 1 THEN 1
WHEN ((CurrentValue - StartValue)/StartValue) < -1 THEN -1
ELSE ((CurrentValue - StartValue)/StartValue)
END;
When I run this query, I get this error:
Incorrect syntax near the keyword 'WHEN'., which references the WHEN ((CurrentValue - StartValue)/StartValue) > 1 THEN 1 section of the code.
Thoughts on the best way to implement this logic?
The correct syntax should be:
UPDATE Temp
SET ValueChange =
CASE
WHEN StartValue = 0 THEN (
CASE
WHEN CurrentValue = 0 THEN 0
WHEN CurrentValue > 1 THEN 1
ELSE 0
END)
ELSE
CASE
WHEN ((CurrentValue - StartValue)/StartValue) > 1 THEN 1
WHEN ((CurrentValue - StartValue)/StartValue) < -1 THEN -1
ELSE ((CurrentValue - StartValue)/StartValue)
END
END;