计算数组中x和y之间的值出现多少次

时间:2018-10-31 02:58:38

标签: arrays ruby

我有一个200个1到100之间的随机数的数组,需要查找数组中x和y之间有多少个数字。范围在1-10、11-20、21-30等之间。

我有这样的东西

num_array = Array.new
200.times do
   num_array.push(rand(1...100))
end
print num_array
x = 1
y = 10
9.times do
  print num_array.count(x...y)   #<==
  x += 10
  y += 10
end

我希望第9行(箭头指向的那一行)告诉x和y之间的数字出现在数组中的次数。我知道如何用更多的代码来实现,但是我真的很想只用这一行或两行来实现。

3 个答案:

答案 0 :(得分:4)

Array#count,带有参数且没有块,检查参数和当前元素是否相等。您需要传递一个块并进行比较,您可以使用Comparable#between?,如下所示:

num_array.count { |el| el.between?(x,y) }

答案 1 :(得分:1)

您可以使用group_by并根据(n / 10)* 10将数组中的值分组,这将为您提供相应的十个值,例如:

  

(83/10)* 10 = 80

p Array.new(200) { rand(1..100) }.group_by { |i| (i / 10) * 10 }
{ 70=>[73, 77, 79, 70, 72, 78, 72, 75, 74, 71, 79, 70, 75, 78, 76, 70, 77, 76, 76, 70],
  80=>[81, 85, 81, 86, 88, 87, 85, 86, 80, 80, 82, 88, 89, 87, 87, 89, 86, 84, 82],
  60=>[68, 60, 64, 63, 64, 66, 69, 68, 66, 68, 67, 69, 61, 69, 62, 61, 66, 67, 69, 64, 65, 66, 68, 68, 60, 64, 69],
  30=>[31, 39, 39, 37, 32, 35, 31, 39, 30, 39, 33, 31, 36, 38, 30, 30, 36, 32, 39, 32, 31],
  20=>[22, 27, 24, 27, 25, 20, 23, 25, 27, 23, 24, 26, 27, 25, 25, 28, 20],
  40=>[42, 44, 47, 43, 45, 45, 41, 48, 41, 42, 45, 46, 45, 48, 47, 46, 45],
  90=>[92, 97, 92, 94, 94, 95, 93, 98, 93, 94, 94, 92, 96, 94, 91, 95, 94, 93, 99, 92, 99, 96, 91, 97, 99, 96, 94, 99],
  10=>[17, 13, 16, 18, 13, 19, 14, 16, 14, 12, 10, 15, 13, 16, 14, 13],
  50=>[58, 50, 54, 58, 57, 52, 53, 53, 56, 57, 50, 52, 55],
  0=>[4, 3, 7, 4, 2, 5, 9, 1, 4, 7, 8, 7, 3, 7, 9, 6, 6, 9, 6],
  100=>[100, 100, 100] }

应用transform_values,它为哈希中的每个元素获取每个数组的元素总数:

p Array.new(200) { rand(1..100) }.group_by { |i| (i / 10) * 10 }.transform_values(&:count)  
# {70=>20, 80=>19, 60=>27, 30=>21, 20=>17, 40=>17, 90=>28, 10=>16, 50=>13, 0=>19, 100=>3} 

注意0是1到10(不包含)之间数字的键。

答案 2 :(得分:1)

附加:您还可以获得x和y之间的哪些元素以及这些元素重复多少次,

from math import log,ceil,pow
from flask import Flask, request
from flask_restful import Resource, Api
import struct

app = Flask(__name__)
api = Api(app)
def binaryToDecimal(n): 
        return int(n,2)     
def dostuff(inputarray):
        args = request.args
        lower_end_range = args['lower_end_range']
        higher_end_range = args['higher_end_range']
        amount = args['amount']
        lower_end_range =int(lower_end_range)
        higher_end_range=int(higher_end_range)
        amount          =int(amount)
        #range_input is the range
        range_input=higher_end_range-lower_end_range+1 
        #taking the log of the range  to generate offset
        log_of_range=log(range_input,2)
        log_of_range=int(ceil(log_of_range)) 
        higher_end_range_represented_by_bits     =   0
        lower_end_range_represented_by_bits      =   0    
        lst                                      =   []
        FinalRandomArray                         =   []
        #creating the maximum of numbers which  it can go to by saving,for ex: 2^3+2^2+2^1+2^0
        for i in range(0,(log_of_range)):
                higher_end_range_represented_by_bits+=pow(2,i)
        while True:
            i=range_input%2
            range_input=range_input/2
            lst.append(i)
            if range_input==0:
                break
        length    =   len(lst)
        #where length is equal to the window size
        for file in range(0,len(inputarray),length):
            print(inputarray[0])
            number=binaryToDecimal((inputarray[file]+inputarray[file+1]+inputarray[file+2]))+lower_end_range
            if(number>=lower_end_range and number<=higher_end_range):
                    if(amount!=0):
                        FinalRandomArray.append(number)
                        amount-=1
        return {'finalrandomarray':FinalRandomArray}         
class ReturnMainModule(Resource):
    def get(self):
        with open("testUniform.bin", 'rb') as f:
            for chunk in iter(lambda: f.read(4096), b''):
                dostuff(chunk)

api.add_resource(ReturnMainModule, '/main')

# Driver code   
if __name__ == '__main__':
    app.run(port='5004')