New answers tagged

-1

# ruby syntax def compare_them (obj1, obj2) if obj1[0].nil? if !obj2[0].nil? return obj1[1] <=> obj2[0] else if obj2[1].nil? if obj1[1] == obj2[0] return -1 else return obj1[1] <=> obj2[0] end else return obj1[1] <=> obj2[1] end end else if ...


1

Most languages allow a custom comparator function to be supplied to the sort. See Java Comparator One use of a custom comparator, for example, is to sort based on two attributes (a primary an secondary) with one sort. A comparator for this will first compare the primary attributes, then only if they are equal, will check the secondary attribute. A custom ...


2

To guarantee ascending order in both columns we can compare by column when there is no nil and by max value when there is: .sort { |(a0, a1), (b0, b1)| if [a0, b0].none?(&:nil?) a0 <=> b0 elsif [a1, b1].none?(&:nil?) a1 <=> b1 else a0, a1, b0, b1 = [a0, a1, b0, b1].map(&:to_i) [[a0, a1].max, a0, a1] <=> ...


-1

Ah...I think I may have an answer. Here's a two-pass approach (calling each pair [x, y]). Select all pairs where x is not nil. Sort them by x. Call the resulting list s. Select all pairs where x is nil. Insert them into s at the appropriate places (I'm not sure how you determine the appropriate places, though). Does that help?


2

No. Your function does not give consistent answers on how to sort numbers, because greater(x, y) != not greater(y, x) for some values of x and y. For example (3 % 4) % 2 == 1, and (4 % 3) % 2 == 1 also! This means that 3 > 4 AND 3 < 4, which is nonsense. If you convert it to a cmp-style function like @SystemDown suggests, you will get inconsistent ...


0

It sounds like the thing you want to do is this: Pick an array from the weighted list of non-empty arrays Pop a value from that array and add it to the output array Repeat some number of times You seem to be having some trouble doing step 1 in a fair way. My suggestion is to flatten the weighted list into a list containing each array <array-weight> ...


0

If you are talking about huge data, you should consider using BTree data structure (or its variants). BTrees are used by most relational databases for indexing huge data. You can check B-tree on Wikipedia for more details. Based on the amount of data you have to deal with, your objective functions vary. For example, if you have to deal with very huge data, ...



Top 50 recent answers are included