Sorting array in ruby - ruby

A user has defined the order of columns which is in an array.
order = [:col1, :col2, :col3]
Since the user defined columns order the state of table has changed and the current list of columns is
cols = [:col1, :col4, :col3, :col5]
With the given order cols need to sorted. In this case the sorted columns could look like one of these two cases.
[:col2, :col3, :col4, :col5]
[:col2, :col3, :col5, :col4]
Here is my code to get it working. Wondering if there is a better way.
#get rid of :col2 since it is not present in the cols listing
sanitized_order = order - (order - cols)
sorted_cols = sanitized_order + (cols - sanitized_order)

What do you mean by better? You already accomplished your task pretty handily.
1) This is like yours but 1 readable line.
#order & cols -> provides unique items found in both
#cols - order -> provides columns that are in cols but not order
sorted_cols = (order & cols) + (cols - order)
2)
Here is a way that reads more like a book so someone could follow it line by line to see the logic instead of figuring out the differences in tables:
order = [:col1, :col2, :col3]
cols = [:col1, :col4, :col3, :col5]
sanitized_order = []
order.each do |column|
if cols.include?(column) then
sanitized_order << column
cols.delete(column)
end
end
cols.each do |remainingcolumn|
sanitized_order << remainingcolumn
end
puts sanitized_order

Here's another wordy way to do it:
order = [:col1, :col2, :col3]
cols = [:col3, :col2, :col5, :col4]
sorted_order = cols.sort_by do |c|
if order.index(c)
[1, order.index(c)]
else
[2, cols.index(c)]
end
end
p sorted_order # => [:col2, :col3, :col5, :col4]
Here's how it works. sort_by yields elements of an array to the block; the block should then return something comparable (technically, something that responds to the <=> operator). sort_by uses the <=> operator on the results returned by the block to decide what order the array should be in.
The <=> (spaceship) operator, as you may know, is a binary operator taking two elements a and b. If a < b, it returns -1. If a == b, it returns 0. If a > b, it returns +1.
Arrays respond in an unsurprising way to the <=> operator. The elements of the left array are compared with the elements of the right array in turn, starting with index 0 and increasing. If the a[i] <=> b[i] is != 0, return that result, but if the result is 0, examine the next element. If the last pair of elements compared is 0 (equal) and the arrays are the same size, the arrays are equal and Array.<=> returns 0, otherwise the longer array is considered larger (this example always returns equal size arrays, however).
So, for example:
[2, 1] <=> [2, 2] == -1
[2, 2] <=> [2, 2] == 0
[2, 3] <=> [2, 2] == +1
[1, 2] <=> [2, 1] == +1
So, inside a sort_by, we can use the elements of an array a to indicate primary, secondary, tertiary etc. sort order. a[0] is the primary sort order, a[1] is the secondary sort order, and so on.
All of the columns the customer specified should come first. So, for each column, find its index in customer specified order (order). If that returns a number, then we know that the customer specified that column, and we know its index in the customer-specified list. The primary sort order is 1, because we want the customer specified columns to come first; the secondary sort order is the index, since that gives us the order the customer specified the columns.
If we don't find the column in the order array (that is, order.index(c) returns nil), then we'll 2 as the primary sort order and the index in the master column list (cols) as the secondary sort order. That way all columns the customer did not specify will be last, but in the order they are specified in the laster column list.

Related

What's wrong with this implementation of 'Highest product of consecutive elements'

