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.
- 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
- In the root directory, run
- To run an anagram generator that finds the anagrams formed by a given input:
- In the root directory, run
$ ruby lib/get_anagrams.rb
- In the root directory, run
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.
- 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
- Two inputs do not form a palindrome if first input does not equal reverse of second.
- 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."
- Method creates an Anagram object with input string saved as the original string.
- Example Input: "desserts"
- Example Output: original = "desserts"
- 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."
- 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"]
- 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"]
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.
- 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
Class that handles determining whether inputs are Palindromes. Code adapted from my earlier Epicodus exercise here.
- 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
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.
Please contact [email protected] with questions, comments, or issues.
- Ruby
Copyright (c) 2017 Kelsey Langlois
This software is licensed under the MIT license.