Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
# Finds the longest streak and the current streak within a sequence. | |
# The sequence could correspond to consecutive days of Github commit history. | |
# The assumptions is that it is ordered chronologically so the current streak is computed from the tail end of the sequence. | |
# reduce maintains the state as a tuple consisting of (longest streak count, current streak count) | |
# The int(bool(y)) trick is to convert a numeric into a binary 0 or 1 which will determine whether or not the current streak should continue. | |
# Time complexity: O(n) since reduce will traverse each item in the sequence exactly once. | |
# Space complexity: O(n) + k to store the original sequence plus the additional state tuple that propagates through reduce. | |
data = [0, 2, 4, 6, 4, 32, 24, 4, 4, 5, 0, 2, 3, 4, 0, 0, 0, 0, 2, 0, 0, 4, 7, 9] | |
longest, current = reduce(lambda state, y: (max(state[0], state[1] + 1 if state[1] and y else int(bool(y))), state[1] + 1 if state[1] and y else int(bool(y))), data, (0, 0)) |
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
class Timer: | |
""" | |
A ``Timer`` ContextManager to simplify timing sections of code. | |
""" | |
def __init__(self, name=None, logger=None, log_level=logging.DEBUG): | |
""" | |
Constructs a ``Timer``. | |
Parameters | |
---------- |
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
/*--- waitForKeyElements(): A utility function, for Greasemonkey scripts, | |
that detects and handles AJAXed content. | |
Usage example: | |
waitForKeyElements ( | |
"div.comments" | |
, commentCallbackFunction | |
); |
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
from collections import OrderedDict | |
import pytest | |
class MagicalPatchersMixin(object): | |
""" | |
A mixin for pytest xunit-style classes that provides scaffolding for setting up | |
patchers more easily that are setup for each class test method. |
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
# Xcode convenience function | |
function xcode { | |
if (( $# == 0 )); then | |
if [ -f *.xcodeproj ]; then | |
open *.xcodeproj | |
else | |
open -a Xcode.app | |
fi | |
fi | |
open -a Xcode.app $1 |