Skip to content

Instantly share code, notes, and snippets.

Avatar

Ashwin Nanjappa ashwin

View GitHub Profile
@ashwin
ashwin / boost_dfs_example.cpp
Created Jul 26, 2008
Example of boost DFS on an undirected graph
View boost_dfs_example.cpp
// 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;
View gist:2421530
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
Range of integers cached by Python
View PyIntCacheRange.py
# 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
Generate random float in Python
View RandomFloat.py
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
Function name in Python
View FunctionName.py
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
Generator function in Python
View GeneratorFunction.py
# 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
Adding function attributes in Python
View AddFunctionAttribute.py
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
eval in Python
View Eval.py
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
Output 3D mesh to Ply file
View 3DMeshToPly.cpp
#include <fstream>
#include <iostream>
#include <vector>
using namespace std;
struct Point
{
float _p[ 3 ];
};
@ashwin
ashwin / CharTranslate.py
Created May 4, 2012
Character translation in Python
View CharTranslate.py
# 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
You can’t perform that action at this time.