using .delete_if and reject on elements in an array

View: New views
8 Messages — Rating Filter:   Alert me  

using .delete_if and reject on elements in an array

by Matt Beckley :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Working on code to fill an array with 200 random integer elements. The
elements range from 1-100 in value. Next I use the .sort method to see
how many values fall between 1-10 to visually check how many random
numbers will fall between 1-10. Now I wanna take my array and apply a
method to it that will check which elements are less 21 but more then
10. For you math types 10<x<21. How do I accomplish that with ruby. Do I
accomplish it by doing it this way or am I overlooking a better method
for evaluating and comparing the elements in my array? I think I got the
right method but the block is wrong?

a = nums1.reject {|x| x<10 && x>21}

Heres what I have so far of my program:
********************************************************************************
nums = []
200.times do
  nums << rand(100)
end

nums1 = nums.sort
nums1.each {|n| print n, "," }

********************************************************************************

Please include the code so I can try it out on my editor and get it
figured out!
Thanks,
Matt
--
Posted via http://www.ruby-forum.com/.


Re: using .delete_if and reject on elements in an array

by Gavin Sinclair :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

> I think I got the
> right method but the block is wrong?
>
> a = nums1.reject {|x| x<10 && x>21}

A number can't be both less than 10 and greater than 21, so your block
won't match anything in the array.

Replace && with || (or better still, to my eyes, "or").

Also, "reject" seems so _negative_.  Why not be positive? ;)

  nums1.select { |x| (10..21).include? x }

Some other suggestions for your code:

> nums = []
> 200.times do
>   nums << rand(100)
> end

  nums = (1..200).map { rand(100) }

> nums1 = nums.sort
> nums1.each {|n| print n, "," }

You don't really need a new variable, do you?

  nums.sort.each do |n| puts n end

or just

  puts nums.sort


--
Gavin Sinclair


Re: using .delete_if and reject on elements in an array

by Rajinder Yadav-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Matt Beckley wrote:
> Now I wanna take my array and apply a
> method to it that will check which elements are less 21 but more then
> 10. For you math types 10<x<21.
>
> a = nums1.reject {|x| x<10 && x>21}

What you want is:

a = nums.find_all { |n| n > 10 && n < 21 }

OR using Range

a = nums.find_all { |n| (11..20) === n }

Generating random number

>   nums << rand(100)

rand(100) will generate a random number from 0 to 99,
what you want to do is

nums << rand(100)+1  # random number from 1 to 100


> Thanks,
> Matt


--
Kind Regards,
Rajinder Yadav

http://DevMentor.org

Do Good! - Share Freely, Enrich and Empower people to Transform their lives.


Re: using .delete_if and reject on elements in an array

by Matt Beckley :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Thanks for the pointers, this sure is a good community/forum!

I appreciate both inputs, while I learn Ruby. The && and or make better
sense now. I'll be looking at the .map method. Also didn't know you
could apply multiple methods in one line, very neat. Thanks for the
help!

Matt
--
Posted via http://www.ruby-forum.com/.


Re: using .delete_if and reject on elements in an array

by Robert Klemme-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On 10/25/2009 03:57 AM, Matt Beckley wrote:

> Working on code to fill an array with 200 random integer elements. The
> elements range from 1-100 in value. Next I use the .sort method to see
> how many values fall between 1-10 to visually check how many random
> numbers will fall between 1-10. Now I wanna take my array and apply a
> method to it that will check which elements are less 21 but more then
> 10. For you math types 10<x<21. How do I accomplish that with ruby. Do I
> accomplish it by doing it this way or am I overlooking a better method
> for evaluating and comparing the elements in my array? I think I got the
> right method but the block is wrong?
>
> a = nums1.reject {|x| x<10 && x>21}
>
> Heres what I have so far of my program:
> ********************************************************************************
> nums = []
> 200.times do
>   nums << rand(100)
> end
>
> nums1 = nums.sort
> nums1.each {|n| print n, "," }
>
> ********************************************************************************
>
> Please include the code so I can try it out on my editor and get it
> figured out!

