This question already has answers here:
Ranges A to B where A > B in F#
(5 answers)
Closed 9 years ago.
Is there a way one can get a range in descending order?
Ex
[1..4]
evaluates to
> val it : int list = [1; 2; 3; 4]
But
[4..1]
evaluates to
> val it : int list = []
Is there a different syntax to achive this without having to do a List.Reverse ?
You have to do:
[4..-1..1]
The -1 is the step
Related
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.
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:
Split list into multiple lists with fixed number of elements
(5 answers)
Closed 8 years ago.
I would like to subdivide a list into lists of list with a max size for each sublist. For example, given List(1,2,5,3,90,3,4,1,0,3) and max size of sublists defined as 4, I would like to get List(List(1,2,5,3), List(90,3,4,1), List(0,3)) back.
This is what has already been done:
val l: List[Int] = ???
val subSize: Int = 4
val rest: Int = if(l.size % subSize == 0) 1 else 0
val subdivided: List[List[Int]] = for{
j <- List.range(0, l.size/subSize - rest, 1)
}yield{
for{
i <- List.range(subSize*j,subSize*j+3,1)
if(i < l.size)
}yield{
l(i)
}
}
Is there a better, more functional way of doing this?
Yes there is, using grouped.
scala> List(1,2,5,3,90,3,4,1,0,3).grouped(4).toList
res1: List[List[Int]] = List(List(1, 2, 5, 3), List(90, 3, 4, 1), List(0, 3))
Note that grouped actually returns an Iterator, so that you can lazily traverse the collection without doing all the computation at once.
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))
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
B = 3
while(B <= 11):
print(B)
B = B+2
I already tried a bunch of stuff, all of it crap, like:
for B <= 11:
which is apparently invalid syntax, and i've tried:
B= 3
if B <= 11:
print(B)
B = B+2
which does absolutely nothing
So, any suggestions?
Looks like you're learning Python. What you are looking for is a range:
for B in range(3,12,2):
print(B)
Note that the parameters here are 3, 12 and 2.
The 3 is the starting point.
The 12 is used instead of 11 for the end of the range, because the range() function in Python excludes the last value of the range. You'll want to keep that in mind when writing Python code.
The 2 is the step value.
You can use something like this (Java):
for(int B = 3; B <= 11; B += 2)
{
System.out.println(B);
}