I was practicing my Code and soon I found that '.order' method works in reverse. My rails version is '5.0.0.1'
I used it like:
(i) User.order(salary: :desc).first(3)
(ii) User.order(salary: :desc).first(3)
I got reverse results. I am using mysql database. Can anybody tell me whether its mine mistake or really it's rails problem.
If you are trying to get ascending order result, you should be doing Ascending instead of Descending. The following code will sort the salary in ascending order and get the first 3.
User.order(salary: :asc).first(3)
The code that you provided below will sort the salary in descending order, and then select the first 3.
User.order(salary: :desc).first(3)
Example: given an array with unsorted value [3, 2, 4, 5, 1]. After the following code is executed, you will have the results as below
User.order(salary: :desc) >>> [5, 4, 3, 2, 1]
User.order(salary: :desc).first(3) >>> [5, 4, 3]
User.order(salary: :asc) >>> [1, 2, 3, 4, 5]
User.order(salary: :desc).first(3) >>> [1, 2, 3]
Related
I am learning algorithms and Insertion sort. While solving the quiz, I came across the questions:
In insertion sort, after M passes through the array, How many elements are in sorted order?
With my understanding, I wrote M+1 as the answer. But that turned out to be wrong.
Actual answer is : First M elements are in sorted order after M passes of insertion sort on the array.
Why is this so? This is what I thought:
Say I/p array : 5, 4, 3, 2, 1 is given to insertion sort. Now, after each iteration, the result will look like:
Initial Input ==> after 1st iteration ==> after 2nd ==> after 3rd ==> after 4th
[5, 4, 3, 2, 1] ==> [4, 5, 3, 2, 1] ==> [3, 4, 5, 2, 1] ==> [2, 3, 4, 5, 1] ==> [1, 2, 3, 4, 5]
Here say after 2nd Iteration, we get [3, 4, 5, 2, 1] In which, elements 3, 4, 5 are sorted. So, after 2 passes, 3 elements are sorted. So, why was the answer M?
I tried finding answer from the internet, but there are no reliable resources / no explanations given. What am I missing here?
I am faced with this optimization challenge:
Take for example the array, [1, 2, 4, 3, 3, 6, 2, 1, 6, 7, 4, 2]
I want to split this into multiple sub-arrays, such that their sums are as close to a target sum. Say, 7.
The only condition I have is the sums cannot be more that the target sum.
Using a greedy approach, I can split them as
[1, 2, 4], [3, 3, 1], [6], [2, 4], [6], [7], [2]
The subset sums are 7, 7, 6, 6, 6, 7 and 2.
Another approach I tried is as follows:
Sort the array, in reverse.
Set up a running total initialized to 0, and an empty subset.
If the list is empty, proceed to Step 6.
Going down the list, pick the first number, which when added to the running total does not exceed the target sum. If no such element is found, proceed to Step 6, else proceed to Step 5.
Remove this element from the list, add it to the subset, and update running total. Repeat from step 3.
Print the current subset, clear the running total and subset. If the list isn't empty, repeat from Step 3. Else proceed to Step 7.
You're done!
This approach produced the following split:
[7], [6, 1], [6, 1], [4, 3], [4, 3], [2, 2, 2]
The subset sum was much more even: 7, 7, 7, 7, 7 and 6.
Is this the best strategy?
Any help is greatly appreciated!
I think you should use the terms "subset" and "sub-array" carefully. What you are looking for is "subset".
The best strategy here would be to write the recursive solution that tries each possibility of forming a subset so that the sum remains <= maximum allowed sum.
If you carefully understand what the recursion does, you'll understand that some sub-problems are being solved again and again. So, you can (memoize) store the solutions to the sub-problems and re-use them. Thus, reading about dynamic programming will help you.
I was doing a quick read up on arrays and some basic methods. And one of the exercise questions at the end of the reading gave me an array and asked to get the following output
=> [10, 8, 4, 2]
Here's the array:
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
solution:1
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
numbers = numbers.select { |number| number.even? }.reverse
numbers.delete(6)
p numbers
But my question to you is why would the above code return the correct output but the following code won't?
solution: 2
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
numbers = numbers.select { |number| number.even? }
numbers.delete(6)
numbers.reverse
p numbers
I understand it's not the most fluent, but when I try to solve these exercises it's easier for me to separate everything and then clean up the code.
I expected it to pull the even numbers delete 6 from them and then print the reversed array.
Instead it pulls the even numbers, deletes 6, and prints the even numbers. Completely skips the .reverse
As max says, .reverse doesn't change the array. Try, instead:
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
numbers = numbers.select { |number| number.even? }
numbers.delete(6)
numbers.reverse!
p numbers
=> [10, 8, 4, 2]
As other commenters have mentioned, .reverse doesn't change the array.
You either have to declare numbers.reverse as a new variable (i.e. reversed_numbers = numbers.reverse) or use numbers.reverse! (as demonstrated by jvillian) to change the value of the numbers variable itself at invocation.
Between the two, the latter method is more suitable.
Hope this helped!
I have to sort an array of arrays. I've searched for solutions however my problem is:
need to sort arrays that may have different sizes from a script run to another.
need to sort not only by one or two elements, but, if possible based in all elements.
For example, for the following inputs:
[[2,3,4,5,6],[1,3,4,5,7],[1,3,4,5,8]]
[[5,2,3],[2,2,4],[2,2,5]]
The output should be, respectively:
[[1,3,4,5,7],[1,3,4,5,8],[2,3,4,5,6]]
[[2,2,4],[2,2,5],[5,2,3]]
Do as below
input=[[2,3,4,5,6],[1,3,4,5,7],[1,3,4,5,8]]
input.sort # => [[1, 3, 4, 5, 7], [1, 3, 4, 5, 8], [2, 3, 4, 5, 6]]
I have an array of objects of variable length n. Defined by the number of records in my database.
I need a function to grab subsets (keeping the objects in order and always beginning at index 0) of the array of specified length m where m can be any integer I pass in.
e.g. if n = 10 and m = 4
array foo = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
subset a = [0, 1, 2, 3]
subset b = [4, 5, 6, 7]
subset c = [8, 9]
So, I need to programmatically be able to say, "Give me the i-th subset of length m from an array, given the array is length n." Using the previous example: "Give me the second subset of length four from foo" => returns the items at positions [4, 5, 6, 7].
I hope that made sense. Assistance with a ruby solution would be much appreciated! thx!
foo.each_slice(subset_length).to_a[subset_index]
e.g. foo.each_slice(4).to_a[2] returns "the second subset of length four from foo".
You can use Enumerable#each_slice:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9].each_slice(4).to_a
#=> [[0, 1, 2, 3], [4, 5, 6, 7], [8, 9]]