Skip to content

Instantly share code, notes, and snippets.

@jczaplew
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jczaplew/89659a2c111a1831f606 to your computer and use it in GitHub Desktop.
Save jczaplew/89659a2c111a1831f606 to your computer and use it in GitHub Desktop.
(Climbing) Suicide Counter
function countSuicide(moves) {
var totalMoves = 0;
for (var i = moves; i > 0; i--) {
totalMoves += i * 2;
}
return totalMoves;
}
def count_suicide(moves):
total_moves = 0;
while moves > 0:
total_moves += moves * 2
moves -= 1
return total_moves
@tomMulholland
Copy link

def count_suicide(moves):
    total_moves = sum(2 * i for i in range(0, moves + 1))
    return total_moves

List comprehension = "More pythonic Python"

@jczaplew
Copy link
Author

def count_suicide(moves):
  return sum(2 * i for i in range(0, moves + 1))

@jczaplew
Copy link
Author

...i for i... sounds like the Piratic way of doing it.

@tomMulholland
Copy link

Talk about pirating! (See comment #1 by jczaplew)
We should now run speed tests to see what really is the fastest.

@tomMulholland
Copy link

def count_suicide():
    print("Goodbye, cruel world")
    suicide = 1
    from sys import exit as suicide
    suicide()

@jczaplew
Copy link
Author

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