using .delete_if and reject on elements in an array

M

Matt Beckley

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
 
G

Gavin Sinclair

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
 
R

Rajinder Yadav

Matt said:
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.
 
M

Matt Beckley

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
 
R

Robert Klemme

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
 
S

Simon Krahnke

* Robert Klemme said:
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 = a.fetch(i, 0) + 1
a
end

mfg, simon .... l
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top