如何简化或清理这个anagram方法?

时间:2022-08-24 04:00:42

I have a method here that takes an array of strings and groups the ones that are anagrams of each other together, with each group forming a sub-array of the main anagram_groups array.

我这里有一个方法,它接受一个字符串数组并将彼此的字符串组合在一起,每个组形成主anagram_groups数组的子数组。

The output is fine but I feel like my code is probably overly-complicated. How could my logic and/or syntax be simplified, short of refactoring things into more methods?

输出很好,但我觉得我的代码可能过于复杂。如何简化我的逻辑和/或语法,而不是将事情重构为更多方法?

def combine_anagrams(words)
  anagram_groups = []
  # For each word in array argument
  words.each do |word|

    # Tracking variable for the word
    word_added = false

    anagram_groups.each do |group|
      # Check if word already exists (prevents duplicates)
      if group.include? word
        word_added = true
      # Add word to group if it is an anagram of the first string in the group
      elsif word.downcase.chars.sort == group[0].downcase.chars.sort
        group << word
        word_added = true        
      end
    end

    # If word was not an anagram of anything, create new group (subarray)
    unless word_added
      anagram_groups << [word]
      word_added = true
    end

  end
  return anagram_groups
end

This is an array of words for testing:

这是一系列用于测试的单词:

test_words = ['cars', 'for', 'potatoes', 'racs', 'four', 'scar', 'creams', 'scream']

2 个解决方案

#1


3  

test_words.group_by{|w| w.each_char.sort}.values

would give

会给

[
  ["cars", "racs", "scar"],
  ["for"],
  ["potatoes"],
  ["four"],
  ["creams", "scream"]
]

#2


0  

I modified sawa's answer slightly in order to ignore case and make sure there's no duplicate values:

我稍微修改了sawa的答案,以便忽略大小写并确保没有重复的值:

test_words.group_by{|w| w.downcase.each_char.sort}.values.each{|v| v.uniq!}

I realize this will still give duplicates in the output if the words have characters with different cases, but that's fine for my purposes. Now I'm all sorted, thanks!

我意识到如果单词具有不同情况的字符,这仍然会在输出中给出重复项,但这对我的目的来说很好。现在我整理好了,谢谢!

#1


3  

test_words.group_by{|w| w.each_char.sort}.values

would give

会给

[
  ["cars", "racs", "scar"],
  ["for"],
  ["potatoes"],
  ["four"],
  ["creams", "scream"]
]

#2


0  

I modified sawa's answer slightly in order to ignore case and make sure there's no duplicate values:

我稍微修改了sawa的答案,以便忽略大小写并确保没有重复的值:

test_words.group_by{|w| w.downcase.each_char.sort}.values.each{|v| v.uniq!}

I realize this will still give duplicates in the output if the words have characters with different cases, but that's fine for my purposes. Now I'm all sorted, thanks!

我意识到如果单词具有不同情况的字符,这仍然会在输出中给出重复项,但这对我的目的来说很好。现在我整理好了,谢谢!