CIS 343

Ruby

Winter 2023

  1. Write a method to convert fahrenheit to celsius. The formula is c = 5/9(f - 32)
  2. Write a method to compute the distance between two points on the coordinate plane. Have your function take two parameters p1 abd p2. Each parameter should be a Hash with keys :x and :y.
  3. Write a method rectangle(width, height) that prints a rectangle of asterisks with the given width and height.
  4. Write a method that uses each to count the number of occurrences of item in an array.
  5. Now use the built-in Array#select method.
  6. Take an array of integers. Use Array#each to print the even integers.
  7. Now, use Array.select to filter and return only the even integers, then print them out.
  8. Given an array of symbol values representing an ensemble and a hash that maps instruments to sections, write a method called tabulate_sections that produces a hash that maps instrument sections to the number of instruments in that section of the ensemble. For example, given the input
    ensemble = [:piano, :clarinet, :oboe, :trumpet, :frenchhorn, :violin, :piano, :oboe, :cello] and
    section = { piano: :percussion, clarinet: :woodwind, oboe: :woodwind, flute: :woodwind, trumpet: :brass, frenchhorn: :brass, violin: :string, oboe: :woodwind, cello: :string, viola: :string, timpani: :percussion}
    the function should return {percussion: 2, woodwind: 3, brass: 2, strings: 2}.
  9. Repeat the previous exercise, but try to find the must "Rubyesque" solution (i.e., try chaining a bunch of functions together.) Consider methods like Hash#values, Array#uniq, Array#select, Array#map, and Array#reduce.
  10. Write your own version of Array#select.

Updated Monday, 3 April 2023, 9:46 AM

W3c Validation