How to get the indexes from duplicate values in a list - composite

I have the following code.
R_sigma22 = [matrix([[489.13845994]]), matrix([[1.87773385]]), matrix([[1.8822108]]), matrix([[1.70095661]]), matrix([[1.70095661]]), matrix([[1.8822108]]), matrix([[1.87773385]]), matrix([[489.13845994]])]
R_min = [[1.70095661]]
##find the element indices for duplicate minimum R values##
index = []
bam = len(R_min_MST_list)
for n in range(bam):
index.append(R_sigma22.index(R_min_MST))
Index of minimum R's
[3, 3] <--- 2nd index should be a 4
The second index found should be a 4, not a 3. Any ideas on how I can skip the first duplicate value to the second duplicate?

Related

How cascading modifies original object?

var intList = [3, 2, 1];
var sorted = intList..toList()..sort(); // [1, 2, 3]
var sorted2 = intList..toList().sort(); // [3, 2, 1]
Why my original list is also being modified in first sort and which list is being sorted in second sort?
NOTE: I'm not looking for the correct way to do it which is this:
var sorted = intList.toList()..sort(); // [1, 2, 3]
x..y evalutes to x. Cascade chains are evaluated left-to-right, so x..y..z is the same as (x..y)..z. Your first example therefore makes calls to toList() and to sort() on the original object.
Member access (.) has higher precedence than the cascade operator (..). Your second example calls sort() on the copy returned by toList(), not on the original object.

Create nXn matrix with values incrementing Swift

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

In Rails, how do I figure out if an array of objects contains specific attributes matching given values?

I'm using Ruby on Rails 5.0.1 with Ruby 2.4. I have an array of objects, stored in the array, "results." Each object has a numeric attribute
numeric_attr
I would like to know, given my array, how I can tell if I have exactly one object with a numeric attribute value of "1" and incrementing by one. Order is not important. So, for instance, if I have an array of three objects,
[MyObject(numeric_attr = 2), MyObject(numeric_attr = 1), MyObject(numeric_attr = 3)]
I want to know if I have exactly one object with numeric_attr = 1, another object with numeric_attr = 2, and another with numeric_attr = 3. So the above satisfies the condition. The below example does not
[MyObject(numeric_attr = 4), MyObject(numeric_attr = 1), MyObject(numeric_attr = 3)]
because although there is an object with numeric_attr = 1, there is no object with numeric_attr = 2. It is possible thet the numeric_attr field is nil. How can I figure this out?
This one-liner should work:
results.map(&:numeric_attr).sort == (1..results.count).to_a
Explanation:
results
#=> [#<MyObject:... #attr=2>, #<MyObject:... #attr=3>, #<MyObject:... #attr=1>]
results.map(&:attr)
#=> [2, 3, 1]
results.map(&:attr).sort
#=> [1, 2, 3]
(1..results.length).to_a
#=> [1, 2, 3]
# therefore:
results.map(&:attr).sort == (1..results.count).to_a
#=> true
If there is a chance that numeric_attr is nil:
results.map(&:attr).compact.sort == (1..results.count).to_a
Of course, if there is even a single nil value, the result is guaranteed to be false.
If the sequence could start at any number, not just 1:
results.map(&:attr).sort == results.count.times.to_a.
map { |i| i + results.map(&:attr).sort.first }
This is not very efficient though, as it sorts the numbers twice.
If they always start at 1 #Máté's solution works, if they can start at any arbitrary number then you could:
count = 0
array_objects.sort_by(&:numeric_attr).each_cons(2) {|a,b| count+=1 if a.numeric_attr==b.numeric_attr-1 }
count+1==array_objects.count
Not as elegant but handles a lot more situations

remove item from a list by specific index

I have this table:
local ls = {
["foo"] = {1, 2, 3, 4, 5},
["bar"] = {5, 4, 3, 2, 1}
}
I want to remove "foo" from list.
I tried this:
table.remove(ls, "foo")
but returns a error: "Only numbers"
Okay, but I can't input a number. This list isn't static, in my code a lot of indexes will be inserted in this list.
The question is, is there other way to do this or other function that fit my problem?
table.remove only works for a sequence. In your code, the table ls isn't one.
To remove an entry from a table, just assign the value of specific key to nil:
ls.foo = nil

LUA: Loop in 2d table to display first key name

Currently I'm stuck on this:
t = {['79402d'] = {'-5.4','5','1.6'}, ['5813g1'] = {'3','0.15','18'}}
Now i need to loop through this table to check if name == t[1], but how can i do so?
I tried doing something like: for i=1,#t,1 do print(t[i]) but it doesn't seem to work.
I hope you can help me guys ;)
Not sure why it didn't worked first time but i solved my problem with:
for a,b in pairs(t) do
print(a, b[1], b[2], b[3])
end
Please note that the length operator # will give you the correct number of elements in a table only in a special case. in your case #t will return 0, hence your for loop does nothing.
Please refer to https://www.lua.org/manual/5.3/manual.html Section 3.4.7 – The Length Operator for details on how to use the lenght operator.
For a regular array, with non-nil values from 1 to a given n, its length is exactly that n, the index of its last value. so #t will be zero if you have no t[1] or n-1 if t[n] is the first nil value in your table starting from t[1].
As you have no t[1] but only a t['79402d'] and a t['5813g1'] your for loop indexing t[i] would not work anyway.
Please read 3.4.9 – Table Constructors on how table construction works.
t = {"hello", "world"}
is the same as
t = {}
t[1] = "hello"
t[2] = "world"
(here t[1] is "hello" and #t is 2
whereas t = {['key1'] = "hello", ['key2'] = "world"}
equals
t = {}
t['key1'] = "hello"
t['key2'] = "world"
so t[1] here is nil and #t is 0
# operator returns lenght of array part of the table. Your table is not an array (i.e. a table with non-nil values from index 1 to a given n). Because of that your loop is not iterating any elements.
Use pairs to iterate over all keys in the table regardless of what they are.

Resources