生成带有变量的句子的所有可能组合

时间:2019-03-20 12:20:19

标签: string algorithm

说我有一个包含多个变量的句子,如下所示:

"lorem ipsum {a, b} dolor {c, d, e} sit amet"

假设括号中的字母是变量 ,那么如何从变量的所有可能组合中生成一组句子?

注意: 变量组的数量或每组变量中的个计数未知


此特定示例的预期输出为:

"lorem ipsum {a} dolor {c} sit amet"
"lorem ipsum {b} dolor {c} sit amet"
"lorem ipsum {a} dolor {d} sit amet"
"lorem ipsum {b} dolor {d} sit amet"
"lorem ipsum {a} dolor {e} sit amet"
"lorem ipsum {b} dolor {e} sit amet"

2 个答案:

答案 0 :(得分:2)

一般情况下(“变量组的数量...每个组中的数量...是未知”),我们应该解析初始字符串(让我们在正则表达式的帮助下完成),然后枚举所有组合。

C#代码:

import numpy as np
import cv2
import glob 
mean = 0
var = 10
sigma = var ** 0.5
gaussian = np.random.normal(mean, sigma, (224, 224)) 



for image in glob.glob('/home/aub/myflower/flower_photos/dandelion/*.jpg'):
    img = cv2.imread(image)
    noisy_image = np.zeros(img.shape, np.float32)

    if len(img.shape) == 2:
        noisy_image = img + gaussian
    else:
        noisy_image[:, :, 0] = img[:, :, 0] + gaussian
        noisy_image[:, :, 1] = img[:, :, 1] + gaussian
        noisy_image[:, :, 2] = img[:, :, 2] + gaussian

        cv2.normalize(noisy_image, noisy_image, 0, 255, cv2.NORM_MINMAX, dtype=-1)
noisy_image = noisy_image.astype(np.uint8)

       cv2.imshow("img", img)
       cv2.imshow("gaussian", gaussian)
       cv2.imshow("noisy", noisy_image)
cv2.waitKey(0)

演示:

using System.Text.RegularExpressions;
...

private static IEnumerable<string> Generator(string source) {
  // parsing: variables extracted: array of variables and their possible values
  string[][] variables = Regex
    .Matches(source, @"\{.*?\}")
    .OfType<Match>()
    .Select(match => match
       .Value
       .Trim('{', '}')
       .Split(',')
       .Select(item => "{" + item.Trim() + "}")
       .ToArray())
    .ToArray();

  // now we should enumerate all possible variables' values
  int[] indexes = new int[variables.Length];

  do {
    // code golf : ugly side effects but short code
    int at = 0;

    yield return Regex.Replace(source, @"\{.*?\}", match => variables[at][indexes[at++]]);

    for (int i = 0; i < indexes.Length; ++i)
      if (indexes[i] < variables[i].Length - 1) {
        indexes[i] = indexes[i] + 1;

        break;
      }
      else
        indexes[i] = 0;
  }
  while (!indexes.All(index => index == 0));
}

结果:

string source = @"lorem ipsum {a, b} dolor {c, d, e} sit amet";

string report = string.Join(Environment.NewLine, Generator(source));

Console.Write(report);

另一个示例:

lorem ipsum {a} dolor {c} sit amet
lorem ipsum {b} dolor {c} sit amet
lorem ipsum {a} dolor {d} sit amet
lorem ipsum {b} dolor {d} sit amet
lorem ipsum {a} dolor {e} sit amet
lorem ipsum {b} dolor {e} sit amet

结果:

// 3 groups of variables with strange names
string source = @"lorem ipsum {A + 2, B, C?} dolor {XY, PQR} sit {eh?, bla-bla-bla} amet";

Console.Write(string.Join(Environment.NewLine, Generator(source)));

答案 1 :(得分:1)

因此,基本上,您想为每个数组中单个值的所有可能组合迭代两个不同的数组-嵌套循环可能是最好的选择。

使用c#代码可以做到这一点,每行都带有注释,以便轻松翻译为其他语言:

var values0 = new string[] {"a", "b"}; // All possible values for first slot
var values1 = new string[] {"c", "d", "e"}; // All possible values for second slot

foreach(var val0 in values0) // Iterate first array
{
    foreach(var val1 in values1) // Iterate second array
    {
        var result = $"Lorem ipsum {val0} dolor {val1} sit amet"; // Insert values to slots
        Console.WriteLine(str); // output
    }
}

结果:

Lorem ipsum a dolor c sit amet
Lorem ipsum a dolor d sit amet
Lorem ipsum a dolor e sit amet
Lorem ipsum b dolor c sit amet
Lorem ipsum b dolor d sit amet
Lorem ipsum b dolor e sit amet