Im trying to find an easy way to create matrix with self incrementing values i.e., if 3x3 array then it should look like
[[0,1,2],[3,4,5],[6,7,8]]
when I do something like below, I could get all zero's
var arr = Array(repeating: Array(repeating: 0, count: 3), count: 3)
Inorder to acheive, I need to loop through elements and reassign incremented values. Instead of that is there any fast approach I could follow without using for-loop?
A possible approach is to use map() on the range of rows and columns:
let nrows = 3 // Number of rows
let ncols = 3 // Number of columns
let matrix = (0..<nrows).map { row in (0..<ncols).map { col in ncols * row + col } }
print(matrix) // [[0, 1, 2], [3, 4, 5], [6, 7, 8]]
The outer (0..<nrows).map maps each row number to an array (the “row”), and the inner (0..<ncols).map maps each column number to a matrix entry.
With a little bit “tuple magic” you could assign auto-incrementing values:
var entry = 0
let matrix = (0..<nrows).map { _ in (0..<ncols).map { _ in (entry, entry += 1).0 } }
but that's not what I would really recommend.
There is the a Subscripts tutorial on Swift.org showing a Matrix struct example, which I really like
Related
I have searched a lot for removing duplicates from a list in Dart using ANOTHER variable.
Here is what I mean:
List<int> numbers = [1, 2, 3, 4];
// This list has 4 new elements than the first one
List<int> moreNumbers = [1, 2, 3, 4, 5, 6, 7, 8];
// Now I want to push the moreNumbers unique elements to the numbers one
I want to push it so the end result for the numbers variable should be:
[1, 2, 3, 4, 5, 6, 7, 8];
Is it possible?
void main() {
var lst = [1,2,3,4];
var lst2 = [1,2,3,4,5,6,7,8];
var s = {...(lst+lst2)};
print(s.toList());
}
The trivial approach would be:
for (var number in moreNumbers) {
if (!numbers.contains(number)) {
numbers.add(number);
}
}
Not particularly efficient if numbers is long, because contains on a list can take time proportional to the length of the list.
The time/space trade-off would be creating a set from numbers, because sets have cheap contains:
var alsoNumbers = numbers.toSet(); // Also linear, but only happens once.
for (var number in moreNumbers) {
if (alsoNumbers.add(number)) { // true if elements was added
numbers.add(number);
}
}
(Using add instead of contains ensures that you update the set with new values, so you won't add the same new value twice.)
If you could just make numbers a Set to begin with, it would be much easier to avoid duplicates, just do numbers.addAll(moreNumbers).
There were a lot of questions with transpose but I have some specifics and can't create the right formula.
So I have near 100k rows in the following format
https://docs.google.com/spreadsheets/d/146-6YHv69DDOnPKheKeRFZIQeZYIgO6UwCMd7X9VeKU/edit?usp=sharing
I need ARRAYFORMULA or something like that to make this 100k rows in the format that you can see on the "Expected Outcome" tab.
=ARRAYFORMULA({UNIQUE(INDIRECT("source!A2:A"&COUNTA(source!A2:A))),
QUERY(SPLIT(TRANSPOSE(SPLIT(QUERY("♦"&INDEX(SPLIT(TRANSPOSE(SPLIT(
TRIM(QUERY(TRANSPOSE(QUERY(TRANSPOSE(
IF(source!B2:J<>"", "♦"&source!A2:A&"♠"&{"♥"&source!B2:B, source!C2:J}, ))
,,999^99)),,999^99)), "♦")), "♠"),,2),,999^99), "♥")), "♦"),
"where Col2 is not null", 0)})
You have a vast number of records but there are 19 data fields to each record spread over 10 columns & 12 rows. You suggested an arrayformula (and much more clever people than I can probably do that), but I suggest a script which takes the data in its current form on one sheet (say the 'source'), and which outputs the data to a new sheet (say, the 'target').
There are a couple of issues to be addressed in "mapping" the data:
how many records are represented on the input sheet - required to enable looping. I used the Javascript Math.floor method to calculate the number of products.
identify the correct row/column combination for each field. The data is in three segments
8 fields in contiguous columns in the first row,
2 fields in 2 rows in the ninth column, and
9 fields in nine contiguous rows in the tenth column.
Total = 12 rows/10 columns. To do this, in order to navigate the rows, I took the counter (i) multiplied by the number of rows, plus 1; the columns are more intuitive.
for efficiency,
get data once only at the beginning of the script;
use arrays to progressively build the output; and
update the output (setValues) once at the end of the script.
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sourcesheetname = "source";
var targetsheetname = "target";
var source = ss.getSheetByName(sourcesheetname);
var target = ss.getSheetByName(targetsheetname);
var sourcerange = source.getDataRange();
var sourcedata = sourcerange.getValues();
var sourceheaders = 1;
var rowsperproduct = 12;
var sourcelr = source.getLastRow();
var integerPart = Math.floor((sourcelr-sourceheaders)/rowsperproduct);
//Logger.log("DEBUG: number of products = "+integerPart);
var rowdata = [];
// look thought the number of products
for (var i = 0; i<integerPart; i++){
// use a temporary array to build the data for the row.
var temp = [];
// row 1, copy first eight columns (0-7)
temp.push(sourcedata[(i*12)+1][0]);//ID
temp.push(sourcedata[(i*12)+1][1]);//GID
temp.push(sourcedata[(i*12)+1][2]);//NAME
temp.push(sourcedata[(i*12)+1][3]);//PRICE
temp.push(sourcedata[(i*12)+1][4]);//BRAND
temp.push(sourcedata[(i*12)+1][5]);//URL
temp.push(sourcedata[(i*12)+1][6]);//country
temp.push(sourcedata[(i*12)+1][7]);//instock
// row 2 & 3 in Column I(8)
temp.push(sourcedata[(i*12)+2][8]);//url1
temp.push(sourcedata[(i*12)+3][8]);//url2
// rows 4 - 12 in Column J(9)
temp.push(sourcedata[(i*12)+4][9]);// tech name
temp.push(sourcedata[(i*12)+5][9]);// size
temp.push(sourcedata[(i*12)+6][9]);// inches
temp.push(sourcedata[(i*12)+7][9]);// mm
temp.push(sourcedata[(i*12)+8][9]);// depth
temp.push(sourcedata[(i*12)+9][9]);// BB
temp.push(sourcedata[(i*12)+10][9]);// ref1
temp.push(sourcedata[(i*12)+11][9]);// ref2
temp.push(sourcedata[(i*12)+12][9]);// ref3
// Update the rowdata for this row
rowdata.push(temp);
}
// update data to target
target.getRange(2,1,integerPart,19).setValues(rowdata);
}
Is this script:
local data =
{
{ "data1", "1"},
{ "data5", "2"},
{ "3453453", "3"},
{ "zzz", "4"},
{ "222", "5"},
{ "lol", "6"},
{ "asdf", "7"},
{ "hello", "8"},
}
local function test()
local count = #data
for i = 1, count do
print(data[i][1] .. " = " .. data[i][2])
end
end
test()
Guaranteed to output:
data1 = 1
data5 = 2
3453453 = 3
zzz = 4
222 = 5
lol = 6
asdf = 7
hello = 8
If not then why, and what is best way performance wise to make it so?
I read something about pairs VS ipairs not returning a fixed order of results
ipairs is an iterator of the array elements of a table, in order from first to last. "Array elements" being defined as the members of a table with keys that are numeric values on the range [1, #tbl], where #tbl is the length operator applied to the table.
pairs is an iterator over all of the elements of a table: array and non-array elements alike. Non-array elements of a table have no intrinsic order to Lua, so pairs will return them in any order. And even though the array elements do technically have an order, pairs will not make an exception for them; it always operates in an arbitrary order.
Your code works like ipairs: iterating over each of the numeric keys of the table from 1 to its length.
ReactiveCocoa has a collect operator which aggregates all next values into an array until the signal or producer completes then sends that aggregated value. What I need is a progressive collect operator where values will collect into an array until a filter condition is passed at which point the array will be forwarded and the operator will start over again at an empty array and aggregate values all over again.
For example, if given a producer that emits the values 1, 2, 3, 4, 5, 6, 7, 8, 9 in that order and a collectWhile operator:
producer.collectWhile { $0 % 5 == 0 }.on(next: { print($0) })
there'd be two print statements:
[1, 2, 3, 4] and [5, 6, 7, 8, 9]
at which point self would complete and so would the collectWhile operator.
collect is built off of reduce but it uses a private data structure:
/// A reference type which wraps an array to avoid copying it for performance and
/// memory usage optimization.
private final class CollectState<Value> {
var values: [Value] = []
func append(value: Value) -> Self {
values.append(value)
return self
}
}
to avoid copying the aggregate values so I assume I'd probably have to do something similar in my implementation. Any one have any thoughts on how I can accomplish the behavior I'm looking for?
I have some space top with fields:
-id,
-status,
-rating
I have two indexes for space top:
--primary
box.space.top:create_index('primary', { type = 'TREE', unique = true, parts = { 1, 'NUM' } })
--status
box.space.top:create_index('status', { type = 'TREE', unique = false, parts = { 2, 'NUM' } })
I can select by id or status
--select by id
space.top.index.primary:select(someId)
--select by status with limit/offset
space.top.index.status:select({someStatus}, {iterator = box.index.EQ, offset = 0, limit = 20})
Sometimes i need select by status with ordering by rating.
What is the best way? Create another index with parts status, rating and make some tricky query if it`s possible? Or continue select by status and make sort by rating in Lua procedure?
Thanks!
UPD:
Thanks, Kostya!
I modified index status like this:
box.space.top:create_index('status_rating', { type = 'TREE', unique = false, parts = { 2, 'NUM', 3 'NUM' } })
And now i can query:
local active_status = 1
local limit = 20
local offset = 0
box.space.top.index.status_rating:select({active_status}, {iterator = box.index.LE, offset=offset, limit=limit})
Great!
Doesn't make sense to create the third index, if you need to order by rating, just include it into the second index as the second part, and use GE/GT iterator, the data will come out ordered. This is an in-memory database, adding more parts to an index doesn't use up more memory, only slows down insertion a bit.
Call with GE/LE iterator and partial index may work not as expected than there is no matching tuples or limit is too high.
Suppose, we have following tuples (status, rating):
{ 1, 1 }
{ 3, 1 }
{ 3, 2 }
Than call
box.space.top.index.status_rating:select({2}, {iterator = box.index.GE, limit=1})
will return tuple {3, 1} as it greater than {2}
And call
box.space.top.index.status_rating:select({1}, {iterator = box.index.GE, limit=2})
will return two tuples {1, 1}, {3, 1}
In both case tuple {3, 1} may be not expected