E
es_
Hi,
I'm just getting to discover ruby, but I find it very nice programming
language. I just still don't understand how the "or" and "and" in
ruby...
I was playing with ruby and for example made a def to print Stem and
Leaf plot (for those who didn't have a statistics course or slept on
it, e.g. http://cnx.org/content/m10157/latest/)
Here is the Beta version of it:
class Array
def n ; self.size ; end
def stem_and_leaf(st = 1)
# if st != (2 or 5 or 10) then ; st = 1 ; end
k = Hash.new(0)
self.each {|x| k[x.to_f] += 1 }
k = k.sort{|a, b| a[0].to_f <=> b[0].to_f }
te_last = 999999999999999999999999999999999 # impossible number
puts " Stem and Leaf plot" + " N = " + self.n.to_s + "
Steps / " + st.to_s
st = 10 / st
k.each do |key, val|
te = (key / 10).to_i
on = (Math.sqrt((key - te * 10).to_i**2)).to_i
if te == te_last and on < st
val.times { print on.to_s }
te_last = te
elsif te == te_last and on > st and on < st + st
val.times { print on.to_s }
te_last = te
st += st
else
print "\n" + "%+6s" % te.to_s + " : "
val.times { print on.to_s }
te_last = te
end
end
end
end
It prints:
Stem and Leaf plot N = 15 Steps / 2
1 : 234
1 : 56789
2 : 019 # it shouldn't be like this
3 : 08 # it shouldn't be like this
4 : 07 # it shouldn't be like this
Or with different "steps" value:
Stem and Leaf plot N = 15 Steps / 5
1 : 23
1 : 4567
1 : 89
2 : 019 # it shouldn't be like this
3 : 08 # it shouldn't be like this
4 : 07 # it shouldn't be like this
(data: 12 13 14 15 16 17 18 19 20 21 29 30 38 40 47)
My questions:
/1 If I just wanted it to work with "st" values: 1, 2, 5, how should
the "or" statement look like? Ruby don't understand multiple or's (I
tried using different brackets is several places but it still doesn't
work).
/2 It feels like Ruby didn't understand 3 and's in the elsif part of
script. It don't understand the 3rd "and". Why? How to write those
"and" statements?
Thanks in advance : )
If anyone'd like to use the script feel free, I'd be just grateful to
if you cite this message as a source ; )
Greetz,
Timo
I'm just getting to discover ruby, but I find it very nice programming
language. I just still don't understand how the "or" and "and" in
ruby...
I was playing with ruby and for example made a def to print Stem and
Leaf plot (for those who didn't have a statistics course or slept on
it, e.g. http://cnx.org/content/m10157/latest/)
Here is the Beta version of it:
class Array
def n ; self.size ; end
def stem_and_leaf(st = 1)
# if st != (2 or 5 or 10) then ; st = 1 ; end
k = Hash.new(0)
self.each {|x| k[x.to_f] += 1 }
k = k.sort{|a, b| a[0].to_f <=> b[0].to_f }
te_last = 999999999999999999999999999999999 # impossible number
puts " Stem and Leaf plot" + " N = " + self.n.to_s + "
Steps / " + st.to_s
st = 10 / st
k.each do |key, val|
te = (key / 10).to_i
on = (Math.sqrt((key - te * 10).to_i**2)).to_i
if te == te_last and on < st
val.times { print on.to_s }
te_last = te
elsif te == te_last and on > st and on < st + st
val.times { print on.to_s }
te_last = te
st += st
else
print "\n" + "%+6s" % te.to_s + " : "
val.times { print on.to_s }
te_last = te
end
end
end
end
It prints:
Stem and Leaf plot N = 15 Steps / 2
1 : 234
1 : 56789
2 : 019 # it shouldn't be like this
3 : 08 # it shouldn't be like this
4 : 07 # it shouldn't be like this
Or with different "steps" value:
Stem and Leaf plot N = 15 Steps / 5
1 : 23
1 : 4567
1 : 89
2 : 019 # it shouldn't be like this
3 : 08 # it shouldn't be like this
4 : 07 # it shouldn't be like this
(data: 12 13 14 15 16 17 18 19 20 21 29 30 38 40 47)
My questions:
/1 If I just wanted it to work with "st" values: 1, 2, 5, how should
the "or" statement look like? Ruby don't understand multiple or's (I
tried using different brackets is several places but it still doesn't
work).
/2 It feels like Ruby didn't understand 3 and's in the elsif part of
script. It don't understand the 3rd "and". Why? How to write those
"and" statements?
Thanks in advance : )
If anyone'd like to use the script feel free, I'd be just grateful to
if you cite this message as a source ; )
Greetz,
Timo