-1

我有一个字符串数组dictionary和一个字符串target

dictionary = ['a', 'b', 'c', 'ab', 'abc']
target = 'abba'

我的目标是返回dictionary可以组成的单词组合target。它应该返回这样的东西

['a abc', 'a a b c', 'a ab c']

这就是我所拥有的:

def possible_combinations(dictionary, target)
        results = [] #eventually an array of results 
        i = 0 #to go through the dictionary index starting at 0
        t = 0 #to go through the target index starting at 0
        while i < dictionary.count #while 0 is less than the total index in dict
            while t < target.length 
                if dictionary[i] == target[t]#dict is not changing but target[t] is changing
                    puts 'I am ' + dictionary[i] + ' at DICT for now'
                    puts 'I am ' + target[t] + ' at t for now'
                    puts 'I match somewhere in target so I am added.'#dict[1] is not happening here.
                    # results.push(dictionary[i])
                    if results.empty? 
                        results.push(dictionary[i])
                        puts results
                    else
                        results = results[0] + ' ' + dictionary[i] #this is not entirely working?
                        puts results
                    end
                else
                    puts 'forget about me'
                end 
            t = t + 1
            end 
        i = i + 1
        end 
    end 

当我运行它时,我得到了这个:

I am a at DICT for now
I am a at t for now
I match somewhere in target so I am added.
a
forget about me
forget about me
I am a at DICT for now
I am a at t for now
I match somewhere in target so I am added.
a a

我注意到这种target[t]情况正在改变,但dictionary[i]没有。我不明白嵌套的while循环。我认为内部的while循环必须在它到达外部之前完成,所以dictionary[i]被卡住了。i我想对dictionaryand进行迭代target,所以我使用嵌套的 while 循环。

如果target = 'aaaba',我得到这个:

I am a at DICT for now
I am a at t for now
I match somewhere in target so I am added.
a
I am a at DICT for now
I am a at t for now
I match somewhere in target so I am added.
a a
I am a at DICT for now
I am a at t for now
I match somewhere in target so I am added.
a a
forget about me
I am a at DICT for now
I am a at t for now
I match somewhere in target so I am added.
a a

注意结果是如何卡在两个'a'而不是三个或四个上的?

4

3 回答 3

1

while您可以使用on eachthedictionaryeach_charon thetarget

dictionary.each do |word|
  target.each_char do |char|
    puts word, char
  end
end

当前循环的问题是您t = 0在两个循环之外进行初始化,因此您只target在内部while条件始终为假之前循环一次。如果您将该声明移到第一个 while 循环中,您将获得更类似于您期望的结果

于 2013-11-08T21:02:27.493 回答
0
ri Array.each
ri String.index

你正在以最不像红宝石的方式这样做。阅读有关 Enumerable 模块的章节。查找 String 支持的所有方法。几乎总有比在 Ruby 中使用 while 更好的方法。

http://ruby-doc.com/docs/ProgrammingRuby/

请注意,虽然 String.[1] 作品字符串是不可枚举的,如果你想枚举它,你最好将字符串拆分成一个字符数组。或者更好的是使用字符串搜索函数而不是直接比较。在您的代码中

字典[i] 是一个字符串

尽管

target[i] 是单个字符。

因此,当字典元素长于一个字符时,您的测试将永远不会相等。

于 2013-11-08T21:01:09.927 回答
0

这是编写程序的更类似于 Ruby 的方式:

def possible_combinations(dictionary, target)
  results = #eventually an array of results 
  dictionary.each do |d|
    str = ''
    target.each do |tl
      if d == t #dict is not changing but target[t] is changing
        puts 'I am ' + d + ' at DICT for now'
        puts 'I am at target ' + t + ' now'
        puts 'I match somewhere in target so I am added.' #dict[1] is not happening here.
        str = << ' ' unless str.empty?
        str << d
        puts results
      else
        puts 'forget about me'
      end 
    end
    results << str
  end 
end

完成这个翻译只用了几分钟。我主要删除了索引,所以迭代是在dictionarytarget对象的元素上。

于 2013-11-08T22:32:35.437 回答