python 3 recursive function that prints in order - function

I want to print my list in order, but it keeps printing the first value
def print_order(s):
if not s:
return
print(s[0])
print_order(s[:-1])
for example I have a list [1, 2, 3, 4, 5, 6, 7] I want it to be
printed out as
1
2
3
4
5
6
7

You are taking the last element off instead of the first. Try changing the recursive call's argument to s[1:].

The slice s[:-1] is all elements except the last.
You want s[1:], which is all elements except the first.

Related

Formatting text (from array) on prompt window

I have a very simple prompt that outputs elements from an array:
const arr = [1, 2, 3, 4, 5, 6];
function alertMessage() {
SpreadsheetApp.getUi().alert(`List of numbers: ${arr}`);
}
alertMessage();
The message currently reads like this:
List of numbers: 1,2,3,4,5,6
However, I would like it to create a new line after each element (with no commas), so it would look like the below example. What would be the best way to do this?
List of numbers:
1
2
3
4
5
6
It seems that in your script, arr is an array. So, how about the following modification?
From:
SpreadsheetApp.getUi().alert(`List of numbers: ${arr}`);
To:
SpreadsheetApp.getUi().alert(`List of numbers: \n${arr.join("\n")}`);
If you want the double line breaks, please modify to SpreadsheetApp.getUi().alert(List of numbers: \n${arr.join("\n\n")});
Reference:
join()

Aurelia: Trying to make 2 HTML columns with single array

I'm having a problem figuring out how to make two equal columns (won't be exactly equal if the array length is odd) out of a single array.
So , and have them in two columns.
This isn't really a question specific to Aurelia, but I'm guessing the follow-up question would be.
won't be exactly equal if the array length is odd
That tells me you want to have this:
[1, 2, 3, 4, 5, 6]
And turn it into this:
[[1, 2], [3, 4], [5, 6]]
If you want to do this in a repeater, try this:
export class PairValueConverter {
fromView(input) {
return input.reduce((res, cur, i, arr) {
if (i % 2 === 0) res.push(arr.slice(i, i + 2));
return res;
}, []);
}
}
And then in your html:
<div repeat.for="item of items | pair">${item[0]} - ${item[1]}</div>
It's better if you put more effort in your question though, show what you've tried, etc. Someone might judge me for answering this :)

can anyone explain to me why the following two arrow functions are equivalent? [duplicate]

This question already has answers here:
What do curly braces inside of function parameter lists do in es6?
(3 answers)
Closed 5 years ago.
I am pretty new to javascript. saw this on MDN regarding arrow functions.
Can anyone explain to me how does the 2nd one work? I understand the first one.
Not quite sure why we put length in an object, and then return the length???
Case 1 (which i understand from how it transformed from ES5):
materials.map((material) => {
return material.length;
}); // [8, 6, 7, 9]
Case 2 (not getting what {length} is doing here and also why do we return length:
materials.map(({length}) => length); // [8, 6, 7, 9]
Thank you so much!
Update:
So reading from the answer from Jeff B. It appears that the 2nd one is doing the following with destructuring:
materials.map(({length}) => length)
in which {length} will set a variable var length to equal to materials.length; and that's why we can simply return length. That makes sense. Thanks Jeff
This uses a destructuring assignment to get at the length property without saving the whole object. Specifically, this is a case of "object destructuring".
For instance:
let yourObject = {foo: 1, bar: 2}
let {bar} = yourObject;
// bar now equals 2
With this in mind, you can see how ({length}) => length sets the name length to the length property of the first parameter and then immediately returns it—making the two expressions equivalent.

for loop in thymeleaf

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)}

Mathematica - CSV to Multidimensional Charts

I have a CSV file with 5 columns and about 2*104 rows that I need to visualise.
I've imported the file like so:
data = Import["res.csv", "CSV"];`
Now, I'm going to want to generate a lot of visuals from this - all 5 dimensions on a single plot as well as various cross sections.
My questions:
If I want to select, say columns 1, 4 and 5 from my data and feed them to ListPlot3D how would I do that?
And, values in columns can be grouped. So if I wanted to ListPlot3D colums 1, 2, 4 and 5, but I want to group columns 1 and 2 on the same axis, how would I tell Mathematica to do that?
Thanks.
I hate to disagree with a fellow poster especially after it has been accepted, but the Transpose is unnecessary. Almost everything you're asking for can be done within the context of Part:
ListPlot3D[ data[[All, {1, 4, 5}]] ]
Since matrices are stored row-wise within Mathematica, [[All, {1, 4, 5}]] can be read [[rows, columns]]. More specifically, All indicates here that you want all rows, but you can specify specific rows as well. Another construct that may be of interest is Span which is used to specify groups of indices, and if your CSV file contains a header row, you can strip it from your data using
ListPlot3D[ data[[ 2 ;; , {1, 4, 5}]] ]
As to your second requirement, to use both columns 1 and 2 as the x coordinate, then it is simply
ListPlot3D[ {data[[All, {2, 4, 5}]], data[[All, {1, 4, 5}]]} ]
and you change All to 2;; if you wish to strip off the header row.
If I understand you correctly that would be
ListPlot3D[Transpose[{data[[All, 1]], data[[All, 4]], data[[All, 5]]}]]
and for the multiple sets:
ListPlot3D[
{
Transpose[{data[[All, 1]], data[[All, 3]], data[[All, 4]]}],
Transpose[{data[[All, 2]], data[[All, 3]], data[[All, 5]]}]
}
]