Calculation of heights

Hi All

The question I have is how to load a hash where the key is not numeric
or does not follow a cycle. I’m doing an exercise where I have to go
asking people’s names and their corresponding heights, these the income
each in a different array, and then deposit them into a hash and get
over 2 meters, 1.60 meters minors, etc. The problem is how to charge the
hash data arrays. Here is the code:

##############################################################

heights=Hash.new
a=[]
b=[]
r=0
i=0

puts “calculation of heights”
while r!=2
puts "enter name: "
a[i]=gets.chomp.to_s
puts "enter height: "
b[i]=gets.chomp.to_f
i=i+1
puts "want to perform other income: 1-> yes, 2-> no: "
r=gets.chomp.to_i
puts “”
end

for i in 1…a.length
for j in 1…b…length
alturas[i]=a[i]
alturas[j]=b[i]
end
end

######################################################################

is not applicable in Ruby using for loops, but not the only language I
use then I get a little tricky to get used.

not if you understand the problem.

Thanks.

Hi,

so the question in your other thread has been answered?

The problem in this case, again, is that you somehow don’t see the
obvious solution but instead do all kinds of complicated things with
nested loops and such.

Why do you need the intermediate arrays when you could simply store the
value in the hash? Also be careful with variable naming. There is no
“alturas”. Just stick with English.

So dump the arrays and loops and simply save the height directly in the
hash with the name as the key:

heights = {}

name = gets.chomp
height = gets.chomp
heights[name] = height

That’s it.

If you do want to use the arrays for whatever reason, you mustn’t use a
nested loop. This will give you each combination of a name and a height.
But what you actually want is the first element of each array, then the
second etc. In other words: You must iterate over the arrays parallely.
Like this, for example:

a.each_with_index do |name, i| # pass an index for each element

get height from other array using the index

height = b[i]
heights[name] = height
end

Am 04.10.2012 19:26, schrieb Joao S.:

heights=Hash.new
a=[]
b=[]
r=0
i=0

puts “calculation of heights”
while r!=2

  • you should use more descriptive variable names

puts "enter name: "
a[i]=gets.chomp.to_s

  • gets.chomp is already a string
  • you can push a new element to an array with <<,
    so that you do not need an index i

puts "enter height: "
b[i]=gets.chomp.to_f
i=i+1
puts "want to perform other income: 1-> yes, 2-> no: "
r=gets.chomp.to_i
puts “”

you do not need the empty string

end

names = []
heights = []

puts “calculation of heights”

answer = nil
while answer != ‘n’
print 'Enter name: ’
names << gets.chomp
print 'Enter height: ’
heights << gets.to_f

print 'Again? [y/n] ’
answer = gets.chomp
puts
end

On Thu, Oct 4, 2012 at 7:26 PM, Joao S. [email protected] wrote:

The question I have is how to load a hash where the key is not numeric
or does not follow a cycle. I’m doing an exercise where I have to go
asking people’s names and their corresponding heights, these the income

The term “income” has a meaning not applicable here:
http://www.merriam-webster.com/dictionary/income

each in a different array, and then deposit them into a hash and get
over 2 meters, 1.60 meters minors, etc. The problem is how to charge the
hash data arrays. Here is the code:

The program does not do any calculations. What are you trying to
achieve? Do you want to ouput all names of people who are less than
1.6m and over 2m? That would be a selection of the data which I
cannot see in your program.

Example

irb(main):006:0> heights = {“foo”=>1.0, “bar”=>1.8, “baz” => 2.1}
=> {“foo”=>1.0, “bar”=>1.8, “baz”=>2.1}
irb(main):007:0> puts heights.select {|n,h| h < 1.6}.map {|n, h| n}
foo
=> nil
irb(main):008:0> puts heights.select {|n,h| h < 2}.map {|n, h| n}
foo
bar
=> nil
irb(main):009:0> puts heights.select {|n,h| h >= 2}.map {|n, h| n}
baz
=> nil

is not applicable in Ruby using for loops, but not the only language I
use then I get a little tricky to get used.

not if you understand the problem.

You lost me here completely. What are you trying to say?

Kind regards

robert

Just keep in mind that all (programming) languages are different, and
the
solutions to a single problem must necessarily be different in those
languages.

Writing a java program using code that works in C is just as “wrong.”

It just so happens that ruby provides a much more “functional” standard
library, so where creating or analysing a java-esque solution will help
your understanding for similar languages (C++, C#, etc.) you’d still
need
to come up with a different algorithmic approach to solve it in a purely
procedural language like assembly, and yet another for a more functional
language like ruby.

PS stay away from lisp

Robert K. wrote in post #1078745:

is not applicable in Ruby using for loops, but not the only language I
use then I get a little tricky to get used.

not if you understand the problem.

You lost me here completely. What are you trying to say?

Kind regards

robert

What I’m trying to say is that whenever I post some code, which use a
conventional structure, such as a for loop, a while loop, etc, I
responded
to my something like this is Ruby and is not C, C++ or Java, and
some works that utilize own Ruby (ruby way), the problem is that these
functions only work in ruby and whenever you use another language, I
have to change the way I program certain functions.

Sorry for my English. :slight_smile:

On Sat, Oct 6, 2012 at 6:37 AM, Matthew K. [email protected]
wrote:

language like assembly, and yet another for a more functional language like
ruby.

Absolutely agree! Joao, also consider this: if all programming
languages would be usable in the same way there would not be different
programming languages at all. The whole point of having so many of
them is that they all have a different set of features which make
solving some types of problems easier than others. There is simply no
programming language which fits all kinds of problems equally well.
you better get used to the specific ways to solve problems in the
languages you use. Please do not expect all approaches to work in all
languages equally well. You’re not using a saw and a drill the same
way, do you?

Kind regards

robert

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs