Code Monkey home page Code Monkey logo

phase-3-ruby-oo-basics-anagram-detector's Introduction

Bonus: Anagram Detector Lab

Learning Goals

  • Build a class that uses an initialize method
  • Use an attribute accessor macro to make an attribute available to a class's methods
  • Practice using Ruby Array methods

Description

You will write a program that, given a word and a list of possible anagrams, selects the correct one(s).

Your class, Anagram should take a word on initialization; instances should respond to a #match instance method that takes an array of possible anagrams. It should return all matches in an array. If no matches exist, it should return an empty array.

In other words, given: "listen" and %w[enlists google inlets banana] the program should return ["inlets"].

listen = Anagram.new("listen")
listen.match(%w[enlists google inlets banana])

# => ["inlets"]

Top Tip: %w[some words] is simply a shortcut for instantiating an array of strings!

Instructions

Once again, this lab is test-driven. Run the test suite to get started. Remember that you can use the learn --fail-fast option to run the tests only up until the first failure, in order to work progressively through the tests.

Write your solution in anagram.rb.

This is a difficult lab that will require some algorithmic thinking! Try breaking writing out some pseudocode and break the problem down into smaller steps before writing out your implementation.

Hints:

How will you determine if one word is an anagram for another?

You'll need to iterate over the array of words that the #match method takes as an argument. You will compare each word of that array to the word that the Anagram class is initialized with.

To determine one word is an anagram of another word, try determining if they are composed of the same letters. You can use the #chars method on a string to get an array of its individual letters:

"hello".chars
# ["h", "e", "l", "l", "o"]

You can compare two arrays using the ==. For example:

[1, 2, 3] == [1, 2, 3]
# => true

[1, 3, 2] == [1, 2, 3]
# => false

Two arrays are equal if they contain the same elements, in the same order. Remember that you can #sort an array's elements. This will help in your comparison:

[1, 3, 2].sort == [3, 2, 1].sort
# => true

Resources

phase-3-ruby-oo-basics-anagram-detector's People

Contributors

maxwellbenton avatar lizbur10 avatar aviflombaum avatar kthffmn avatar loganhasson avatar sarogers avatar ahimmelstoss avatar ihollander avatar benjagross avatar flatiron-bot avatar fislabstest avatar gj avatar deniznida avatar annjohn avatar fs-lms-test-bot avatar drewprice avatar howardbdev avatar jmburges avatar sophiedebenedetto avatar babyshoes avatar louisasm avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.