This question already has answers here:
What is x after "x = x++"?
(18 answers)
Why is this Java operator precedence being ignored here?
(7 answers)
Closed 4 years ago.
int f = 1;
f = f++;
System.out.println(f);
post increment operator is having a higher precedence than assignment operator so
i suppose value of f (that is 1) is used for assignment and f is incremented then the output would be 2 (as value of f is now 2)
but the output is 1, but how? where am i wrong?
my explanation results in right answer in the code below
int f = 1;
int g = f++;
System.out.println(f);
output is 2 in this case.
You can't assign a value to a variable before you evaluate the value. Hence you must evaluate f++ first. And since f++ returns 1, that's the value assigned to f, which overwrites the incremented value of f.
Your second snippet is a completely different scenario, since you assign the value of f++ to a different variable, so the incremented value of f is not overwritten.
Related
This question already has an answer here:
How to use multiple variables in Rust's for loop?
(1 answer)
Closed last month.
I want initialize multiple variables in a for loop, like for example something like this:
for (i, j) in (1..=4),(10..=16).step_by(2){ println!("i = {i}, j ={j}"); }
The result would be something like:
i = 1, j = 10 i = 2, j = 12 i = 3, j = 14 i = 4, j = 16
I have checked this post: How to use multiple variables in Rust's for loop?
but I havenĀ“t found anything useful for me, I know I can use a while loop, but I find the ability to set everything in just one line much clearer
The zip iterator method, mentioned in the very answer you link to, creates tuples by pairing up elements of two iterators. You can still use it all on one line; there is nothing that requires you to create the two iterators as separate statements.
for (i, j) in (1..=4).zip((10..=16).step_by(2)) {
println!("i = {i}, j ={j}");
}
(Playground)
This question already has answers here:
Why is division in Ruby returning an integer instead of decimal value?
(7 answers)
Closed 3 years ago.
I am not understanding why my code isn't looping, for any input I give I get the result of "1.00"
I feel like I am being silly and missing something very obvious.
This series is shown in other places most often as 1/3n+1
But if series_sum(1) = "1.00"
then 3+1 = 4 giving you 1/4 to add to your sum for input of 1 which doesn't make sense
def series_sum(n)
sum = 0
if n == 0
return "0.00"
else
for i in 1..n
sum += 1/(1+(3*(i-1)))
end
end
return "%.2f" % sum.to_s
end
for series_sum(1) should be "1.00"
series_sum(2) should be "1.25"
series_sum(3) should be "1.39"
etc
My code gives "1.00" for any input
Why won't this code run the loop and perform the sum?
When you have expression like z = x/y, in ruby it does specify type for output based on operand provided for division. If x & y both are integer, output calculated is also integer and float value is removed.
So to obtain output as float, you need to have one operand at least as float value which can be found using to_f on variable. Here you need to change only,
- sum += 1/(1+(3*(i-1)))
+ sum += 1.0/(1+(3*(i-1)))
This question already has answers here:
How does this block work for Integer times method?
(2 answers)
Closed 6 years ago.
I was reading through the documentation for Enumerator and I ran across this example:
fib = Enumerator.new do |y|
a = b = 1
loop do
y << a
a, b = b, a + b
end
end
Everything makes sense to me except for this line: a, b = b, a + b. Could somebody please explain what's happening?
It's a parallel assignment pattern which you can see in many languages including ruby
probably you will find this helpful
Parallel Assignment operator in Ruby
This question already has answers here:
Valid Permutation of Parenthesis [duplicate]
(4 answers)
Closed 7 years ago.
This was asked to me in an interview. I didn't know the proper recurrence for it at that time. The question was, if I am given the length of the expression, then how many proper bracket expressions can be made of that length and what will they be?
A proper bracket expression is for example,
[[]][[[]][]]
[[[][]]][][[]]
The following is not a proper bracket expression,
[[[][]]][]][[]]
That is, there is a closing bracket for each opening bracket.
I am not looking for the implementation, but just the algorithm or how should I approach it?
Thanks!
Here's an algorithm in Python:
def properbracket(l, s):
if l == 0:
if s == 0:
return 1
else:
return 0
ret = 0
if s > 0:
ret += properbracket(l-1, s-1)
ret += properbracket(l-1, s+1)
return ret
print properbracket(2, 0)
print properbracket(4, 0)
print properbracket(6, 0)
...and here are the valid expressions for different lengths:
len == 2:
[]
len == 4:
[[]]
[][]
len == 6:
[[[]]]
[][][]
[][[]]
[[][]]
[[]][]
This assumes that "][" is not a valid expression.
The following recurrence is based on the leading open bracket must have a matching closing bracket, such that there must be a valid expression inside these brackets and another after the closing one
P(0) = 1
P(1) = 0
P(2) = 1
P(N>2) = sum(i=0..N/2-1)( P(2i) * P(N-2-2i) )
This question already has answers here:
Ternary expression with "defined?" returns "expression" instead of value?
(3 answers)
Closed 9 years ago.
def fib(n)
[0,1].include? n ? n : (fib(n-1) + fib(n-2))
end
fib 5 => false
Why is this? Since it works when broken out into standard if then else.
Operator precedence. You're effectively doing this:
[0, 1].include? (n ? n : fib...)
That is, the result of n ? n : (fib(n-1) + fib(n-2)) is found, and that is passed to include?.
Use parenthesis to force the order of evaluation you intend:
[0, 1].include?(n) ? n : (f(n - 1) + fib(n - 2))