Created
February 9, 2013 08:58
-
-
Save passcod/4744638 to your computer and use it in GitHub Desktop.
Reverse array sort
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
function rarr (n) { | |
var arr = []; | |
for (var i = n; i >= 0; i--) { | |
arr.push(i); | |
} | |
arr.sort(); | |
return arr; | |
} | |
var total = 0; | |
for (var n = 5; n < 30000; n++) { | |
total += rarr(n).length; | |
} | |
print(total); |
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
# Python 2 | |
def rarr(n): | |
arr = [] | |
for i in xrange(n, 0, -1): | |
arr.append(i) | |
arr.sort() | |
return arr | |
total = 0 | |
for n in xrange(5, 30000): | |
total += len(rarr(n)) | |
print total |
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 rarr n | |
arr = [] | |
n.step(1, -1) { |i| arr.push i } | |
arr.sort | |
end | |
total = 0 | |
5.step(30000) { |n| total += rarr(n).length } | |
puts total |
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
# Python 3 | |
def rarr(n): | |
arr = [] | |
for i in range(n, 0, -1): | |
arr.append(i) | |
arr.sort() | |
return arr | |
total = 0 | |
for n in range(5, 30000): | |
total += len(rarr(n)) | |
print(total) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment