Ruby difference in arrays [closed] - ruby

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
Write a program that takes as input a list of n integers and produces as output the largest difference obtained by subtracting an integer in the list from the one following it

I can think of this using the combination method.
ar = (1..12).to_a
ar.combination(2).max_by { |a,b| b - a }
# => [1, 12]
ar = [1,23,56,11]
# => [1, 23, 56, 11]
ar.combination(2).max_by { |a,b| (b - a).abs }
# => [1, 56]
It seems I misunderstood the line as OP mentioned :
output the largest difference obtained by subtracting an integer in the list from the one following it
Here is the correct one :-
ar = [1,23,56,11]
ar.each_cons(2).max_by { |a, b| (b - a).abs }
# => [56, 11]

Related

RAILS - HOW TO COMPARE VALUES BY ARRAY INDEX [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 12 months ago.
Improve this question
I'm new to rails and I was wondering how I compare the first value of array "a" if it is greater than the first value of array "b"?
Example:
a = [1, 2, 3]
b = [3, 2, 1]
How do I check if a[0] is greater than b[0].
You can use the first method:
a = [1, 2, 3]
b = [3, 2, 1]
a.first > b.first #=> false

Merging/Compressing list of arbitrary integer [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I'm looking for an algorithm to merge/compress lists of unsorted integers in to a single list while maintaining the original lists.
Example
A = [1, 1, 3]
B = [3, 2, 9]
C = [1, 3, 2]
D = [1]
A C
| |
Result[ 1 1 3 2 9 ]
| |
D B
I found lots of results with similar questions but most of them deal with sorted lists or break them up. I'm sure something like this exists but I simply don't know the correct terminology.

How to find uncommon elements from three arrays? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
A,B,C are the arrays :
A = {1,2,3,4}
B = {8,1,2,3}
C = {1,2,9,3}
Result is the uncommon values from three arrays
Result = {4,8,9}
Asking for the logic what I can implement ?
There might be some other better answers. But here is the most simple one.
On line 4 I took the Intersection of 3 sets
On line 7 I took the Union of three sets
On line 10 the difference or XOR operation, of Union and intersection
I hope it helps:-
>>> a = {1,2,3,4}
>>> b = {8,1,2,3}
>>> c = {1,2,9,3}
>>> d = a & b & c
>>> print(d)
{1, 2, 3}
>>> e = a | b | c
>>> print(e)
{1, 2, 3, 4, 8, 9}
>>> f = d^e
>>> print(f)
{4, 8, 9}

Difference between select and collect [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Can't understand the difference between select and collect methods. Also want to know when to use each.
Enumerable#collect (or Enumerable#map) returns a result of applying block to each items.
[1, 2, 3, 4].collect { |x| x > 2 }
# => [false, false, true, true]
While Enumerable#select returns an array of filtered items:
[1, 2, 3, 4].select { |x| x > 2 }
# => [3, 4]

Fastest way to split ruby array following a structure [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I'm looking for the fastest way to split an array into sub-arrays with different size. The size of every array is driven by a configuration file.
Example:
c = [1,2,3,4,5,6,7,8]
My configuration file:
block
contents: 3
type: ...
scope ...
block
contents: 1
type: ...
scope ...
block
contents: 2
type: ...
scope ...
block
contents: 2
type: ...
scope ...
c.size is equal to the sum of the content number of every block.
I must split my array into 'n' arrays where n is the number of blocks I define in my config file and the size of every array is the number of contents defined in that block.
The result with the given array and config file is:
[1,2,3]
[4]
[5,6]
[7.8]
Any idea with good performance result?
A slight variant of Matt's answer:
If you read the values from the file into:
a = [3,1,2,2]
you can then do this:
a.each_with_object([]) {|e,b| b << c.shift(e)}
#=> [[1, 2, 3], [4], [5, 6], [7, 8]]
c = [1,2,3,4,5,6,7,8]
d = [3,1,2,2]
d.map { |n| c.shift n } # => [[1, 2, 3], [4], [5, 6], [7, 8]]
This destroys the original c.

Resources