If I understand your requirement properly you want to sort numeric
values into buckets.  Here is one way that you can do it, assuming your
buckets are 10 numbers wide each:

nums ...
buckets = Hash.new 0

nums.each do |x|
   buckets[x - x % 10] += 1
end

buckets.sort.each do |b,count|
   printf "%4d %5d\n", b, count
end

If you have different alignments of buckets the code in the block of
course becomes more complicated.

Kind regards

        robert

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/


Re: using .delete_if and reject on elements in an array

by David A. Black :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi --

On Sun, 25 Oct 2009, Rajinder Yadav wrote:

> Matt Beckley wrote:
>> Now I wanna take my array and apply a
>> method to it that will check which elements are less 21 but more then
>> 10. For you math types 10<x<21.
>>
>> a = nums1.reject {|x| x<10 && x>21}
>
> What you want is:
>
> a = nums.find_all { |n| n > 10 && n < 21 }
>
> OR using Range
>
> a = nums.find_all { |n| (11..20) === n }

You can also do:

   a = nums.grep(11..20)

which is a wrapper for a bunch of calls to ===.


David

--
The          Ruby training with D. Black, G. Brown, J.McAnally
Compleat     Jan 22-23, 2010, Tampa, FL
Rubyist      http://www.thecompleatrubyist.com

David A. Black/Ruby Power and Light, LLC (http://www.rubypal.com)


Re: using .delete_if and reject on elements in an array

by Simon Krahnke :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

* Robert Klemme <shortcutter@...> (2009-10-25) schrieb:

> buckets = Hash.new 0
>
> nums.each do |x|
>   buckets[x - x % 10] += 1
> end

Why not just

buckets = num.inject([]) do | a, x |
  i = x.div 10
  a[i] = a.fetch(i, 0) + 1
  a
end

mfg,                  simon .... l


Re: using .delete_if and reject on elements in an array

by Christian Bradley :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

One liner...
b=(a=Array.new(200).collect{rand(100)+1}).grep(11..21)

From IRB:

irb(main):046:0> b=(a=Array.new(200).collect{rand(100)+1}).grep(11..21)
=> [14, 21, 15, 12, 18, 15, 12, 17, 20, 21, 19, 19, 14, 21, 17, 15, 18,
20, 17, 11, 17, 11, 19, 12]
irb(main):047:0> a
=> [90, 76, 14, 84, 66, 37, 77, 92, 46, 24, 79, 46, 87, 90, 21, 90, 59,
48, 62, 67, 55, 33, 58, 47, 23, 4, 95, 76, 66, 95, 66, 10, 49, 36, 75,
74, 53, 99, 30, 15, 58, 26, 77, 95, 6, 89, 92, 43, 55, 54, 38, 12, 41,
18, 6, 15, 12, 9, 58, 36, 17, 40, 8, 42, 68, 26, 71, 2, 3, 92, 20, 46,
84, 95, 21, 90, 74, 31, 4, 59, 49, 36, 45, 52, 100, 98, 32, 25, 70, 3,
69, 19, 45, 7, 46, 19, 34, 27, 82, 85, 59, 82, 94, 7, 4, 14, 21, 41, 29,
88, 26, 42, 83, 33, 27, 42, 61, 72, 17, 66, 66, 73, 68, 96, 15, 5, 6,
49, 18, 23, 72, 90, 84, 96, 46, 83, 44, 10, 75, 20, 43, 17, 63, 39, 25,
3, 88, 81, 11, 7, 62, 24, 62, 22, 17, 72, 77, 82, 64, 11, 2, 50, 50, 19,
40, 10, 93, 52, 4, 66, 23, 70, 3, 62, 44, 83, 38, 76, 53, 97, 48, 12,
68, 64, 70, 77, 77, 49, 72, 34, 94, 94, 99, 79, 31, 73, 85, 59, 46, 64]
irb(main):048:0> b
=> [14, 21, 15, 12, 18, 15, 12, 17, 20, 21, 19, 19, 14, 21, 17, 15, 18,
20, 17, 11, 17, 11, 19, 12]
--
Posted via http://www.ruby-forum.com/.