Git Product home page Git Product logo

anagrams's Introduction

Anagrams

Epicodus Project in Ruby, September 8, 2017

By Kelsey Langlois

Description

A Ruby script with an anagram generator and methods to evaluate whether two inputs are anagrams, palindromes, or antigrams. Two strings are anagrams if they share all the same letters, palindromes if the first string is the reverse of the second, and antigrams if they share no letters.

Setup/Installation Requirements

  • Clone this repository
  • To run an anagram tester that determines whether two inputs are anagrams, palindromes, or antigrams:
    • In the root directory, run $ ruby lib/test_anagrams.rb
  • To run an anagram generator that finds the anagrams formed by a given input:
    • In the root directory, run $ ruby lib/get_anagrams.rb

Specifications

Anagram

The main class for generating anagrams, and determining if two strings are anagrams, palindromes, or antigrams. Can create a specific instance with a set original string, or use class method which takes two strings and checks them.

Class Methods

are_anagrams(string1, string2)

  • Method returns "These words are not anagrams." if two words are not anagrams.
    • Example Input: "Bob" "Potato"
    • Example Output: "These words are not anagrams."
  • Method returns "These words are anagrams." if two words have the same letters.
    • Example Input: "ruby" "bury"
    • Example Output: "These words are anagrams."
  • Method determines if two words are anagrams regardless of case.
    • Example Input: "Ruby" "BURY"
    • Example Output: "These words are anagrams."
  • Method returns "These words form a palindrome." if two words form a palindrome.
    • Two inputs do not form a palindrome if first input does not equal reverse of second.
      • Example input: "cat" "act"
      • Example output: false
    • Two inputs forma a palindrome if first input equals the reverse of the second.
      • Example input: "diva" "avid"
      • Example output: true
    • Method ignores case when checking if word is a palindrome.
      • Example input: "Avid" "DIVA"
      • Example output: true
  • Method returns an error if either input is not a word (as determined by Dictionary.is_word?).
    • Example Input: "Rrgh" "Argh"
    • Example Output: "Error: Inputs must consist of real words."
  • Method returns "These words are antigrams (no letter matches)." if two words have no matching letters
    • Example Input: "Bob" "Cat"
    • Example Output: "These words are antigrams (no letter matches)."
  • Method does all of the above for two inputs of one or more words.
    • Example Input: "eat desserts" "stressed tea"
    • Example Input: "These phrases are anagrams."
  • Method returns an error if either input phrase has a word that is not a word (as determined by Dictionary.is_word?).
    • Example Input: "eat brains" "ggggrrr arrrgh"
    • Example Output: "Error: Inputs must consist of real words."
  • Method ignores punctuation and spaces when evaluating inputs as anagrams, antigrams, or palindrome.
    • Example Input: "Eat desserts!" "Stressed, tea."
    • Example Output: "These phrases are anagrams."

new(string)

  • Method creates an Anagram object with input string saved as the original string.
    • Example Input: "desserts"
    • Example Output: original = "desserts"

Instance Methods

anagram_of(string)

  • Method determines whether original string of Anagram object and inputted string are anagrams, palindromes, or antigrams, following all specifications listed for are_anagrams.
    • Example Input: a has orginal string = "eat desserts", input string = "stressed tea"
    • Example Output: "These phrases are anagrams."

get_anagrams

  • Method returns array with empty string if original string is empty.
    • Example Input: ""
    • Example Output: [""]
  • Method returns array containing original string as is if original string is one character.
    • Example Input: "a"
    • Example Output: ["a"]
  • Method returns array containing all possibly combinations of original strings characters if string is longer than one character.
    • Example Input: "abc"
    • Example Output: ["abc", "acb", "bac", "bca", "cab", "cba"]
  • Method ignores case when finding anagrams.
    • Example Input: "AbC"
    • Example Output: ["abc", "acb", "bac", "bca", "cab", "cba"]
  • Method ignores non-alphabetical characters (including spaces) in original string when finding anagrams.
    • Example Input: "I'd a"
    • Example Output: ["ida", "iad", "dia", "dai", "aid", "adi"]

get_anagram_words

  • Method returns empty array if there are no anagrams of input that Dictionary.is_words recognizes as words.
    • Example Input: ""
    • Example Output: []
  • Method returns all single-word anagrams formed from input that Dictionary.is_words? recognizes as words.
    • Example Input: "abc"
    • Example Output: ["bac", "cab"]

Dictionary

Custom Dictionary class for determining if the words given to Anagrams as input are words. Currently has a few very basic rules to account for variations on words existing in the file.

Class Methods

is_word?

  • Method returns false if word is an empty string.
    • Example Input: ""
    • Example Output: false
  • Method returns false if word is not in words file.
    • Example Input: "rrrrrrgh"
    • Example Output: false
  • Method returns true if word is in words file.
    • Example Input: "hello"
    • Example Output: true
  • Method ignores case when checking word.
    • Example Input: "paris"
    • Example Output: true
  • Method returns true if word is equal to a word in words file + "s".
    • Example Input: "zombies"
    • Example Output: true
  • Method returns true if word is equal to a word in words file + "ed".
    • Example Input: "stressed"
    • Example Output: true
  • Method returns true if word is equal to a word in words file + "est".
    • Example Input: "tallest"
    • Example Output: true

Palindrome

Class that handles determining whether inputs are Palindromes. Code adapted from my earlier Epicodus exercise here.

Class Methods

are_palindrome?(string1, string2)

  • Method returns false if first input does not equal reverse of second.
    • Example Input: "cat", "act"
    • Example Output: false
  • Method returns true if first input equals the reverse of the second.
    • Example Input: "diva", "avid"
    • Example Output: true
  • Method ignores case when checking if word is a palindrome
    • Example Input: "Diva" "AVID"
    • Example Output: true

Known Bugs

Dictionary currently has a fairly limited is_word? method. This means that are_anagrams may incorrectly state that inputs did not consist of words, especially if the words were less common English words. Adding these words to words.txt will let Dictionary recognize them. Contractions will not be recognized except for 's, and need to be added to words.txt without apostrophes.

Support and contact details

Please contact [email protected] with questions, comments, or issues.

Technologies Used

  • Ruby

License

Copyright (c) 2017 Kelsey Langlois

This software is licensed under the MIT license.

anagrams's People

Contributors

langlk avatar

Watchers

 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.