I am trying to write a method that returns the highest product from adjacent values within an array. Below is my attempt to do so however it fails to return the highest products in some instances, patterns of which I am unclear on and I cannot see why the problems with this code:
def adjacentElementsProduct(inputArray)
inputArray.each_with_index do |value, index|
if inputArray[index+1]
products = [] << value * inputArray[index + 1]
return products.max
end
end
end
a) Can anyone help me understand what is wrong with the above implementation
b) Can anyone suggest a less verbose and simpler method of achieving the desired.
Here are the fails and passes (this is from codefights.com, 1st question, 2nd chapter 'Edge of the Ocean'):
First of all, you're returning from the method after calculating only the first product. So all your answers are simply the product of the first two numbers. To fix it, initialize your products variable before the loop and put your return after the loop.
As for a cleaner implementation, take a look at Enumerable's each_cons, which returns consecutive members of an array (for example, each_cons(2) returns consecutive pairs). Then you can multiply each pair in one fell swoop via map and return the maximum.
def adjacentElementsProduct(inputArray)
inputArray.each_cons(2).map{|a,b| a*b}.max
end
I presume that, for an array arr, you want the largest product
arr[i] * arr[i+1] * arr[i+2] *...* arr[j-1] * arr[j]
where 0 <= i <= j <= arr.size-1. We can do that in a Ruby-like way using Enumerable#each_cons, as #Mark suggested.
def max_adjacent_product(arr)
n = (1..arr.size).each_with_object({prod: -Float::INFINITY, adj: []}) do |len, best|
arr.each_cons(len) do |a|
pr = a.reduce(:*)
best.replace({prod: pr, adj: a}) if pr > best[:prod]
end
end
end
max_adjacent_product [2, -4, 3, -5, -10]
#=> {:prod=>150, :adj=>[3, -5, -10]}
I loop through the number of adjacent elements to consider. In the example, that would be from 1 to 5. For each number of adjacent elements, len, I then loop through each subarray a of adjacent elements of arr for which a.size equals len. In the example, for len = 3, that would be [2, -4, 3], [-4, 3, -5] and [3, -5, -10]. For each of those subarrays of adjacent elements I then compute the product of its elements (-24, 60 and 150 for the example just given). If a product is greater than the best known product so far I make it the best subarray so far. The final value of best is returned by the method.

How to write a binary search method if value might not be in array

