Skip to content

Instantly share code, notes, and snippets.

@ppworks
Created September 27, 2012 09:41
Show Gist options
  • Save ppworks/3793160 to your computer and use it in GitHub Desktop.
Save ppworks/3793160 to your computer and use it in GitHub Desktop.
配列の要素をランキングする
#!/usr/bin/env ruby
def ranks scores
ranks = Array.new scores.size, 1
prev = nil
rank = 0
scores.each_with_index do|score, i|
unless prev == score
rank = i
end
ranks[i] += rank
prev = scores[i]
end
ranks
end
puts ranks([100, 90, 90, 90, 90, 80, 70])
@rinmu
Copy link

rinmu commented Sep 27, 2012

def ranks ary
  rank = ary.sort.reverse
  ary.map{|n| rank.index(n) + 1}
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment