This question already has answers here:
Sass is returning a list of negative numbers instead of subtracting them
(2 answers)
Closed 7 years ago.
li.level0 {
#for $i from 1 through 10 {
#if $i % 2 == 0 {
$n: 2 * $i -1;
&:nth-child($n) {
// when n is even
}
} #else {
$n: 2 * $i -2;
&:nth-child($n) {
// when n is odd
}
}
}
}
I want to get the sequence 1,2,5,6,9,10,13,14,17,18
which is 2n-1 if n is odd 2n-2 if n is even how do I calculate that? I keep getting syntax error, for substituting An+b with variable $n. what did I do wrong?
I have find a formula online which is a (1/2)(4n-(-1)^n + 3) now is just a matter of insert the product in the nth-child so I expect my output to be
li.level0:nth-child(1) {}
li.level0:nth-child(2) {}
li.level0:nth-child(5) {}
li.level0:nth-child(6) {}
li.level0:nth-child(9) {}
li.level0:nth-child(10) {}
Here I have found an interesting solution to this problem.
according to http://nthmaster.com/
I can apply multiple nth-child clause to the selector
the answer ending up as
&:nth-child(odd):nth-child(4n-1) {
background-color: black;
}
&:nth-child(even):nth-child(4n-2) {
background-color: black;
}
please see fiddle for DEMO
https://jsfiddle.net/djs0m63q/
Related
This question already has answers here:
Why is my power operator (^) not working?
(11 answers)
Closed 2 years ago.
Given the equation y^2=x^3+2*x+4 mod 7 i want to find all possible solutions with x and y being in the range of 0 to 6. I have written the following program:
for (int y=0;y<7;y++)
{
for (int x=0;x<7;x++)
{
if ((x^3+5*x+4)%7==(y^2)%7)
{
cout<<"("<<x<<","<<y<<")"<<endl;
}
}
}
However, the output of this program is wrong. For example, the program prints out (4,1), however this does not satisfy the equation. How can i fix this?
I think the problem is that x^3 and y^2 are not power operation, it is xor operation indeed, so, you can use x*x*x and y*y instead:
for (int y=0;y<7;y++)
{
for (int x=0;x<7;x++)
{
if ((x*x*x+5*x+4)%7==(y*y)%7)
{
std::cout<<"("<<x<<","<<y<<")"<<std::endl;
}
}
}
}
I have this code which is not compatible with swift3
for var i = FCatalogIdx-1; i > -1; i -=1 {
// access items in array FCaalogArr
}
Code converter suggest converting to
for i in ((-1 + 1)...FCatalogIdx-1).reversed() {
which seems to be the the same as:
for i in (0...FCatalogIdx-1).reversed() {
Is this the closest I can get to my old code? And if so, is it recommended to use the code converter suggestion - the (-1 + 1) looks ugly, but I suspect it is to give a hint of the original c-style loop logic?
You shouldn't use the (-1+1) because that is just needlessly confusing. Also, use the half-open range operator ..< to create a range that doesn't include the final index:
for i in (0 ..< FCatalogIdx).reversed() {
}
The advantage of the half-open range operator ..< over the closed range operator ... is that it is possible to create an empty range.
Consider this example:
let n = 0
for i in 0 ..< n {
print("hello") // range is empty, so this is never printed
}
for i in 0 ... n-1 { // fatal error: Can't form Range with upperBound < lowerBound
print("hello")
}
for i in FCaalogArr.indicies.reversed() would be better assuming FCaalogIdx is the last index of the array
This question already has answers here:
How do I use the conditional (ternary) operator?
(10 answers)
What does the question mark character ('?') mean in C++?
(8 answers)
Closed 6 years ago.
I took few help online to complete my task. And I found this code but I do not know the actual working as I have never used such syntax before in c++. The (?) Question mark and (:) colon.Can any one provide a simple general syntax code explaining same line?
x = (i-coins[j] >= 0)? table[i - coins[j]][j]: 0;
This means
if (i-coins[j] >= 0)
x = table[i - coins[j]][j];
else
x = 0;
This is called ternary operator, and it is used in place for a short if-else statement.
int factorial(int number) {
if (number < 1) {
return 1;
} else {
return number*(number-1);
}
}
The above function can be summed up using a ternary operator:
int factorial(int number) {
return (number < 1) ? 1 : number*(number-1);
}
I am trying to validate mobile using regular expression
so for i have tried
https://regex101.com/#javascript
My Expresion is ((\d[^0-5]|[6-9])\d{9})|^(\d)\1*$
i need validate moblie number like below
1.it should not start with 0-5
e.g 0600432102
2.not all the same or in sequence
e.g 1111111111 or 0123456789 or 9876543210
3.lenght is 10 digit
where i made error.
help me....
thanks ....
This covers all criteria and tests a few numbers. It does however not specify the reason for a number being invalid - I leave that to you.
var numArr = ["1111111111", "0123456789", "9876543210", "8682375827", "83255"];
for (var i = 0; i < numArr.length; i++) {
console.log(numArr[i] + " is " + validate(numArr[i]));
}
function validate(num) {
if ((num.match(/^[6-9]\d{9}$/) && (!num.match(/^([0-9])\1{9}$/))) && (!isIncr(num) && (!isDecr(num)))) {
return ( "Valid") ;
} else {
return ( "Not valid") ;
}
}
function isIncr(num) {
for (var i = 1; i < num.length; i++) {
if (num[i] == parseInt(num[i - 1]) + 1) {
continue;
} else {
return false;
}
}
return true;
}
function isDecr(num) {
for (var i = 1; i < num.length; i++) {
if (num[i] == parseInt(num[i - 1]) - 1) {
continue;
} else {
return false;
}
}
return true;
}
(([6-9])(?!\2{9})\d{9})
will:
Check if the number starts with 6-9. It stores this in match group 2.
Check that the first digit is not followed by exactly 9 (you can set the limits here) repetitions of the same digit.
Continues to find if there are 9 more digits after the first digit.
will not:
Check whether the numbers are ascending/descending order.
Check for patterns such as 6566666666
you can use the following regex:
/^(?!9876543210)(?!([\d])\1{9})[6-9][\d]{9}$/mg
Explanation
(?!9876543210) Check that the string is different (it's the only sequence possible)
(?!([\d])\1{9}) Check that this is not the same number repeated
[6-9][\d]{9} Check that the number start with 6 to 9, followed by 9 numbers
This regex works for repititions and numbers not starting with 0-5.
(?!([0-9])\1{9})(\b[6-9]\d{9})
See demo here: https://regex101.com/r/uJ0vD4/9
It doesn't detect ascending and descending numbers. You can achieve that using a loop in your program
I have a BST of three elements {1, 2, 3}. Its structure looks like
2
/ \
1 3
Now I try to calculate the height for each node using BSTHeight() defined below and have some problem with calculating the height of '2', which value is supposed to be 1 as the heights of '1' and '3' are defined as 0. My problem is that with direct use of heights from '2's two children (see part 2 highlighted below), its height is ALWAYS 0. However, its value is correct if I use two temporary integer variables (see part 1 highlighted below). I couldn't see any difference between the two approaches in terms of functionality. Can anyone help explain why?
void BSTHeight(bst_node *p_node)
{
if (!p_node)
return;
if (!p_node->p_lchild && !p_node->p_rchild) {
p_node->height = 0;
} else if (p_node->p_lchild && p_node->p_rchild) {
BSTHeight(p_node->p_lchild);
BSTHeight(p_node->p_rchild);
#if 0 // part 1
int lchild_height = p_node->p_lchild->height;
int rchild_height = p_node->p_rchild->height;
p_node->height = 1 + ((lchild_height > rchild_height) ? lchild_height : rchild_height);
#else // part 2
p_node->height = 1 + ((p_node->p_lchild->height) > (p_node->p_rchild->height)) ? (p_node->p_lchild->height) : (p_node->p_rchild->height);
#endif
} else if (!p_node->p_lchild) {
BSTHeight(p_node->p_rchild);
p_node->height = 1 + p_node->p_rchild->height;
} else {
BSTHeight(p_node->p_lchild);
p_node->height = 1 + p_node->p_lchild->height;
}
}
Problem lies in operator precedence. Addition binds stronger than ternary operator, hence you must surround ternary operator (?:) with brackets.
Below is the corrected version. Note that all brackets you used were superflous and I've removed them. I've added the only needed pair instead:
1 + (p_node->p_lchild->height > p_node->p_rchild->height ?
p_node->p_lchild->height : p_node->p_rchild->height);
Even better would be to use std::max (from <algorithm>) instead:
1 + std::max(p_node->p_lchild->height, p_node->p_rchild->height)