How can I do the following (java):
for(int i = 0; i < 81 ; i+=20){
//Should loop through 5 times!
}
in Thymeleaf?
I've tried this:
<option th:each="i : ${#numbers.sequence( 1, 81/20)}">
<p th:text="${ i }"></p> <!-- THIS loops 4 times, instead of 5 -->
</option>
The problem is that it is not as accurate as the java piece of code. How to do this?
Add step to your code is quite easy.
#{numbers.sequence(0, 81, 20)}
use iterStat key word to iterate. Example
If you have an Array of String and you are iterating the same using thymeleaf.
<div th:each="str,iterStat : strings">
<span th:text="${str}"/><!--This will print the index value-->
<span th:text="${iterStat.index}"/><!--This will print the Index-->
</div>
I am assuming this is due to the numbers you are using. For your java code,
int i = 0; i < 81 ; i+=20
will return i=0, i=20, i=40, i=60 and i=80
however your following code
numbers.sequence( 1, 81/20)}
should returns the integers from 1 to 4.05, being 1, 2, 3, and 4.
The first loop returns 5 results for i, therefore runs 5 times.
the second returns only 4 results, so runs 4 times. I would suggest running your sequence starting at 0 to return 5 results as desired.
If you wanted your java code to mirror the second code, you should change it to:
int i = 1; i < 4.05 ; i+=1
To put it simply, you are running through a loop with different numbers, I suggest changing the second statement to start from 0.
The 1st value is the beginning of count, the 2nd is the maximum value and the 3rd is the incremental value
${#numbers.sequence( 1, 4, 1)}
Related
I had the task to code the following:
Take a list of integers and returns the value of these numbers added up, but only if they are odd.
Example input: [1,5,3,2]
Output: 9
I did the code below and it worked perfectly.
numbers = [1,5,3,2]
print(numbers)
add_up_the_odds = []
for number in numbers:
if number % 2 == 1:
add_up_the_odds.append(number)
print(add_up_the_odds)
print(sum(add_up_the_odds))
Then I tried to re-code it using function definition / return:
def add_up_the_odds(numbers):
odds = []
for number in range(1,len(numbers)):
if number % 2 == 1:
odds.append(number)
return odds
numbers = [1,5,3,2]
print (sum(odds))
But I couldn’t make it working, anybody can help with that?
Note: I'm going to assume Python 3.x
It looks like you're defining your function, but never calling it.
When the interpreter finishes going through your function definition, the function is now there for you to use - but it never actually executes until you tell it to.
Between the last two lines in your code, you need to call add_up_the_odds() on your numbers array, and assign the result to the odds variable.
i.e. odds = add_up_the_odds(numbers)
I'm trying to understand why the output for z is always -1 whenever I trace the variable. I'm counting coins and I'm trying to set up a for loop, however, I'm always prompted by an error because of z = -1.
for (var z:int = coins.length; z >= 0; z--);
{
trace(z);
trace(coins.length);
}
The output answer I get for these two variables are:
Z = -1
coins.length = 3
Why is this the case? Because all I'm seeing on the output box is:
-1
-1
-1
-1
-1
-1
keeps repeating
If we were to go by the for loop logic, shouldn't the variable z be like this instead?
2
1
0
What can be wrong?
There's your problem:
for (var z:int = coins.length; z >= 0; z--); // the semicolon at the end.
With the semicolon, the loop ends. You wrote a loop without a body. That's perfectly valid and executes just fine.
After the loop, the following code is executed once:
{
trace(z);
trace(coins.length);
}
z is -1, because that's its last value in the loop which causes the loop to stop executing. coins.length never changed and has a value according to the array.
If we were to go by the for loop logic, shouldn't the variable z be like this instead?
2
1
0
No, because it starts at 3, not 2.
def listc(favn):
num = 0
while num < favn :
num += 1
return num
list = []
i = int(raw_input("Input your favourite number : > "))
for num in range(0,i):
list.append(listc(i))
print list
The elements of the list are just same. Little iterations in code are sometime printing [None] in list also.
I want to generate a list with content as 1 to i.
There are two issues with your code.
First the while loop does not run 'favn' no. of times because the return statement is within while loop.It just runs single time, and everytime it returns 1.
Also, you should change
for num in range(0,i):
list.append(listc(i))
to
for num in range(0,i):
list.append(listc(num))
You will get the output you wanted.
If you want to generate a list from 1 to i, you can simply do list = range(1, i + 1).
ex:
public static int fibb (int n) {
if(n==0||n==1)
return 1;
else{
return fibb(n-1)+fibb(n-2);
}
}
How will the line fibb(n-1)+fibb(n-2) be executed .. like will fibb(n-1) finish first the fibb(n-2) starts or how exactly, I'm fairly new to recursion and can't seem to wrap my head around how it works.
Help appreciated.
First, the recursive calls will be executed (the order in which being dependent on your programming language), then their results will be summed together.
fibb(3) returns fibb(2) + fibb(1)
fibb(2) returns fibb(1) + fibb (0)
so you get 1 + 1 + 1 = 3
fibb(4) returns fibb(3) + fibb(2), we know fibb(3) returns three from above,
fibb(2) returns fibb(1) + fibb(0) also from above,
so fibb(4) returns 3 + 2 = 5
It's important to notice that with this implementation you must computer each previous Fibonacci number twice. Which means by the time you get to around ~20 (guess) it's going to get VERY slow.
I'm trying to split an HTML document into its head and body:
my #contentsArray = split( /<\/head>/is, $fileContents, 1);
if( scalar #contentsArray == 2 ){
$bodyContents = $dbh->quote(trim($contentsArray[1]));
$headContents = $dbh->quote(trim($contentsArray[0]) . "</head>");
}
is what i have. $fileContents contains the HTML code. When I run this, it doesn't split. Any one know why?
The third parameter to split is how many results to produce, so if you want to apply the expression only once, you would pass 2.
Note that this does actually limit the number of times the pattern is used to split the string (to one fewer than the number passed), not just limit the number of results returned, so this:
print join ":", split /,/, "a,b,c", 2;
outputs:
a:b,c
not:
a:b
sorry, figured it out. Thought the 1 was how many times it would find the expression not limit the results. Changed to 2 and works.