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

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

Related

Bash - how to sort negative values? [duplicate]

This question already has answers here:
Linux sort doesn't work with negative float numbers
(3 answers)
Closed 6 years ago.
is there a way to sort negative numbers with sort in bash ? I have sin written out
...
0.250109
0.188852
0.126850
0.064349
0.001593
-0.061168
-0.123689
-0.185722
-0.247023
-0.307349
...
and the problem is when I run sort on it, it just sorts it by values - regardless of the minus in front of some values. Is there a way to fix it ? Thanks
Use sort -g (--general-numeric-sort), not sort -n (--numeric-sort).
See sort Invocation for an explanation of the subtle differences between these two options.
my problem was that the data wasnt well formatted - because of my locale, I had to sed decimal point into decimal comma - that's how its written in czech republic
thanks

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.

Find the words in string with no spaces [duplicate]

This question already has answers here:
Detect most likely words from text without spaces / combined words
(5 answers)
Closed 8 years ago.
Lets suppose a string with no spaces:
Input : "putreturnsbetwenparagaphs"
Output : put returns between paragraphs
This could get more complex as more words overlap. How to achieve this really fast. If required does spell corrections and splits the word. Think about it.
One problem could be the plural or case of the word. In your example it could be difficult to make a difference between paragraph and paragraphs.
Do you have more information? Are some words in a explicit grammatical form, or could any word of a common dictionary including case, numerus etc. occour?

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.

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

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] }

Resources