Ruby Codes - Anagram

Here we have an array of words:

words =  ['demo', 'none', 'tied', 'evil', 'dome', 'mode', 'live',
          'fowl', 'veil', 'wolf', 'diet', 'vile', 'edit', 'tide',
          'flow', 'neon']

My teacher wrote a program that prints groups of words that are anagrams. Anagrams are words that have the same exact letters in them, but in a different order. The result should look something like this:

["demo", "dome", "mode"]
["neon", "none"]
(etc)

And here is the solution that our teacher showed us:

result = {}

words.each do |word|
  key = word.split('').sort.join
  if result.has_key?(key)
    result[key].push(word)
  else
    result[key] = [word]
  end
end

result.each do |k, v|
  puts "------"
  p v
end

I got a little confused about how this program works, for example, when this part was set up result[key].push(word)and the part where it says result[key] = [word]I know that this can be a problem, but can someone explain this decision line by line in a non-specialized term or in some then a mannequin like me would understand.

PS. Sorry, newbie here.

+4
source share
3 answers

. :

words.each do |word| #=>  iterate each word in words array. "word" variable will have value at a particular iteration
  key = word
  .split('') #=> splits word, if word is 'demo' on iteration then it will be: `['d', 'e', 'm', 'o']`
  .sort #=> sorts the splitted array, i.e. the array above will be: `['e', 'd', 'm', 'o']`
  .join #=> joins the array sorted in above operation, it will be: 'edmo'. Since this is last operation, it will be returned and saved in `key` variable
  if result.has_key?(key) #=> Check whether result(Hash) already has key: `edmo`, returns true if present
    result[key].push(word) #=> result['edmo'] will give an array value, which can push word in that array
  else #=> false when key is not present in result Hash.
    result[key] = [word] #=> then add key with an array such as: `result['edmo] = ['demo']`
  end
end

-:

result = Hash.new{|h, k| h[k] =[] } #=> if key does not exist then the default value will be an array.

, :

words.each do |word|
  key = word.split('').sort.join
  result[key] << word # no need to validate whether we have key in Hash or not
end

. , . :

require 'set'
result = Hash.new{|h, k| h[k] = Set.new }

.

+2

, .

{}, result[key] nil - .

01:  if result.has_key?(key)
02:    result[key].push(word)
03:  else
04:    result[key] = [word]
05:  end

, 01 , result hash, , result[key] , , .

result hash , 04 , , [word].


Ruby, - if-else:

words.each do |word|
  key = word.split('').sort.join
  result[key] ||= []
  result[key] << word
end
+2

, (key = word.split('').sort.join). , .

, neon none enno.

result. , . ().

() :

# If there is an entry in the hash for the current anagram key 
if result.has_key?(key)
  # Then add the current word in the array, together with all the
  # other anagrams that are already there (because they share the
  # same key)
  result[key].push(word)
else
  # If the key has not yet been created, then create it and assign
  # as a value a new array that only contains the current word.
  result[key] = [word]
end
+1

Source: https://habr.com/ru/post/1656782/


All Articles