-
-
Save SergeiStruk/d22233c2233dddf58c55 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