Is there a way to use sort_by on a second criterion if the first is equal? [duplicate] - ruby

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Sort strings and numbers in Ruby
I have an array of place objects, each with a name (unique) and number (non-unique). Is there a simple way to use sort_by to first sort by number and then (within each number) by name?
I know I can write a custom block for sort, but if this is possible, it'd be even easier!

Not sure if this is what you mean by "custom block" but it seems pretty simple to me:
places.sort_by { |place| [place.number, place.name] }

Related

Is map just a more powerful each? [duplicate]

This question already has answers here:
Can't all or most cases of `each` be replaced with `map`?
(4 answers)
What is the difference between map, each, and collect? [duplicate]
(2 answers)
Closed 8 years ago.
If you want a method that collects an array without modifying it, you can use map, and you'll have something that works the same as each. For example, you could do this:
array.each do |x|
x += 10
print "#{x}"
end
but you could just as easily do this:
array.map{|x| print (x + 10).to_s}
and it would have the exact same result. While each can only do that, map can alter its function using the !, so I don't see why I would use each anymore. Could you explain why I should ever use each instead of map if map seems more versatile?
No. Use each for side-effects; use map for a (side-effect free) transformation.
While they both iterate the enumerable (at some point1), map collects the transformed results which should be used. To say map is a more powerful each is like saying a method that returns an unused value is more powerful than a method does not return a value - it's not of being more powerful, it's about using the correct tool.
Thus, while map can "do" what each does (by evaluation of supplied block), it does more and is useful for a different task: when the transformation, and not the side-effect, is desired. It is often considered poor practice to perform side-effects in a map (excluding, perhaps, the mutation of the mapped objects).
1Furthermore, map and each are not strictly interchangeable. In lazy vs. eager situations, a transformation like map can be lazy while each is only useful for side-effects and is never lazy. (It is not possible for each to be lazy because there is no resulting sequence to "observe" and force the evaluation later.)

Why does Ruby allow me to push an array on itself? [duplicate]

This question already has answers here:
What are recursive arrays good for?
(2 answers)
Closed 8 years ago.
This code is valid in Ruby
a = [5,10,15]
[5,10,15]
a.push a
[5,10,15,[...]]
Resulting in the fourth array slot pointing to the array itself, (seemingly) infinitely. Why does Ruby allow this and does the functionality offer any practical applications?
Since in Ruby everything is an object, variables just point to the objects (more strictly speaking, memory locations). An array is a collection of such a pointers, which means it can store a pointer to itself. It is not an extra feature added in Ruby, it would be actually an extra feature not to allow it.
As for application, check out "What are recursive arrays good for?" (directed graph representation).
Note however, that such an array is not infinite:
a = []
a << a
a.length = 1
Since Ruby is a dynamic language, an array is, in essence, a collection of "any object" so you can push anything you want into it, including other arrays, including (in this case) a reference to itself. It's like an ArrayList<Object> in Java, which can do the same thing (you can add it to itself, but why?)
It might be sometimes useful to have recursive structures, though nothing comes to mind.

confirmation of my Counting inversion experiment [duplicate]

This question already has answers here:
Counting inversions in an array
(38 answers)
Closed 9 years ago.
I have written a mergesort(divide and conquer) algorithm and I want to use the following arrays to test if the inversion works efficiently..
So, i would just want to confirm the inversion for each of the following arrays .
1. {10,2,3,22,33,7,4,1,2} = 13
2. {4,5,6,1,2,3} = 9
3. {1,20,6,4,5} = 5
4. {3,1,2,0,4} = 5
are all these correct? I do know a question similar has been asked but I just want to confirm if my calculation was correct. with that, I can test my algorithm.Also, this is not an homework. I just want to be so so sure that I have the write inversion count so that i can test it against my code..
No.
Your output for the first case should be 22. Also this is not how you check the efficiency of your code. You should try to check in some competitive programming sites for similar kind of problems. For example there a problem in spoj for Counting inversion. Here is the link:
http://www.spoj.com/problems/INVCNT/
Try submitting it there.

Is there a way to check if two regexps can match the same string? [duplicate]

This question already has answers here:
Regex: Determine if two regular expressions could match for the same input?
(5 answers)
Closed 10 years ago.
I have two regexps. I need to determine if it is possible to build string of given length that matches these two regexps simultaneously. I need algorithm to do that.
String's length wouldn't exceed 20 characters.
It depends. For perl compatible regular expressions (pcre), this is not generally possible, as they are turing complete: you cannot even be sure that matching always terminates.
For the original, "clean" form of reguler languages as defined in the Chomsky-hierarchy, it is known that they are closed under intersection, this is already discussed in this thread.
As soon as you have the NFA for the intersection, it is easy to check whether any string matches it - if thera is a path from the start to the end of your NFA, then the string for this path is the string you are searching for, for DFAs, an algorithm is given here, it should be simple to adapt it to NFAs.

Compare two versions of a text file and find additions/removals with Ruby? [duplicate]

This question already has answers here:
diff a ruby string or array
(12 answers)
Closed 8 years ago.
I am tracking changes in a web-page using Ruby. After I removed all html tags and blank lines, I get an array of lines which needs to be checked for additions/removals assuming that there may be repetitions. Could you recommend a good gem if it has been done already?
I could make the array lines unique and then the problem is avoided. But what if I need to track the repeated lines as well with respect to their position in the text?
Sounds like a textbook case of where you'd want to use the Diff algorithm.
There's a 'diff' gem, although to be fair I've never used it: http://rubygems.org/gems/diff

Resources