I have an array #a = [[9, 15], [], []]
I need to make it [9,15] using map method. How it is possible?
I have tried the below statement,
#a.map{|array| array.collect{|element| element} if array.any?}.compact
But giving [[9,15]] as output. Can anyone just help me out. Thanks :)-
I'm not sure about the requirements, but this gives the desired result
[[9, 15], [], []].flatten
Related
How can I change subscript coordinates in an Array of Arrays [[Int]] so that the way to address a cell would be table[column-letter][row-number], same as Excel, instead of current ios table[row-number][column-number] for a given table like:
var table = [[0, 1, 2, 3],
[1, 32, 44, 25],
[2, 12, 66, 43],
[3, 3, 4, 5]]
Today table[2][1] = 12, but I want table[C][1] = 44.
I need this change to adapt formulas from excel into my app without changing the coordinate system or the tables from an Excel.
I know there are subscript functions in Array class which may help, but I couldnĀ“t make it work so far.
Today table2 = 12, but I want tableC = 44.
Do you know what is [2][1] actually means you are telling the compiler,
i want the element in the array that has index of [2] and by adding another [1], you are telling it that you need the element that has index of [1] that its inside the result of [2].
its an Array of Arrays if you take another look at it empty would be something like this ,
[[],[],[]] // no matter what is the values inside indexing is always the same
with that pointed out now lets take a look on what are you trying to do.
[C][1] = 44
As you can now probably tell this is not valid because [C] is not a valid index as index accept only Integer Types.
So what can we try ?.
Well you can try dictionaries
Read about them here
With dictionary at some point you will be able to get a value like this
value = table["C"]?[1]
or take a look on this example.
var foo :[String:[Int]] = ["A":[1,2,3], "B":[1,2,3]]
let colm = foo["A"]?[0]
Edit another solution inspired by #Paulw11
using this "A".utf16.first! - 65 will return 0 index to use inside the Array so you can additionally say from A to Z
table = ["MyChar".utf16.first! - 65][2]
I'm using Rails 5. I'm having trouble iterating over a set of active records. I access them like so
priority_countries_ids = Country.where(:iso=>priority_countries).all
priority_countries_ids.each do |pc|
but on the "each" line, I'm getting the error
can't quote Array
There is no other information. I don't know how else to troubleshoot this and simply want to iterate over each result from my query.
priority_countries: [["US", "United States"], ["CA", "Canada"]]
The problem is because your priority_countries is not a 1D Array.(http://edgeguides.rubyonrails.org/active_record_querying.html#retrieving-multiple-objects-in-batches)
priority_countries_ids = Country.where(:iso=>priority_countries.flatten).all // Converting priority_countries to 1D using flatten.
Hope it solves your issue.
Could you print priority_countries ? Are you sure it's an array of values ?
try
Country.where(:iso=>priority_countries.to_a).all
I have some code which assembles two-dimensional arrays to be sent to the Spreadsheet gem's excel-building methods. I'd like to "tag" some subarrays (corresponding to rows) with formatting codes like {:color => "red"}.
Can anyone see a way of doing this? I could achieve a similar result by storing a seperate object which has the formatting option and (for example) the indexes to all rows i want to apply that format to. But it would be nicer if I could stick it straight onto the row itself as i build my data.
One thing that occurred to me is to use some kind of namespaced hash as the last entry in an array, if i want to format it, and then strip that out again in the spreadsheet builder. But, this seems risky as it's then in the array's actual data. Is there any kind of instance variable or something i can tap into with an array to "shove" my metadata in there?
I'm using Rails 2.2 for this app, in case that's relevant.
Since each row contains data and possibly some metadata, it seems natural to represent all rows as an Array of Hashes, where each Hash contains a :data key and a :metadata key. The latter can be omitted or can just point to an empty Hash if you do not have any metadata for the row, whichever you prefer.
I have no experience with the Spreadsheet gem, but I assume it requires an Array of Arrays as input, which you can create in a straightforward manner from the Array of Hashes as shown in the code below.
rows = [
{
data: [2, 3, 5],
metadata: { color: 'red' }
},
{
data: [7, 11, 13],
metadata: {}
}
]
# Transform into Array of Arrays, removing all metadata
rows.map { |row| row[:data] }
# => [[2, 3, 5], [7, 11, 13]]
this week I got this homework to do: count the grade of nodes in a undirected graph and test if there is a euler path in it. the function should work like following:
gradliste([[a,b],[b,c],[b,g],[c,d],[d,e],[e,f],[f,g],[g,h],[c,f]],X).
X = [[a, 1], [b, 3], [c, 3], [g, 3], [d, 2], [e, 2], [f, 3], [h, 1]]
testEulerweg([[a,b],[b,c],[c,d],[d,e],[a,e],[b,d],[b,e],[a,d]]).
true.
my first idea of the functiongradlisteis to 'merge' the graph and generate a list like this:
[a,b,b,c,b,g,c,d,d,e,e,f,f,g,g,h,c,f] then I count numbers of every node. unfortunately I stucked at the merge.
and for the second function testEulerweg I think I should firstly write a function allconnected working like following:
allconnected([[a,b],[b,c],[c,d]]).
true.
allconnected([[a,b],[b,c],[e,f]]).
False.
then I can check if there are none or 2 nodes with odd grade number using the gradliste function.
Can anyone help me on my idea? I'm also open for new ideas:)
thanks in advance
bearzk
The merge function is simple. I'll rename it flatten:
flatten([[X,Y] | Edges], [X,Y|Rest]) :-
flatten(Edges, Rest).
And I'll let you write the base case.
As for finding the Eulerian path, check out the algorithms at Wikipedia. The second one can be easily implemented in terms of select/3, as long as you don't flatten the list first :)
Is it possible to do conditional sort in hash?
My hash be like:
{1=>"10",2=>"20",3=>"30",4=>"40",5=>"50",6=>"60",7=>"70",8=>"80",9=>"90"}
Desired result is:
{7=>"70",8=>"80",9=>"90",1=>"10",2=>"20",3=>"30",4=>"40",5=>"50",6=>"60"}
add condition in this code
operation_hour_hash.sort{|key,value| key[1]<=>value[1]}
Sure, you could do something like:
>> {1=>"10",2=>"20",3=>"30",4=>"40",5=>"50",6=>"60",7=>"70",8=>"80",9=>"90"}.sort{|p,n| (p[1].to_i>=70 && n[1].to_i<70) ? -1 : (p[1].to_i<70 && n[1].to_i>=70) ? 1 : p[1].to_i <=> n[1].to_i}
=> [[7, "70"], [8, "80"], [9, "90"], [1, "10"], [2, "20"], [3, "30"], [4, "40"], [5, "50"], [6, "60"]]
But, sorting a hash doesn't really make much sense. Before the sort actually takes place it's converted to an array of [key,value] pairs then sorted by -1,0,1 returned from <=>.
If you need sorted hashes you'll need to use something like RubyFacets Dictionary class.
Your sort call is misleading. You are not comparing key and value, you are comparing two different elements of an array. So it should be:
operation_hour_hash.sort{|a,b| a[1]<=>b[1]}
You can implement whatever logic you want in a sort block, as long as it adheres to following:
The block implements a comparison
between a and b, returning -1, 0, or
+1
(taken from: http://ruby-doc.org/core/classes/Array.html#M002185)
You can try something like this to produce a sorted array
operation_hour_hash.sort {|x,y| (x[0]>6?x[0]-6:x[0]+24)<=>(y[0]>6?y[0]-6:y[0]+24)}
>> [[7, "70"], [8, "80"], [9, "90"], [1, "10"], [2, "20"], [3, "30"], [4, "40"], [5, "50"], [6, "60"]]
You can change the conditions to suit your needs.
However be careful that in ruby 1.8, a hash is not ordered by the key. If you convert back to hash, the order is not guaranteed.