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

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}

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.

Removing elements of an array that are evenly divisible by 3 [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
How can I remove elements from a given array of integers that are evenly divisible by 3? A new array should be returned and the original array should not be altered.
For example, if
array = [1,2,3,4,1,5,2,6,7,8,9,10,7,11,12,13,14,15]
the array
[1, 2, 4, 1, 5, 2, 7, 8, 10, 7, 11, 13, 14]
should be returned.
Just found it:
array.reject{|a| a % 3 == 0}

Getting an array for chart [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 6 years ago.
Improve this question
I had 10 items in total. I lost all of them in 3 days: 5 items on the 1st day, 3 items on the 2nd day, and 2 items on the last day. I need to get an array [5, 2, 0] of remaining items at the end of each day. How can I get the array, given total 10 and the array of lost items [5, 3, 2]?
[5, 3, 2].each_with_object([10]){|e, a| a.push(a.last - e)}.drop(1)
# => [5, 2, 0]
Know why you need the complication drop(1)? It is because, without it, the answer makes more logical sense. Your requirement is what was complicated.
[5, 3, 2].each_with_object([10]){|e, a| a.push(a.last - e)}
# => [10, 5, 2, 0]
The initial 10 represents the initial state.

Ruby difference in arrays [closed]

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]

Resources