title: Pick "least congested" number in a Ruby array
body: <p>I have an intellectual curiosity that I would love your thoughts on. Don't necessarily need a whole solution; just want to get more eyes on it.</p>
  
  <p><strong>Given:</strong></p>
  
  <ul>
  <li>An array of integers (with the possibility of duplicates)</li>
  <li>A range of "acceptable" integers to choose from.</li>
  </ul>
  
  <p><strong>Problem:</strong></p>
  
  <p>Weight the integers in <code>r</code> based on how "congested" they are in <code>a</code>. Two factors go into how "congested" an integer is:</p>
  
  <ol>
  <li>How many times does it appear in the <code>a</code>? The more frequent, the more congested.</li>
  <li>How many immediate neighbors does it have? The closer the neighbors, the more congested.</li>
  </ol>
  
  <p>#1 weights much more heavily than #2 (how much? not sure; I just think it ought to be "a lot").</p>
  
  <p><strong>Example:</strong></p>
  
  <p><code>a = [1, 1, 2, 4, 6, 8, 8, 8, 8, 9, 10, 10]</code></p>
  
  <p><code>r = (1..11)</code></p>
  
  <p><strong>Solution Idea:</strong> </p>
  
  <p>Here's a quick (and dirty, definitely) solution that I came up with; seems to do the job:</p>
  
  <pre><code>$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
  </code></pre>
  
  <p><strong>Issue:</strong></p>
  
  <ul>
  <li>This takes into account immediate neighbors, but not neighbors 2 spots away, 3 spots away, etc. I think there should be some sort of sliding scale (e.g., "next-door" neighbors count 2x as much as neighbors 2 spots away, etc.).</li>
  <li>I came up with this "algorithm" on a napkin, but I'm wondering if there's a more intelligent way to do it?</li>
  </ul>
  
  <p>Appreciate your thoughts!</p>