Ruby Arrays (Pushing Array Inside the same array)

I got two arrays A and B, A contains the KM and B contains value

A = [9,18,27,36,45,54,63,72,81,90,99,108,117,126]
B = [1,1,2,1,1,3,4,5,2,1,1,3,6,1]

I have to remove the repeated values and store the unique value from
here,

This is the desire

Output: A = [9,27,54,63,72,117,126]
B = [1,2,3,4,5,6,1]

But there is unique case in output in the last value of Array B, the
same value(1) already exits but it is still stored again.

The pattern which I tried to make is store the first value of array A
and B as base and start comparing
C = [9]
D = [1]

For first case I wrote if a[0]%c.reverse[0]==0 then check the value if
b|0| ==d|0| if both cases true ignore the value else store the new value
in C and D.

This pattern works but How to write code for it, this is what I tried:

(a.length)times do |aa|
(c.length).times do |cc|
if a[aa]%c[cc]==0 && b[aa]==d[cc]
puts “the value is already there”
else
c.push(aa) // I know if I do this it will start repeating
end
end
end

On Wed, May 16, 2012 at 5:22 PM, Joe S. [email protected] wrote:

Output: A = [9,27,54,63,72,117,126]
B = [1,2,3,4,5,6,1]

first try,

[[],[]].tap{|d,c| b.zip(a).group_by{|x,y| x}.map{|x,y| y.first}.each{|x,y| d<<x;
c<<y}}
=> [[1, 2, 3, 4, 5, 6], [9, 27, 54, 63, 72, 117]]

then handle the exception

kind regards -botp

On Wed, May 16, 2012 at 5:22 PM, Joe S. [email protected] wrote:

Output: A = [9,27,54,63,72,117,126]
B = [1,2,3,4,5,6,1]

second try,

[[],[]].tap{|d,c| b.zip(a).uniq{|x,y| x}.each{|x,y| d<<x; c<<y}}
=> [[1, 2, 3, 4, 5, 6], [9, 27, 54, 63, 72, 117]]

kind regards -botp

B = [1,2,3,4,5,6,1]

Here is one approach. I am not sure about your rules for the last
elements.
I also am not sure this hash will always do the trick. Maybe someone
knows.

a = [9,18,27,36,45,54,63,72,81,90,99,108,117,126]
b = [1,1,2,1,1,3,4,5,2,1,1,3,6,1]

h = Hash[*b.zip(a).reverse.flatten]
d = b.uniq #> [1, 2, 3, 4, 5, 6]
c = d.map{|y| h[y]} #> [9, 27, 54, 63, 72, 117]

Harry

The main problem here is the description of what is desired. A lot of
information has been left out. An analysis of the given input and output
arrays would seem to suggest the algorithm:

c << a[i] and d << b[i] if
a[i] is not a multiple of a[j] from any other element
&& b[i] != b[j]

Is there a pattern to the sequence of x values? Those given are
ascending multiples of 9.
What is the relationship of b[i] to a[i]?

The following ruby implementation performs the above algorithm with no assumption about the pattern or order of elements in array a, or the relationship of b[i] to a[i].

a, b, c, d =
[9, 18, 27, 36, 45, 54, 63, 72, 81, 90, 99, 108, 117, 126],
[1, 1, 2, 1, 1, 3, 4, 5, 2, 1, 1, 3, 6, 1],
[],
[]

(a.count - 1).times {|i|
(ai, bi) = a[i], b[i]
ej = (a.count - 1).times
loop do
begin
j = ej.next
(aj, bj) = a[j], b[j]
break if i != j and ai % aj == 0 and bi == bj
rescue
c << ai
d << bi
break
end
end
}
c << a[-1]
d << b[-1]

Output:

c: [9, 27, 54, 63, 72, 117, 126] d: [1, 2, 3, 4, 5, 6, 1]

It may not be the most concise usage of the Ruby syntax, but it gets the
job done without generating any extra array copies, and terminates the
inner loop once a reason is found to not push the current (ai, bi) pair.

If more were known about the pattern of values in a, or the relationship
of b[i] to a[i], a more efficient implementation could be created.

You might find it more friendly to Ruby to change the input values from
two separate arrays with the same length to one array with 2-element
pairs as elements. [[9, 1], [18, 1]…]

I hope you find that of some help.

P.S. If anyone can create the same implementation with more concise Ruby
syntax without generating any extra interim arrays, I’d love to see it.