Skip to content

Instantly share code, notes, and snippets.

@SergeiStruk
Created February 20, 2015 18:53
Show Gist options
  • Save SergeiStruk/d22233c2233dddf58c55 to your computer and use it in GitHub Desktop.
Save SergeiStruk/d22233c2233dddf58c55 to your computer and use it in GitHub Desktop.
def check_stability
result = []
1000.times{ result << yield }
raise Exception, 'function is unstable' if result.uniq!.size > 1
p "Result: #{result}"
end
arr = [{:id=>1, :val=>30}, {:id=>2, :val=>30}, {:id=>1002, :val=>82}]
check_stability do
arr.sort{|x,y| y[:val] <=> x[:val]}.map{|x| x[:id]}
end
# Result: [[1002, 2, 1]]
arr = [{:id=>1, :val=>30}, {:id=>2, :val=>30}]
check_stability do
result = arr.sort{|x,y| y[:val] <=> x[:val]}.map{|x| x[:id]}
end
# Result: [[1, 2]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment