Latin Roots: Prefixes

Published by Helen Yu in

Write a Prefix class. The class should have a single getter: prefixes, which is initialized as an empty hash. Each (key, value) pair in this hash will be a prefix coupled with the words that begin with this prefix.

The Prefix class should have three instance methods:

  • add_prefixes: Takes in a variable list of prefixes as input.
  • add_words: Takes in a variable list of words as input.
  • sort: Sorts the prefix-word groups in lexicographic order by prefix.

To illustrate, if we create an instance of the Prefix class:

Examples

prefix_list = Prefix.new
prefix_list.add_prefixes("auto", "aqua", "amphi")
prefix_list.add_words("automate", "automobile", "automotive", "amphibian", "aquarium")
prefix_list.sort

prefix_list.prefixes
# {"amphi"=>["amphibian"],
# "aqua"=>["aquarium"],
# "auto"=>["automate", "automobile", "automotive"]}

Notes

Words for each prefix-word key-value pair should be in the same order as the input.

Watch a quick demo on how Edabit works.