You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Ruby/Javascript生成A、B、C分组的唯一组合技术需求

Got it, let's break this down and implement it in both Ruby and JavaScript. First, let's clarify the rules to make sure we're aligned:

  • Group A: Starts at 4, each extra item adds 2 → possible values: 4, 6, 8, ... (4 + 2n where n ≥ 0, n is the number of extra items)
  • Group B: Starts at 8, each extra item adds 3 → possible values: 8, 11, 14, ... (8 + 3n)
  • Group C: Starts at 11 (I assume the example's "C=10" was a typo since your initial definition says C=11), each extra item adds 3 → possible values: 11, 14, 17, ... (11 + 3n)

Since generating infinite combinations isn't practical, we'll add a limit (like your examples use up to 2 extra items per group). You can easily adjust this limit later.

Ruby Implementation

# Define each group's base value and increment amount
groups = {
  A: [4, 2],
  B: [8, 3],
  C: [11, 3]
}

# Helper to generate all possible values for a group (up to max extra items)
def generate_group_values(base, increment, max_extra)
  (0..max_extra).map { |extra_count| base + increment * extra_count }
end

# Set how many extra items we allow per group (matches your example with 2)
max_extra_items = 2

# Generate all possible values for each group
a_values = generate_group_values(groups[:A][0], groups[:A][1], max_extra_items)
b_values = generate_group_values(groups[:B][0], groups[:B][1], max_extra_items)
c_values = generate_group_values(groups[:C][0], groups[:C][1], max_extra_items)

# Get every possible combination (Cartesian product)
all_combinations = a_values.product(b_values, c_values)

# Print results in a readable format
puts "All combinations (max #{max_extra_items} extra items per group):"
all_combinations.each_with_index do |combo, idx|
  puts "#{idx + 1}. A=#{combo[0]}, B=#{combo[1]}, C=#{combo[2]}"
end

How it works:

  • The generate_group_values function creates all values from the base up to base + increment * max_extra.
  • Ruby's built-in product method makes it trivial to get all combinations of the three value sets.
  • Adjust max_extra_items if you want more/less extra items per group.

JavaScript Implementation

// Define each group's base value and increment amount
const groups = {
  A: [4, 2],
  B: [8, 3],
  C: [11, 3]
};

// Helper to generate all possible values for a group (up to max extra items)
function generateGroupValues(base, increment, maxExtra) {
  const values = [];
  for (let extraCount = 0; extraCount <= maxExtra; extraCount++) {
    values.push(base + increment * extraCount);
  }
  return values;
}

// Set how many extra items we allow per group
const maxExtraItems = 2;

// Generate all possible values for each group
const aValues = generateGroupValues(groups.A[0], groups.A[1], maxExtraItems);
const bValues = generateGroupValues(groups.B[0], groups.B[1], maxExtraItems);
const cValues = generateGroupValues(groups.C[0], groups.C[1], maxExtraItems);

// Calculate all combinations (vanilla JS, no libraries)
const allCombinations = [];
for (const a of aValues) {
  for (const b of bValues) {
    for (const c of cValues) {
      allCombinations.push({ A: a, B: b, C: c });
    }
  }
}

// Log results in a readable format
console.log(`All combinations (max ${maxExtraItems} extra items per group):`);
allCombinations.forEach((combo, idx) => {
  console.log(`${idx + 1}. A=${combo.A}, B=${combo.B}, C=${combo.C}`);
});

How it works:

  • The helper function builds the value list similarly to the Ruby version.
  • Since vanilla JS doesn't have a built-in Cartesian product method, we use nested loops to generate all combinations.
  • Adjust maxExtraItems to control how many extra items are allowed per group.

Want to limit by max value instead of extra items?

If you'd rather cap each group's value (instead of counting extra items), modify the helper function. For example, in Ruby:

def generate_group_values(base, increment, max_value)
  values = []
  current = base
  while current <= max_value
    values << current
    current += increment
  end
  values
end

Call it like generate_group_values(4, 2, 10) to get [4, 6, 8, 10].

Same idea in JavaScript:

function generateGroupValues(base, increment, maxValue) {
  const values = [];
  let current = base;
  while (current <= maxValue) {
    values.push(current);
    current += increment;
  }
  return values;
}

内容的提问来源于stack exchange,提问作者Andrew Chi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 07:23:14