Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ assignment_ruby_warmup
Dice, dice, baby.

[A Ruby assignment from the Viking Codes School](http://www.vikingcodeschool.com)
by Avonyel (Stephanie)
31 changes: 31 additions & 0 deletions anagrams.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
def anagrams(input_string)
input_string = input_string.downcase
dictionary = []
anagrams = []
input_hash = {}
input_hash.default = 0
is_anagram = false

dictionary_source = File.open("enable.txt", "r")
dictionary_source.each {|line| dictionary << line.strip}

input_string.split("").each {|char| input_hash[char] += 1}

dictionary = dictionary.select {|word| word.length == input_string.length}

dictionary.each do |word|
input_hash.each do |char, char_count|
if char_count == word.count(char)
is_anagram = true
else
is_anagram = false
break
end
end
if is_anagram
anagrams << word unless word == input_string
end
end

anagrams
end
Loading