循环查找匹配项

时间:2018-10-24 03:48:20

标签: ruby-on-rails ruby-on-rails-5

我有一个循环,可抓取用户输入的号码(no_reps)并根据该号码插入某些信息。

例如,如果用户输入数字3,则r.repetition_index中将插入1到3之间的数字。

我要做的是为每个循环匹配每个唯一的数字,并插入treatment_indexes变量,因此对于每个r.repetition_index,它不会重复相同的数字。

例如,我的表结果将是这样,不包括treatment_index。

|id|treatment_selection_id|repetition_index|treatment_index|
|1 |         1            |       1        |               |
|2 |         1            |       2        |               |
|3 |         1            |       3        |               |
|4 |         2            |       1        |               |
|5 |         2            |       2        |               |
|6 |         2            |       3        |               |
|7 |         3            |       1        |               |
|8 |         3            |       2        |               |
|9 |         3            |       3        |               |

请注意每个treatment_selection_id的唯一重复索引为1..3。现在,对于每个等于1的repetition_index,我想在treatment_index中插入一个唯一的数字1..3,以此类推,以repetition_index 2和3表示。

到目前为止,我有以下内容,但是r.treatment_index并没有为每个匹配的r.repetition_index插入唯一编号。

no_reps = @trial.number_of_repetitions
@trial.treatment_selections.each do |r|
 r.repetitions.in_groups_of(no_reps).each_with_index do |a, i|
   treatment_indexes = (1..no_reps).to_a.shuffle
   a.each_with_index do |r, j|
     r.repetition_index = j + 1
     r.treatment_index = treatment_indexes[j]
   end
  end
end

1 个答案:

答案 0 :(得分:1)

下一组迭代时,将创建一个新的随机extension UIImage : NSDiscardableContent { public func beginContentAccess() -> Bool { return false } public func endContentAccess() { print("End content access") } public func discardContentIfPossible() { print("Discarding") } public func isContentDiscarded() -> Bool { return false } } 数组,因此您将以随机分布在treatment_indexes级别的顺序离开。在结果中,应该看到具有相同编号的treatment_selecion_id将具有不同的treatment_selecion_id。但是您找不到treatment_indexrepetition_index之间的唯一性关系。

您可以随机创建此treatment_index,但仍需要搜索treatment_index的外观并避免碰撞。请注意,如果不随机创建此索引,则最终repetition_indextreatment_selection_id的值将相同,并且符合您所要求的唯一性行为。

此更改应使您具有随机性,并且仍可以消除treatment_index级别的重复项。

repetition_index

no_reps = @trial.number_of_repetitions repet_treat = {} # you have to keep track of repetition_indexes and treatment_indexes @trial.treatment_selections.each do |r| r.repetitions.in_groups_of(no_reps).each_with_index do |a, i| treatment_indexes = (1..no_reps).to_a # randomness will be handled elsewhere a.each_with_index do |r, j| r.repetition_index = j + 1 # store treatment_index for this repetition_index in an array repeat_treat[r.repetition_index] ||= [] # get the treatment_index you have already used for this repetition_index used_treat_indexes = repeat_treat[r.repetition_index] # delete the used indexes from the posibilities of the next and get a new random index r.treatment_index = (treatment_indexes - used_treat_indexes).sample # store your newely used treatment_index in its repetition_index group repeat_treat[r.repetition_index] << r.treatment_index end end end 的结构已经存储了@trial,因此您应该能够使用treatment_index而不是新的哈希来实现相同的想法。

@trial