Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
挑选;“最不拥挤”;Ruby数组中的数字_Ruby - Fatal编程技术网

挑选;“最不拥挤”;Ruby数组中的数字

挑选;“最不拥挤”;Ruby数组中的数字,ruby,Ruby,我有一种智力上的好奇心,我喜欢你的想法。不一定需要一个完整的解决方案;我只是想多看看 给定: 整数数组(有可能重复) 可供选择的“可接受”整数范围 问题: 根据整数在a中的“拥挤程度”,对r中的整数进行加权。整数的“拥挤”程度取决于两个因素: 它在a中出现了多少次?越频繁,越拥挤 它有多少近邻?邻居越近,交通就越拥挤 #1比2重得多(多少?不确定;我只是觉得应该“很多”) 示例: a=[1,1,2,4,6,8,8,8,8,9,10,10] r=(1..11) 解决方案理念: 这是我想出的一个

我有一种智力上的好奇心,我喜欢你的想法。不一定需要一个完整的解决方案;我只是想多看看

给定:

  • 整数数组(有可能重复)
  • 可供选择的“可接受”整数范围
问题:

根据整数在
a
中的“拥挤程度”,对
r
中的整数进行加权。整数的“拥挤”程度取决于两个因素:

  • 它在
    a
    中出现了多少次?越频繁,越拥挤
  • 它有多少近邻?邻居越近,交通就越拥挤
  • #1比2重得多(多少?不确定;我只是觉得应该“很多”)

    示例:

    a=[1,1,2,4,6,8,8,8,8,9,10,10]

    r=(1..11)

    解决方案理念:

    这是我想出的一个快速(而且肮脏,绝对)解决方案;似乎在做这个工作:

    $a = [1, 1, 2, 4, 6, 8, 8, 8, 8, 9, 10, 10]
    $r = (1..11)
    
    def how_congested?(integer)
      ((10 * $a.count(integer) + 2.5 * number_of_neighbors(integer))/100)
    end
    
    def number_of_neighbors(integer)
      count = 0
      hash = Hash[$a.uniq.map.with_index.to_a]
      index = hash[integer]
    
      count += 1 unless hash[integer + 1].nil?
      count += 1 unless hash[integer - 1].nil?
      count
    end
    
    $r.each do |i|
      puts "Congestion of ##{ i }: #{ how_congested?(i) }"
    end
    
    # Congestion of #1: 0.225
    # Congestion of #2: 0.125
    # Congestion of #3: 0.05
    # Congestion of #4: 0.1
    # Congestion of #5: 0.05
    # Congestion of #6: 0.1
    # Congestion of #7: 0.05
    # Congestion of #8: 0.425
    # Congestion of #9: 0.15
    # Congestion of #10: 0.225
    # Congestion of #11: 0.025
    
    问题:

    • 这考虑到了近邻,但不是2点外的邻居,3点外的邻居,等等。我认为应该有某种滑动比例(例如,“隔壁”邻居的数量是2点外邻居的2倍,等等)
    • 我在餐巾纸上提出了这个“算法”,但我想知道是否有更智能的方法来实现它
    感谢你的想法

    看看这个:

    class Congestion
      attr_accessor :array, :range
    
      def initialize(array, range)
        @array = array
        @range = range
      end
    
      def how_congested?(integer)
        ((10 * self.array.count(integer) + 2.5 * weight_of_neighbors(integer)) / 100)
      end
    
      def weight_of_neighbors(integer)
        weight = 0
        @array.uniq.each do |elem|
          weight += case (elem - integer).abs
                    when 1 then 3
                    when 2 then 2
                    when 3 then 1.5
                    when 4 then 1.25
                    when 5 then 1
                    else 0
                    end
        end
        weight
      end
    
      def calculate
        self.range.each do |i|
          congestion = how_congested?(i)
          puts "Congestion of #{i}: #{congestion}"
        end
      end
    end
    
    a = [1, 1, 2, 4, 6, 8, 8, 8, 8, 9, 10, 10]
    r = (1..11)
    
    c = Congestion.new(a, r)
    c.calculate
    
    结果是这样的:

    # Congestion of 1: 0.3375
    # Congestion of 2: 0.25625
    # Congestion of 3: 0.2625
    # Congestion of 4: 0.29375
    # Congestion of 5: 0.3125
    # Congestion of 6: 0.325
    # Congestion of 7: 0.3
    # Congestion of 8: 0.60625
    # Congestion of 9: 0.3125
    # Congestion of 10: 0.35625
    # Congestion of 11: 0.1875
    
    基本上,这里的相关变化是,它取我们感兴趣的整数,从数组的当前元素中减去它,然后得到该数字的正版本