Unsubscribe yourself from this mailing list. Why did you subscribe in the first place?
···
------------------------------------------------------------------
发件人:Sarkar Chinmoy<chinmoy_sarkar@yahoo.com>
日 期:2018年06月09日 23:55:04
收件人:Ruby users<ruby-talk@ruby-lang.org>
主 题:Re: ruby beginner help
Please remove me from your mailing list. Thanks
On Saturday, June 9, 2018, 10:49:52 AM CDT, Saumya jeet <promptc3.0@gmail.com> wrote:
This code works as expected, the trick is to get a new array like [3, 5, 57] without changing prime_ary.
Using' push' or '<<' changes the array even if you use 'each' iterator. Instead you can use '+' to join them
into a temp array append it to the solution array.
prime_ary = [[3, 5], [3, 7], [3, 11], [5, 11]]
add_ary = [57, 91, 109]n = add_ary.size
solution_ary = []
n.times do |i|
prime_ary.each do |j|
temp = j + [add_ary[i-1]]
solution_ary << temp
end
end
p solution_ary
On 9 June 2018 at 14:53, derrick <derrick@thecopes.me> wrote:
Hi, I have been trying to use flatten but I keep getting one big array instead of an array of arrays. I know that flatten can take an argument though. Also, product doesnt seem to scale all that well either. I was using product to combine 5 arrays of 800 prime numbers and it ate up all my memory and killed the script.
I haven't tried your exact method though let me try that first.
Thank you for the suggestion.
------------------------------ ------------------------------ ------
From:Paul Martensen <paul.martensen@gmx.de>
Time:2018 Jun 9 (Sat) 16:46
To:ruby-talk <ruby-talk@ruby-lang.org>
Subject:Re: ruby beginner help
Hey derrick!
You could use the Array#product and flatten methods like so:
prime_array.product(add_array) .map(&:flatten)
In short:
#product Gives you the cartesian product of two arrays so [[2, 4]].product([51, 12]) #=> [[[2,4], 51], [[2,4], 12]]
And map(&:flatten) takes each of these [[a,b], c] arrays and turns them into [a, b, c] arrays without flattening the whole
thing.
Hope this helps,
Paul
On 09.06.2018 10:19, derrick wrote:
I am having a lot of trouble wrapping my head around this
I have an array of arrays of prime numbers.
I want to add a each prime number to this array.
example
I have
prime_ary = [[3, 5], [3, 7], [3, 11], [5, 11]]
add_ary = [57, 91, 109]
I would like to get an array of arrays like this
[[3,5, 57], [3, 7, 57], [3, 11, 57], [5, 11, 57], [3, 5, 91], [3, 7, 91], [3, 11, 91] ... ]
There should be 12 sub arrays in the array at the end. I have this function in an array class but it doesn't give me the result I would like. I have been working on this for a few days and feel like I have loosing the ability to think clearly. Any help is appreciated.
def add_ary_prime(numb)
solution_ary = []
numb.each do |rannumb|
solution_ary.push(self.map {|arr| arr << rannumb})
end
solution_ary
end
p prime_ary.add_ary_prime(add_ ary)
sincerely,
Derrick
Unsubscribe: <mailto:ruby-talk-request@ ruby-lang.org?subject= unsubscribe>
<http://lists.ruby-lang.org/ cgi-bin/mailman/options/ruby- talk>
Unsubscribe: <mailto:ruby-talk-request@ ruby-lang.org?subject= unsubscribe>
<http://lists.ruby-lang.org/ cgi-bin/mailman/options/ruby- talk>
--
2nd Year UG
Electrical Engineering
IIEST Shibpur