阵列排列的比较:是否这个数组包含上述任何阵列? [英] Array permutations and comparisons: Does this array contain any of these arrays?

查看:99
本文介绍了阵列排列的比较:是否这个数组包含上述任何阵列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看了遍所以回答这个,虽然我已经发现了一些类似的问题,我只是一直没能解决我的问题。

I've looked all over SO for an answer to this, and while I've found a few similar problems, I just haven't been able to solve my problem.

我建立一个策划游戏。现在我工作的算法的智能电脑的猜测。我读过有关这个有很多,但有人相当新的节目,它是一个小小的挑战开发这个算法(我使用TDD)。

I'm building a Mastermind game. Right now I'm working on the algorithm for an intelligent computer guess. I've read about this a lot, but as someone fairly new to programming, it's been a bit of a challenge to develop this algorithm (I'm using TDD).

secret_code = %w(blue blue orange orange)
guess = %w(orange blue red yellow)
valid_colors = %w(blue green orange purple red yellow)
possible_secret_codes = valid_colors.repeated_permutation(4).to_a

我要消灭尽可能多的possible_secret_ codeS,我可以根据我做我的猜测后,收到的反馈(分数)。

I want to eliminate as many possible_secret_codes as I can based on the feedback (the score) I receive after making my guess.

一个可能的方法(简单,虽然不是最有效的)是第一个专注于发现,无论位置,正确的四种颜色。

One potential approach (simple, albeit not the most efficient) is to first focus on finding the correct four colors, regardless of location.

score = {exact: 1, close: 1}
total_score = score[:exact] + score[:close]
parts_of_secret_code = guess.repeated_permutation(total_score).to_a.uniq

parts_of_secret_ code是要返回数组的数组。我们可以肯定的是秘密code包含的至少有一个的那些阵列。我想从possible_secret_ codeS任何code,这并不包括那些阵列中的至少一个,以消除。

parts_of_secret_code is going to return an array of arrays. We can be certain that the secret code includes AT LEAST ONE of those arrays. I want to eliminate from the possible_secret_codes any code that does not include at least one of those arrays.

使用我提供的信息。例如(假设秘密code我提供,我公司提供的猜想,我提供的分数等),这是parts_of_secret_ code将是:

Using the example information I provided (assuming the secret code I provided, the guess I provided, the score I provided, etc.), this is what parts_of_secret_code would be:

parts_of_secret_code = [["orange", "orange"],
                        ["orange", "blue"],
                        ["orange", "red"],
                        ["orange", "yellow"],
                        ["blue", "orange"],
                        ["blue", "blue"],
                        ["blue", "red"],
                        ["blue", "yellow"],
                        ["red", "orange"],
                        ["red", "blue"],
                        ["red", "red"],
                        ["red", "yellow"],
                        ["yellow", "orange"],
                        ["yellow", "blue"],
                        ["yellow", "red"],
                        ["yellow", "yellow"]]

欲消除codeS不具有这些阵列中的至少一个。这样做会减少possible_secret_ codeS(1296个)是由有效颜色数组上调用repeated_permutation发现的原始列表(正如我上面显示):

I want to eliminate codes that do not have at least one of those arrays. Doing this will cut down the original list of possible_secret_codes (1,296 total) that is found by calling repeated_permutation on the array of valid colors (as I showed above):

possible_secret_codes = valid_colors.repeated_permutation(4).to_a

任何人都可以想办法做到这一点?我已经尝试了一堆东西,并一直没能弄明白。

Can anyone think of a way to do that? I've tried a bunch of things and haven't been able to figure it out.

在此先感谢您的帮助,让我知道,如果我没有提供足够的信息! (我知道,标题可能是奇数...不知道如何字呢。)

Thanks in advance for the help and let me know if I didn't provide enough information! (And I know that title might be odd...wasn't sure how to word it.)

推荐答案

我想消除codeS没有这些阵列中的至少一个。

"I want to eliminate codes that do not have at least one of those arrays."

require 'set' 
possible_secret_codes.select do |ary|
  parts_of_secret_codes.any? {|part| part.to_set.subset? ary.to_set}
end

什么是code片段确实是选择 possible_secret_ $满足这些阵列C $ CS 条件

parts_of_secret_codes.any? {|part| part.to_set.subset? ary.to_set}

条件前presses的事实,部分的一个子集。

这篇关于阵列排列的比较:是否这个数组包含上述任何阵列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