Discussion:
ruby beginner help
derrick
2018-06-09 08:19:36 UTC
Permalink
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
Paul Martensen
2018-06-09 08:45:41 UTC
Permalink
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-***@ruby-lang.org?subject=unsubscribe>
> <http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
derrick
2018-06-09 09:23:22 UTC
Permalink
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 <***@gmx.de>
Time:2018 Jun 9 (Sat) 16:46
To:ruby-talk <ruby-***@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-***@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
Saumya jeet
2018-06-09 15:49:28 UTC
Permalink
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 <***@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 <***@gmx.de>
> Time:2018 Jun 9 (Sat) 16:46
> To:ruby-talk <ruby-***@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-***@ruby-lang.org?subject=unsubscribe> <ruby-talk-***@ruby-lang.org?subject=unsubscribe><http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk> <http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
>
>
>
>
> Unsubscribe: <mailto:ruby-talk-***@ruby-lang.org?subject=unsubscribe>
> <http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
>
>


--
2nd Year UG
Electrical Engineering
IIEST Shibpur
Sarkar Chinmoy
2018-06-09 15:55:04 UTC
Permalink
Please remove me from your mailing list. Thanks
On Saturday, June 9, 2018, 10:49:52 AM CDT, Saumya jeet <***@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 theminto 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 <***@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 <***@gmx.de>Time:2018 Jun 9 (Sat) 16:46To:ruby-talk <ruby-***@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 EngineeringIIEST Shibpur

Unsubscribe: <mailto:ruby-talk-***@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
derrick
2018-06-10 03:09:17 UTC
Permalink
Unsubscribe yourself from this mailing list. Why did you subscribe in the first place?




------------------------------------------------------------------
发件人Sarkar Chinmoy<***@yahoo.com>
日 期2018幎06月09日 23:55:04
收件人Ruby users<ruby-***@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 <***@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 <***@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 <***@gmx.de>
Time:2018 Jun 9 (Sat) 16:46
To:ruby-talk <ruby-***@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

Unsubscribe: <mailto:ruby-talk-***@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
derrick
2018-06-11 00:23:26 UTC
Permalink
Thank you, yes you are correct. I actually meant the algorithm using product in that way doesn't scale well. That is why I was writing for help, to find another algorithm. In the end 800 primes wasn't enough. I had to go up to 1500 primes but I have now found a solution for projecteuler#60. Thank you so much for helping.

Derrick

------------------------------------------------------------------
发件人Jörg W Mittag<ruby-***@joergwmittag.de>
日 期2018幎06月10日 21:50:17
收件人Ruby users<ruby-***@ruby-lang.org>
䞻 题Re: ruby beginner help

derrick <***@thecopes.me> wrote:
> 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.

This has nothing to do with the scalability of `product` or any
other possible solution. It is inherent in the problem:

There are 800^5 = 327680000000000 such combinations, each
containing 5 numbers, so you need to store 1638400000000000
numbers, assuming 64 Bit integers, you need almost 12 PiByte
(12200000 GiByte) of RAM to store them.

Even if you don’t store but only process them lazily one-by-one,
assuming you have 10 GHz CPU and can process one number at every
clock cycle, you need almost two days. And since you probably
don’t have a 10 GHz CPU, more like 3 GHz, and you will probably
need more like 100 clock cycles to process each number, a more
realistic estimate is 1 year and 9 months.

In other words: it doesn’t matter whether you use `product` or
something else. It doesn’t even matter whether you use Ruby, C,
or re-write the entire thing in hand-optimized machine code.

You need to change the algorithm.

Greetings,
Jörg.

Unsubscribe: <mailto:ruby-talk-***@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
derrick
2018-06-10 02:57:08 UTC
Permalink
Thank you for your answer Paul it worked great. I had to do some optimization to get rid of duplicates to keep from running out of ram but your solution was the answer.

Derrick


------------------------------------------------------------------
发件人Paul Martensen<***@gmx.de>
日 期2018幎06月09日 16:45:41
收件人<ruby-***@ruby-lang.org>
䞻 题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-***@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
Loading...