Created
February 20, 2015 18:54
-
-
Save SergeiStruk/13e2e35346cb1d7ac54b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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