This file contains hidden or 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
// see http://www.johndcook.com/IEEE_exceptions_in_cpp.html for details | |
bool IsNumber(double x) | |
{ | |
// This looks like it should always be true, | |
// but it's false if x is a NaN. | |
return (x == x); | |
} | |
#include <float.h> |
This file contains hidden or 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 is_number(s): | |
try: | |
float(s) | |
return True | |
except ValueError: | |
return False |
This file contains hidden or 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
import subprocess | |
def call_command(command): | |
process = subprocess.Popen(command.split(' '), | |
stdout=subprocess.PIPE, | |
stderr=subprocess.PIPE) | |
return process.communicate() | |
# An example | |
if __name__ == "__main__": | |
This file contains hidden or 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
#include <unistd.h> | |
// Check whether file exist | |
// http://stackoverflow.com/questions/230062/whats-the-best-way-to-check-if-a-file-exists-in-c-cross-platform | |
if( access( fname, F_OK ) != -1 ) { | |
// file exists | |
} else { | |
// file doesn't exist | |
} |
This file contains hidden or 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
"""BFS.py | |
Breadth First Search. See also LexBFS.py. | |
D. Eppstein, May 2007. | |
""" | |
try: | |
set | |
except NameError: |
This file contains hidden or 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
# http://stackoverflow.com/questions/893417/item-frequency-count-in-python | |
# defaultdict will automatically initialize values to zero. | |
from collections import defaultdict | |
words = "apple banana apple strawberry banana lemon" | |
d = defaultdict(int) | |
for word in words.split(): | |
d[word] += 1 |
This file contains hidden or 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
# http://docs.python.org/faq/programming.html#how-do-you-remove-duplicates-from-a-list | |
mylist = list(set(mylist)) |
This file contains hidden or 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
# http://stackoverflow.com/questions/613183/python-sort-a-dictionary-by-value | |
# http://wiki.python.org/moin/HowTo/Sorting/ | |
import operator | |
x = {1: 2, 3: 4, 4:3, 2:1, 0:0} | |
sorted_x = sorted(x.iteritems(), key=operator.itemgetter(1),reverse=True) | |
y = [ ('john', 'A', 15), ('jane', 'B', 12), ('dave', 'B', 10),] |
NewerOlder