了解源$ C ​​$ C为Ruby的repeated_permutation方法 [英] Understanding the source code for Ruby's repeated_permutation method

查看:252
本文介绍了了解源$ C ​​$ C为Ruby的repeated_permutation方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经建立在Ruby中的智能策划的游戏。在我的游戏,如果你选择具有电脑玩code断路器的作用的选项,计算机使教育的猜测在code制造商的code是什么。

I've been building an intelligent Mastermind game in Ruby. In my game, if you select the option of having the computer play the role of code breaker, the computer makes educated guesses at what the code maker's code is.

由于我的算法的一部分,计算机首先着眼于所有可能的$ C $的整个列表CS。

As part of my algorithm, the computer first looks at the entire list of ALL POSSIBLE CODES.

例如,如果有6种颜色可供选择(红橙蓝,绿紫黄)和code是由4种颜色(重复允许),则可以查看所有可能的codeS,你可以这样做:

For example, if there are 6 colors to choose from (red orange blue green purple yellow) and the code is made up of 4 colors (repeats are allowed), then to view all possible codes, you can do this:

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

all_possible_ codeS 将充满数组的数组重新presenting每一个可能的code。然后,计算机从该列表中排除codeS,因为它从它的每一个猜测得到反馈。

And all_possible_codes will be an array filled with arrays representing every possible code. The computer then eliminates codes from this list as it gets feedback from each of its guesses.

我的工作接下来的事情,但是,要求我使用的JRuby 1.6.6,它使用了Ruby 1.8.7,它不具有 repeated_permutation 方法。我需要写我自己具有相同功能的方法。

The next thing I'm working on, however, requires me to use JRuby 1.6.6, which uses Ruby 1.8.7, which does NOT have a repeated_permutation method. I need to write my own method with the same functionality.

于是我去了源$ C ​​$ C在这里找到:的 http://www.ruby-doc.org/core-1.9.3/Array.html#method-i-repeated_permutation

So I went to the source code found here: http://www.ruby-doc.org/core-1.9.3/Array.html#method-i-repeated_permutation

不幸的是,我不明白他们在做什么,或者我怎么能去写我自己的方法解决这个。我是相当新的编程和一直没能想出解决办法。要认识到,源$ C ​​$ C任何帮助将大大AP preciated!

Unfortunately, I don't understand what they're doing or how I could go about solving this by writing a method of my own. I'm fairly new to programming and haven't been able to figure this out. Any help with understanding that source code would be greatly appreciated!

推荐答案

您联系电话rpermute0其中完成大部分工作,源$ C ​​$ C在Array.c rpermute0的code是

The code you linked calls rpermute0 which does most the work, source code for rpermute0 in Array.c is

static void
rpermute0(long n, long r, long *p, long index, VALUE values)
{
    long i, j;
    for (i = 0; i < n; i++) {
    p[index] = i;
    if (index < r-1) {              /* if not done yet */
        rpermute0(n, r, p, index+1, values); /* recurse */
    }
    else {
        /* We have a complete permutation of array indexes */
        /* Build a ruby array of the corresponding values */
        /* And yield it to the associated block */
        VALUE result = rb_ary_new2(r);
        VALUE *result_array = RARRAY_PTR(result);
        const VALUE *values_array = RARRAY_PTR(values);

        for (j = 0; j < r; j++) result_array[j] = values_array[p[j]];
        ARY_SET_LEN(result, r);
        rb_yield(result);
        if (RBASIC(values)->klass) {
        rb_raise(rb_eRuntimeError, "repeated permute reentered");
        }
    }
    }
}

Basially蛮力,从0返回一个序列改变每个迭代开始。红宝石的版本是一样的东西。

Basially a brute force, starting from 0 returning one permutation each iteration. ruby version is something like

require 'pp'

def rpermute(numRepeat, pArray, index, originArray)
  0.upto(originArray.length-1) do |i|
    pArray[index] = i
    if index < numRepeat-1
      rpermute(numRepeat, pArray, index+1, originArray)
    else
      result = Array.new
      0.upto(numRepeat-1) do |j|
        result[j] = originArray[pArray[j]]
      end
     pp result
    end
  end
end

originArray1 = [1,2,3,4,5]
originArray2 = ['a','b','c','d','e']
pArray = []

rpermute(4, pArray, 0, originArray1)
rpermute(4, pArray, 0, originArray2)

我上面code测试,它打印出长度为4的所有排列,U可能希望把它们放进数组中。

I tested above code and it prints out all permutation of length 4, u might want to put them into array.

这篇关于了解源$ C ​​$ C为Ruby的repeated_permutation方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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