How would I write a binary search method if the value I'm looking for might not be in the array?
Binary search:
def binary_search(array, value, from=0, to=nil)
to = array.count - 1 unless to
mid = (from + to) / 2
if value < array[mid]
return binary_search(array, value, from, mid - 1)
elsif value > array[mid]
return binary_search(array, value, mid + 1, to)
else
return mid
end
end
It works great if the value is in the array.
binary_search([1,2,3,5], 5)
But if its not, there is an error.
binary_search([1,2,3,5], 4)
stack level too deep (SystemStackError
I have two large arrays of strings (each string is unique in its own array), but there is overlap across the arrays which is what I'm trying to find. I need to modify the method so it won't crash the script if it doesn't find a specific string, and then continues iterating.
You could imagine each string looks similar to akbvuxef. Though that shouldn't matter.
Is this homework? If not, then just use Array#bsearch.
If it is homework, then consider what happens when you reach the point that you have no more values to test; your to will be <= your from (that is, the size of your search space is empty - there are no more values left to test). In the case that happens, you should raise an exception or return some value which is interpreted as "value not found".
If you are trying to find the overlap of 2 arrays use the Set Intersection operator:
[1, 2, 3] & [2, 3, 4]
=> [2, 3]

Merge sort algorithm using recursion

I'm doing The Odin Project. The practice problem is: create a merge sort algorithm using recursion. The following is modified from someone's solution:
def merge_sort(arry)
# kick out the odds or kick out of the recursive splitting?
# I wasn't able to get the recombination to work within the same method.
return arry if arry.length == 1
arry1 = merge_sort(arry[0...arry.length/2])
arry2 = merge_sort(arry[arry.length/2..-1])
f_arry = []
index1 = 0 # placekeeper for iterating through arry1
index2 = 0 # placekeeper for iterating through arry2
# stops when f_arry is as long as combined subarrays
while f_arry.length < (arry1.length + arry2.length)
if index1 == arry1.length
# pushes remainder of arry2 to f_arry
# not sure why it needs to be flatten(ed)!
(f_arry << arry2[index2..-1]).flatten!
elsif index2 == arry2.length
(f_arry << arry1[index1..-1]).flatten!
elsif arry1[index1] <= arry2[index2]
f_arry << arry1[index1]
index1 += 1
else
f_arry << arry2 [index2]
index2 += 1
end
end
return f_arry
end
Is the first line return arry if arry.length == 1 kicking it out of the recursive splitting of the array(s) and then bypassing the recursive splitting part of the method to go back to the recombination section? It seems like it should then just keep resplitting it once it gets back to that section as it recurses through.
Why must it be flatten-ed?
The easiest way to understand the first line is to understand that the only contract that merge_sort is bound to is to "return a sorted array" - if the array has only one element (arry.length == 1) it is already sorted - so nothing needs to be done! Simply return the array itself.
In recursion, this is known as a "Stop condition". If you don't provide a stop condition - the recursion will never end (since it will always call itself - and never return)!
The result you need to flatten your result, is because you are pushing an array as an element in you resulting array:
arr = [1]
arr << [2, 3]
# => [1, [2, 3]]
If you try to flatten the resulting array only at the end of the iteration, and not as you are adding the elements, you'll have a problem, since its length will be skewed:
arr = [1, [2, 3]]
arr.length
# => 2
Although arr contains three numbers it has only two elements - and that will break your solution.
You want all the elements in your array to be numbers, not arrays. flatten! makes sure that all elements in your array are atoms, and if they are not, it adds the child array's elements to itself instead of the child array:
arr.flatten!
# => [1, 2, 3]
Another you option you might want to consider (and will be more efficient) is to use concat instead:
arr = [1]
arr.concat([2, 3])
# => [1, 2, 3]
This method add all the elements in the array passed as parameter to the array it is called on.

Return array a if given number is in it, return array a and the given number if it is not

Here's what I thought:
def appendUnique(a,x)
for i in 0 .. a.size-1 do
if a[i]=x then
a==a
else
a=a+x
end
p(a)
end
end
appendUnique([-1,5,3],4)
Compare each member of a with x, if a equals x, return a, else return a+x. Why doesn't this work? It just replaces all array members with 4s...
I want this: result [-1, 5, 3, 4] from the above since 4 isn't in the array and [-1, 5, 3] from appendUnique([-1,5,3],5).
There are several issues with your code:
in Ruby we usually use each instead of for to iterate collections
a[i] = x is an assignment, you want a[i] == x
a == a just returns true
a + x concatenates two arrays, but x is not an array
I would simply use Array#include? to check if the item is present:
def appendUnique(array, item)
if array.include? item
array
else
array + [item]
end
end
If you want an array with unique elements you can use Set class
It just replaces all array members with 4s...
a[i]=x is an assignment rather than comparison. Running this in a loop, as you do, would set every element of a to x (which is 4).
The rest of the code needs quite a lot of work too. For example: you should only be appending to a after you've run the loop and have established that x isn't in the array.

Ruby : Finding lowest free ID in an ID array

I have an array with different IDs going from 1 to 4000. I need to add some elements in a database with an ID that would go in that array. Since the biggest ID possible is 4000 (which is not that much in my case), I'd like to be able to find the lowest unused ID possible I could use for my new element.
I would know how to do that in C++, but since I'm pretty new in Ruby, I'm asking for help. in C++, I would write a loop in which I would check if array[i] == array[i+1] - 1. If not the case, then the new id would be array[i] + 1.
I have just no idea how to write that in Ruby.
Using a range, you can find the first element that is not part of your array:
array = [1,2,3,5,6]
(1..4000).find { |i| !array.include?(i) }
# => 4
array = [1, 2, 3, 5, 6]
(1..4000).to_a.-(array).min
def first_unused_id(ids)
index = ids.each_index.find{|i| ids[i] + 1 != ids[i+1] }
ids[index] + 1
end
Some explanation:
each_index will transform the array into an Enumerator giving the arrays indices.
find will return the first element that returns true from the block passed to it.
how about this one:
(1..4000).find { |i| array[i-1] != i }
similar to Dylan's answer but in this case, it simply checks whether the [n-1]th member of the array is n. If not, that index is "open" and is returned. This solution only requires one check per index, not 4000...
so for
array = [1,2,3,5,6]
this would find that array[4-1] != 4 (because array[3] = 5) and return 4 as the first available id.
(this requires a sorted array of indices but that has been assumed so far)
array = [1, 2, 3, 5, 6]
def lowest_unused(ids)
ids.find { |e| ids.index(e) + 1 != e } - 1
end
p lowest_unused(array) # 4

Resources