Skip to content

Instantly share code, notes, and snippets.

View ashwin's full-sized avatar

Ashwin Nanjappa ashwin

View GitHub Profile
@ashwin
ashwin / boost_dfs_example.cpp
Created July 26, 2008 06:12
Example of boost DFS on an undirected graph
// Boost DFS example on an undirected graph.
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/depth_first_search.hpp>
#include <iostream>
using namespace std;
typedef boost::adjacency_list<boost::listS, boost::vecS, boost::undirectedS> MyGraph;
typedef boost::graph_traits<MyGraph>::vertex_descriptor MyVertex;
for ( int x = i; x != s; x = next[x] )
{
// Use x here
// next[] might be updated here
}
@ashwin
ashwin / PyIntCacheRange.py
Created May 1, 2012 13:20
Range of integers cached by Python
# Python caches a small range of integers (objects)
# This is implementation specific and can be discovered easily
import platform
cacheBegin, cacheEnd = 0, 0
for i in range( -500, 0 ):
if i is int(str(i)):
cacheBegin = i
@ashwin
ashwin / RandomFloat.py
Created May 3, 2012 10:57
Generate random float in Python
import random
# Random float in [0.0, 1.0)
a = random.random()
# Random float in [0, 100]
b = random.uniform( 0, 100 )
@ashwin
ashwin / FunctionName.py
Created May 3, 2012 22:14
Function name in Python
def squareIt( x ):
return x * x
print( squareIt.__name__ ) # squareIt
foo = squareIt
print( foo.__name__ ) # squareIt
squareIt = lambda x: x * x
print( squareIt.__name__ ) # <lambda>
@ashwin
ashwin / GeneratorFunction.py
Created May 3, 2012 22:27
Generator function in Python
# Infinite sequence generator
def sequenceGen():
i = 0
while True:
yield i
i += 1
g = sequenceGen()
print( next( g ) ) # 0
print( next( g ) ) # 1
@ashwin
ashwin / AddFunctionAttribute.py
Created May 3, 2012 22:52
Adding function attributes in Python
def foo():
return foo.x
print( foo() ) # AttributeError: 'function' object has no attribute 'x'
foo.x = 10
print( foo.x ) # 10
print( foo() ) # 10
@ashwin
ashwin / Eval.py
Created May 3, 2012 23:15
eval in Python
x = 1
y = eval( "x + 1" ) # 2
f = eval( "lambda x: x * x" )
g = f( 10 ) # 100
@ashwin
ashwin / 3DMeshToPly.cpp
Created May 4, 2012 03:58
Output 3D mesh to Ply file
#include <fstream>
#include <iostream>
#include <vector>
using namespace std;
struct Point
{
float _p[ 3 ];
};
@ashwin
ashwin / CharTranslate.py
Created May 4, 2012 14:20
Character translation in Python
# Replace 'a' with '1', 'b' with '2' and 'c' with '3'
table = bytes.maketrans( b"abc", b"123" )
s = "abracadabra"
s2 = s.translate( table )
print( s2 ) # 12r131d12r1