Last active
August 29, 2015 14:06
-
-
Save jczaplew/89659a2c111a1831f606 to your computer and use it in GitHub Desktop.
(Climbing) Suicide Counter
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 countSuicide(moves) { | |
var totalMoves = 0; | |
for (var i = moves; i > 0; i--) { | |
totalMoves += i * 2; | |
} | |
return totalMoves; | |
} |
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 count_suicide(moves): | |
total_moves = 0; | |
while moves > 0: | |
total_moves += moves * 2 | |
moves -= 1 | |
return total_moves |
def count_suicide(moves):
return sum(2 * i for i in range(0, moves + 1))
...i for i...
sounds like the Piratic way of doing it.
Talk about pirating! (See comment #1 by jczaplew)
We should now run speed tests to see what really is the fastest.
def count_suicide():
print("Goodbye, cruel world")
suicide = 1
from sys import exit as suicide
suicide()
import time
def count_suicide1(moves):
total_moves = 0;
while moves > 0:
total_moves += moves * 2
moves -= 1
return total_moves
def count_suicide2(moves):
return sum(2 * i for i in range(0, moves + 1))
start_time = time.time()
count_suicide1(13000)
print(time.time() - start_time)
start_time = time.time()
count_suicide2(13000)
print(time.time() - start_time)
With small numbers they are identical, with larger ones yours is slightly faster.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
List comprehension = "More pythonic Python"