Given a json array:
[{ "x":"5", "y":"20" },{ "x":"6", "y":"10" },{ "x":"50", "y":"5" }]
I'd like to find argmax(x), such that I can do puts argmax(arr, :arg => "x").y and get 5. How can I elegantly implement this in Ruby?
Edit: Clarified a bit. The idea is that you can specify the field of an element in a list that you want to maximize and the method will return the maximizing element.
I think you want Enumerable#max_by. To get y like you're saying, it would be:
arr.max_by {|hash| hash['x']}['y']
(Well, actually, you'll want the numbers to be numbers instead of strings, since '50' sorts lower than '6'. But I think you get the idea. You can to_i or do whatever processing you need in the block to get the "real" value to sort by.)
Related
I have unsorted arrays as some_array. When I use some_array.max to get the max number, I get the following output. For:
some_array = ["1.10.0", "1.11.0", "1.12.0", "1.13.0", "1.14.0", "1.15.0", "1.16.0",
"1.16.1", "1.17.0", "1.18.0", "1.7.0", "1.8.0"]
I get 1.8.0 instead of 1.18.0. For:
some_array = ["1.11.0", "1.12.0", "1.13.0", "1.14.0", "1.14.1", "1.15.0", "1.16.0", "1.17.0", "1.18.0", "1.19.0", "1.5.0", "1.8.0", "1.9.0"]
I get 1.9.0 instead of 1.19.0.
To me, it looks like max is picking up the last value from the array. Shouldn't max print the maximum value in the array? Do I have to sort the array before using max? Is there any other way to get max value out of array?
The existing answers already point out the problem in your code. This is how you may write it:
some_array.max_by { |version| version.split(".").map(&:to_i) }
#=> "1.18.0"
The problem is that you are thinking that they are integers, but they're not - they're strings. So they get sorted lexicographically. That is to say, treat them like an alphabet. "1.8.0" is greater than "1.18.0", because the "8" is greater than the "1" character (in the 3rd position).
Look at the following random strings. They are in order:
"abcdef"
"abdghi"
"adaaaa"
Why are they in order? Because you look at the first character, and compare them. Then look at the next character, and compare them, etc. Now look at your example:
"1.18.0"
"1.8.0"
Look at the first character of each, it's a "1", it's equal. Look at the next character, both are ".", they're equal. Look at the next characters, "1" and "8". "1" comes before "8". Therefore, "1.8.0" must come lexicographically after "1.18.0".
If you want to treat them like integers, there are a few things you can do. 1) You can write your own sort method in a block, or 2) wrap the strings in some hand-made Version object, and then write the comparator there.
If you need help with these specific ideas, let us know.
Shouldn't max() print the maximum value in the array?
Yes. It should. And it is doing so, as you can see in the results you got.
Do I have to sort the array before using max()?
No.
Is there any other way to get max value out of array?
Yes. But you already got the max value.
this method checks character by character.
So, comparing 1.9.0 and 1.19.0 , it will give 1.9.0 as bigger than 1.19.0 because the part "1." is equal in both strings, but the next character is 9 in the first one, and 1 in the second one, so the method will say 1.9.0 is bigger since 9 comes later than 1 in ASCII code.
When you need to sort version numbers the Versionomy Gem helps a lot. It also handles all the edge cases, for example beta and RC versions: 1.8beta < 1.8RC < 1.8.
Using that gem you must change your code to something like this:
require 'versionomy'
some_array.max_by { |version| Versionomy.parse(version) }
I really don't understand the following sorting method:
books = ["Charlie and the Chocolate Factory", "War and Peace", "Utopia", "A Brief History of Time", "A Wrinkle in Time"]
books.sort! { |firstBook, secondBook| firstBook <=> secondBook }
How does the this work? In the ruby books, they had one parameter for example |x| represent each of the values in the array. If there is more than one parameter (firstBook and secondBook in this example) what does it represent??
Thank you!
The <=> operator returns the result of a comparison.
So "a" <=> "b" returns -1, "b" <=> "a" returns 1, and "a" <=> "a" returns 0.
That's how sort is able to determine the order of elements.
Array#sort (and sort!) called without a block will do comparisons with <=>, so the block is redundant. These all accomplish the same thing:
books.sort!
books.sort_by!{|x| x}
books.sort!{|firstBook, secondBook| firstBook <=> secondBook}
Since you are not overriding the default behavior, the second and third forms are needlessly complicated.
So how does this all work?
The first form sorts the array by using some sorting algorithm -- it's not relevant which one -- which needs to be able to compare two elements to decide which comes first. (More on this below.) It automatically, behind the scenes, follows the same logic as the third line above.
The middle form lets you choose what to sort on. For example: instead of, for each item, just sorting on that item (which is the default), you can sort on that item's length:
books.sort_by!{|title| title.length}
Then books is sorted from shortest title to longest title. If all you are doing is calling a method on each item, there's another shortcut available. This does the same thing:
books.sort_by!(&:length)
In the final form, you have control over the comparison itself. For example, you could sort backwards:
books.sort!{|first, second| second <=> first}
Why does sort need two items passed into the block, and what do they represent?
Array#sort (and sort!) with a block is how you override the comparison step of sorting. Comparison has to happen at some point during a sort in order to figure out what order to put things in. You don't need to override the comparison in most cases, but if you do, this is the form that allows that, so it needs two items passed into the block: the two items that need to be compared right now. Let's look at an example in action:
[4, 3, 2, 1].sort{|x, y| puts "#{x}, #{y}"; x <=> y}
This outputs:
4, 2
2, 1
3, 2
3, 4
This shows us that in this case, sort compared 4 and 2, then 2 and 1, then 3 and 2, and then finally 3 and 4, in order to sort the array. The precise details are irrelevant to this discussion and depend on the sorting algorithm being used, but again, all sorting algorithms need to be able to compare items in order to sort.
The block given inside {} is passed as a comparing function for method sort. |a, b| tells us that this comparing function takes 2 parameters (which is expected number of arguments since we need to compare).
This block is executed for each element in array but if we need one more argument we take next element after this.
See http://ruby-doc.org/core-2.0/Array.html#method-i-sort for an explanation. As for a single-parameter method referred to in your books, I can only guess you were looking at sort_by. Can you give an example?
This might be a dumb question.
I know sample returns random number of elements from an array.
For example,
[1,2,3].sample.times do
Is there a way to return a fixed number of elements in a certain order always?
I dont know how to do this in ruby.
EDIT:
Lets say I always want to return penalty_name, severity and name only from the second and last array here always.:
offenses = PERSON_SUMMARY[:offenses].map do |offense|
offense[:penalties].map do |penalty|
penalty.merge(name: offense[:offense_name])
end
end.flatten
=> [{:penalty_name=>"Prison", :severity=>"Medium", :name=>"Speeding"}, {:penalty_name=>"Ticket", :severity=>"Low", :name=>"Speeding"}, {:penalty_name=>"Prison", :severity=>"Medium", :name=>"Shoplifting"}, {:penalty_name=>"Fine", :severity=>"Low", :name=>"Shoplifting"}]
right now I am doing:
offenses.each do |hash|
hash.sample
I think you want something like:
[1,2,3,4,5].sample(4).sort
It will take 4 random number from the array and order it...
edit - after your comment:
[5,4,3,2,1].values_at(1,-1).sort #second element(1) and last one(-1)
=>[1, 4]
You can specify which ones you want with values_at (negative numbers count from the back.)
ar=[{:penalty_name=>"Prison", :severity=>"Medium", :name=>"Speeding"}, {:penalty_name=>"Ticket", :severity=>"Low", :name=>"Speeding"}, {:penalty_name=>"Prison", :severity=>"Medium", :name=>"Shoplifting"}, {:penalty_name=>"Fine", :severity=>"Low", :name=>"Shoplifting"}]
p ar.values_at(1,-1).map{|h|h.values}
#=> [["Ticket", "Low", "Speeding"], ["Fine", "Low", "Shoplifting"]]
I'm looking to extract n random key-value pairs from a hash.
Hash[original_hash.to_a.sample(n)]
For Ruby 2.1,
original_hash.to_a.sample(n).to_h
I don't know of such method. Still you can do something like:
h[h.keys.sample]
If you need to sample more than one element the code will have to be a bit more complicated.
EDIT: to get key value pairs instead of only the value you can do something like:
keys_sample = h.keys.sample(n)
keys_sample.zip(keys_sample.map{|k| h[k])
Reading the top ranked answers, I'd go with it depends:
If you want to sample only one element from the hash, #Ivaylo Strandjev's solution only relies on hash lookup and Array#sample:
hsh[hsh.keys.sample]
To sample multiple hash elements, #sawa's answer leverages Array#to_h:
hsh.to_a.sample(n).to_h
Note that, as #cadlac mentions, hsh.to_a.sample.to_h won't work as expected. It will raise
TypeError: wrong element type String at 0 (expected array)
because Array#sample in this case returns just the element array, and not the array containing the element array.
A workaround is his solution, providing an n = 1 as an argument:
hsh.to_a.sample(1).to_h
PS: not looking for upvotes, only adding it as an explanation for people newer to Ruby.
If your sample has only one element, you could use this:
sample = h.keys.sample
h.select { |k,v| k == sample }
Or if your sample contains more than one element, use this:
n = 2
sample = h.keys.sample(n)
h.select { |k,v| sample.include?(k) }
One way to accomplish this:
rank_hash = {"Listen" => 1, "Download" => 60, "Share" => 150, "Purchase" => 700 }
rank_array = rank_hash.to_a
Than call this to get random array sample of the k/v pair:
rank_array[rand(0..3)]
or this to not hard-code the arrays length:
rank_array[rand(0..(rank_array.length) -1)]
Example:
["Download", 60]
I believe that I may be missing something here, so please bear with me as I explain two scenarios in hopes to reconcile my misunderstanding:
My end goal is to create a dataset that's acceptable by Highcharts via lazy_high_charts, however in this quest, I'm finding that it is rather particular about the format of data that it receives.
A) I have found that when data is formatted like this going into it, it draws the points just fine:
[0.0000001240,0.0000000267,0.0000000722, ..., 0.0000000512]
I'm able to generate an array like this simply with:
array = Array.new
data.each do |row|
array.push row[:datapoint1].to_f
end
B) Yet, if I attempt to use the map function, I end up with a result like and Highcharts fails to render this data:
[[6.67e-09],[4.39e-09],[2.1e-09],[2.52e-09], ..., [3.79e-09]]
From code like:
array = data.map{|row| [(row.datapoint1.to_f)] }
Is there a way to coax the map function to produce results in B that more akin to the scenario A resultant data structure?
This get's more involved as I have to also add datetime into this, however that's another topic and I just want to understand this first and what can be done to perhaps further control where I'm going.
Ultimately, EVEN SCENARIO B SHOULD WORK according to the data in the example here: http://www.highcharts.com/demo/spline-irregular-time (press the "View options" button at bottom)
Heck, I'll send you a sucker in the mail if you can fill me in on that part! ;)
You can fix arrays like this
[[6.67e-09],[4.39e-09],[2.1e-09],[2.52e-09], ..., [3.79e-09]]
that have nested arrays inside them by using the flatten method on the array.
But you should be able to avoid generating nested arrays in the first place. Just remove the square brackets from your map line:
array = data.map{|row| row.datapoint1.to_f }
Code
a = [[6.67e-09],[4.39e-09],[2.1e-09],[2.52e-09], [3.79e-09]]
b = a.flatten.map{|el| "%.10f" % el }
puts b.inspect
Output
["0.0000000067", "0.0000000044", "0.0000000021", "0.0000000025", "0.0000000038"]
Unless I, too, am missing something, your problem is that you're returning a single-element array from your block (thereby creating an array of arrays) instead of just the value. This should do you:
array = data.map {|row| row.datapoint1.to_f }
# => [ 6.67e-09, 4.39e-09, 2.1e-09, 2.52e-09, ..., 3.79e-09 ]