Home > Net >  How to find an array of all the words in json that can be made by rearranging these letters(Ruby)
How to find an array of all the words in json that can be made by rearranging these letters(Ruby)

Time:11-05

I am new to ruby. How do I write a function that takes a find method that accepts a lower case and the find should return an array of all the words in dic.json that can be made by rearranging these letters. So if I input "ab" then the output should be ["ab", "ba"] from the JSON file below.

dic.json

[
"ab",
"ba",
"abc",
"acb",
"bac",
"bca",
"cab",
"cba"
]

This is what have so far

I used File read to access the JSON file and I have a function that can find permutations but I am not sure how to connect the two functions.

class LetterLocater
  def get_file_contents
    return File.read('dictionary.json').split
  end

   def permutation(letters)
    return [''] if letters.empty?

    chrs = letters.chars
    (0...letters.size).flat_map { |i|
      chr, rest = letters[i], letters[0...i]   letters[i 1..-1]
      permutation(rest).map { |sub|
        chr   sub
      }
    }
  end
end

a = LetterLocater.new
puts a.permutation(gets.chomp)                                                                                                                                       ```

CodePudding user response:

Instead of creating all permutations for various inputs, you could also group the words from the dictionary once by sorting their letters:

def sorted(str)
  str.chars.sort.join
end

# assume this was read from the JSON file
dict = %w[ab ba abc acb bac bca cab cba]

lookup_hash = dict.group_by { |word| sorted(word) }
#=> {
#     "ab"  => ["ab", "ba"],
#     "abc" => ["abc", "acb", "bac", "bca", "cab", "cba"]
#   }

Now it's almost trivial to find the permutations. You just have to fetch the values for the sorted input:

input = gets.chomp
puts lookup_hash[sorted(input)]

CodePudding user response:

In Ruby there is already Array#permutation that you can use to calculate all possible words.

letters = "ab" # example
permutations = letters.split(//).permutation.map(&:join)
#=> ["ab", "ba"]

And then there is 'Array#&' that returns only elements from an array that are present in another array.

words = ["ab", "ba", "abc", "acb", "bac", "bca", "cab", "cba"]
words & permutations
#=> ["ab", "ba"]

And you can use JSON.load(File.open('dictionary.json')) to load the JSON file into a Ruby array – as Schwern already wrote in his comment.

Now let's combine all these methods into one class

require 'json'

class LetterLocater
  attr_reader :words

  def initialize(dictionary)
    @words = JSON.load(File.open('dictionary.json'))
  end

  def permutation(letters)
    permutations = letters.split(//).permutation.map(&:join)
    words & permutations
  end
end

ll = LetterLocater.new('dictionary.json')

ll.permutation('ab')
#=> ["ab", "ba"]
ll.permutation('abc')
#=> ["abc", "acb", "bac", "bca", "cab", "cba"]

CodePudding user response:

def find_permutations_in_array(arr, str)
  chars = str.chars.sort
  arr.inject([]) do |res, word|
    res << word if word.size == str.size && word.chars.sort == chars
    res
  end
end
  •  Tags:  
  • ruby
  • Related