Qiita Teams that are logged in
You are not logged in to any team

Community
Service
Qiita JobsQiita ZineQiita Blog
3
Help us understand the problem. What is going on with this article?
@hanahiro_aze

# 組み合わせテストを簡単にするgemを作る

More than 3 years have passed since last update.

by hanahiro_aze
1 / 21

# rspecで使うためには加工が必要。

No. first_name last_name
1 "aaa" "bbb"
2 "あああ" ""
: : :

みたいなやつを

 where(:id, :name, :comment) do
[
[2, "てすと", "コメント"],
[5, "test", "comment"],
[6, "a" * 32, "test"],
[7, "ﾃｽﾄ", "test"],
]
end


のようにイイ感じに整形しないといけない。

# アルゴリズムの紹介

異なる n 個のものから r 個を選ぶ場合の、組み合わせの数

nCr = \frac{nPr}{r!} = \frac{n!}{r!(n-r)!}


# 便利メソッド1

combination

[1,2,3,4].combination(2) do |first, second|
p "(#{first, #{second}})"
end

=> (1,2) (1,3) (1,4) (2,3) (2,4) (3,4)


# これで、2引数のarrayを組み合わせるのが実現できる。

{
:first=>[1, 1, 1, 2, 2, 2, 3, 3, 3],
:second=>["a", "b", "c", "a", "b", "c", "a", "b", "c"]
}


テーブルにすると、こんな感じ。

key v1 v2 v3 ・・・
first 1 1 1 ・・・
second a b c ・・・

No. first_name last_name
1 "aaa" "bbb"
2 "あああ" ""
: : :

# 便利メソッド2

transpose

values =  { :first=>[1, 1, 1, 2, 2, 2, 3, 3, 3],
:second=>["a", "b", "c", "a", "b", "c", "a", "b", "c"]}
values.transpose

=> [[1, "a"], [1, "b"], [1, "c"], [2, "a"], [2, "b"], [2, "c"], [3, "a"], [3, "b"], [3, "c"]]


# これから作るところ

• 網羅する水準の指定ができるようにしたい。
• 組み合わせを作るアルゴリズムを高速に。
• 禁則処理
• 出力された組み合わせをファイル出力 (0.1.3)

# 作ってから知ったgem。pairwise

3
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
1. We will deliver articles that match you
By following users and tags, you can catch up information on technical fields that you are interested in as a whole
2. you can read useful information later efficiently
By "stocking" the articles you like, you can search right away