如何比较整数索引数组中时,有重复的值? [英] How do I compare integer indexes in arrays when there are duplicate values?

查看:142
本文介绍了如何比较整数索引数组中时,有重复的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,一些必要的背景。我试图让游戏策划的基于数字版本,学习Ruby的code的一种方式。我的code基本上是这样的:


  1. 计算机生成1-5( @computer_sequence )的4随机数的数组

  2. 用户输入一个4位数的序列,这称为数组 @user_array 卷起。

  3. 的方法,名为比较,到 @user_array 迭代,比较每个号码的那些价值和指数在 @computer_sequence。程序然后告诉用户有多少他们的数字有正确的价值,正确的位置,或者多少个号码有正确的价值而已。

问题:如果有许多的多个实例在数组中,他们得到同样的指数,对不对?就像如果我有阵 [1,3,3,4] ,三个数为1的指数,即使有两个三分球。在该项目中的工作,但是,每一个号码必须有数组中的一个独特的位置(是指数连我想在这里的话?),即使发生一些多次。这是否有意义?

此外,这里的code为比较方法:

  DEF比较
    VALUE_ONLY = 0
    value_and_place = 0
    把电脑的价值观是:#{@ computer_sequence}
    把用户的值是:#{@ user_array}
    @ user_array.each做|候选人|
        @ computer_sequence.each做| computer_number |
            如果候选人== computer_number&放大器;&安培; @ user_array.index(候选)== @ computer_sequence.index(computer_number)
                value_and_place + = 1
            ELSIF候选人== computer_number&放大器;&安培; @ user_array.index(候选人)!= @ computer_sequence.index(computer_number)
                VALUE_ONLY + = 1
            结束
        结束
    结束


解决方案

假设

 电脑= Array.new(4){[1,2,3,4,5] .sample}
  #=> [3,2,3,3]
user_digits = [2,4,2,3]

元素首先计算对同一指数电脑 user_digits

 对= computer.zip(user_digits)
  #=> [[3,2],[2,4],[3,2],[3,3]

值的计算数量匹配在同一位置的

  pairs.count {| C,U | ç== U】
  #=> 1

值的计算数量匹配在不同位置的

首先,在电脑的相同位置除去比赛和 user_digits

 补偿,用户= pairs.reject {| C,U | ç== U】.transpose
  #=> [[3,2,3],[2,4,2]

含义

 补偿#=> [3,2,3]
用户#=> [2,4,2]

现在通过用户去除补偿第一个匹配的元素(如果有)步骤。

  users.each做| N |
  I = comp.index(N)
  comp.delete_at(i)如我
结束

所以现在:

 补偿#=> [3,3]

含义匹配于不同的位置的元素的数量是:

  users.size-comp.size
  #=> 1

替代计算

计算后

 补偿,用户= pairs.reject {| C,U | ç== U】.transpose

我们可以写

  users.size  -  comp.difference(用户).size
  #=> 1

其中,阵列#区别是我在回答的这里

下面

  comp.difference(用户)
  #=> [3,3]

First, some necessary background. I'm trying to make a number-based version of the game Mastermind as a way of learning to code in Ruby. My code basically works like this:

  1. The computer generates an array (@computer_sequence) of 4 random numbers from 1-5
  2. The user enters a 4 digit sequence, which winds up in an array called @user_array.
  3. A method, called compare, iterates through @user_array, comparing the value and index of each number to those in @computer_sequence. The program then tells the user how many of their numbers have the correct value and the correct position, or how many numbers have the correct value only.

The problem: If there are multiple instances of a number in an array, they get the same index, right? Like if I have the array [1, 3, 3, 4], the number three has an index of 1, even though there are two 3s. For this program to work, though, each number has to have a unique position (is index even the word I want here?) in the array, even if the number occurs multiple times. Does that make sense?

Also, here's the code for the compare method:

def compare
    value_only = 0
    value_and_place = 0
    puts "The computer's values are: #{@computer_sequence}"
    puts "The user's values are: #{@user_array}"


    @user_array.each do |candidate|
        @computer_sequence.each do |computer_number|
            if candidate == computer_number && @user_array.index(candidate) == @computer_sequence.index(computer_number)
                value_and_place +=1
            elsif candidate == computer_number && @user_array.index(candidate) != @computer_sequence.index(computer_number)
                value_only +=1
            end
        end
    end

解决方案

Suppose

computer = Array.new(4) { [1,2,3,4,5].sample }
  #=> [3, 2, 3, 3]
user_digits = [2, 4, 2, 3]

First compute pairs of elements at the same index of computer and user_digits.

pairs = computer.zip(user_digits)
  #=> [[3, 2], [2, 4], [3, 2], [3, 3]]

Compute number of values that match at the same position

pairs.count { |c,u| c==u }
  #=> 1

Compute number of values that match at different positions

First remove the matches at the same positions of computer and user_digits.

comp, users = pairs.reject { |c,u| c==u }.transpose
  #=> [[3, 2, 3], [2, 4, 2]] 

meaning

comp  #=> [3, 2, 3] 
users #=> [2, 4, 2] 

Now step through users removing the first matching element in comp (if there is one).

users.each do |n|
  i = comp.index(n)
  comp.delete_at(i) if i
end

So now:

comp #=> [3,3] 

meaning that the number of elements that match at different positions is:

users.size-comp.size
  #=> 1 

Alternative calculation

After computing

comp, users = pairs.reject { |c,u| c==u }.transpose

we could write

users.size - comp.difference(users).size
  #=> 1

where Array#difference is as I defined it in my answer here.

Here

comp.difference(users)
  #=> [3,3]

这篇关于如何比较整数索引数组中时,有重复的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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