home / study / engineering / computer science / computer science questions and a
ID: 3736457 • Letter: H
Question
home / study / engineering / computer science / computer science questions and answers / ruby programming write a program using the given fraction class that asks the user to enter ...
Question: Ruby Programming Write a program using the given fraction class that asks the user to enter a num...
Ruby Programming
Write a program using the given fraction class that asks the user to enter a number and calculates the harmonic sum up to that number.
hn = 1/1 + 1/2 + 1/3 + · · · + 1/(n 1 )+ 1/n (i.e. h5 = 137/60)
If the user enters 5, the program should calculate and display h1,h2,h3,h4, and h5 like
$What harmonic sums do you wish to calculate? 5
h1: 1/1
h2: 3/2
h3: 11/6
h4: 25/12
h5: 137/60
Given Fraction Class:
class Fraction
attr_accessor :numerator, :denominator
# constructor: initialize with a numerator and denominator
# parameters: a numerator and denominator
# return value: n/a
def initialize(n, d)
@numerator = n
@denominator = d
end
# +: syntactic sugar to sum a fraction and another value
# parameters: a fraction object as the rightmost operand
# return value: a new fraction object containing the sum
def +(rhs)
# calculate the values of the numerator and denominator
n = @numerator*rhs.denominator + @denominator*rhs.numerator
d = @denominator*rhs.denominator
# reduce the fraction to the lowest common term
n, d = reduce(n, d)
return Fraction.new(n, d)
end
# print: displays the fraction in the normal way
# parameters: none
# return value: none
def print
puts "#{@numerator} / #{@denominator}"
end
# reduce: reduces a fraction to the lowest common denominator
# parameters: a numerator and denominator
# return value: the reduced numerator and denominator
def reduce(n, d)
r = gcd(n, d)
return n / r, d / r
end
# gcd: find the greatest common divisor using Euclid's algorithm
# parameters: two integers
# return value: the greatest common divisor
def gcd(a, b)
if a % b == 0
return b
else
return gcd(b, a % b)
end
end
private :reduce, :gcd
end
# Write your code here.
Explanation / Answer
You pass arrays around, and you find yourself calling for x[1] and x.last - this is very unreadable, and brittle.
Ruby allows you to implicitely assign elements of the array to parameters in your block according to its location, so you could write the same "maps" like this:
Note that unneeded array elements at the end of the array are truncated, so you don't have to write them. For the unneeded elements at the beginning of the array I've used the _ idiom which tells the method that I expect these parameters, but I won't need them.
If it deserves a comment - it deserves a method
Instead of commenting your code, consider refactoring it into methods. Each method will do one thing, and, given the proper name, will be descriptive when used, and easily read in itself:
Make your steps logical
The last few steps you take look like they are held out with gum, and that you made some acrobatics to stay one-line and "idiomatic". It is better to make your methods do something you can explain, than simply adding and removing temp-variables:
Update
Yes, it seems that using &method(:something) in map and multiple assignment are mutually exclusive... :(
I can suggest a few strategies where you don't have to fall back to using pair.first, pair.last:
Use multiple assignment in the first line of your method:
Use a hash to hold your values, and use keyword arguments in your method
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.