Skip to content

Instantly share code, notes, and snippets.

def calculate_coins(c, s):
#sort the coins
c = sorted(c)
# remove the coins with values bigger than s
c = [ci for ci in filter(lambda cv: cv<=s, c)]
# sentinel value to represent that one coin sum is not possible. We can use s + 1 as sentinel value to represent
# that case. Mininum value of a coin is 1 so more than s coins can not make a sum of s
sentinel = s+1
@anupamsharma
anupamsharma / knights_tour_randomized.py
Last active December 31, 2015 10:39
Knights tour (On 8 * 8 Chessboard) using simple backtracking and no heuristic. To make it generate different solution I have randomized the part where it selects the next possible move.
"""
Knights tour (On 8 * 8 Chessboard) using simple backtracking and no heuristic.
To make it generate different solutions I have randomized the part where it selects the next possible move.
"""
import itertools
import random
N = 8
N2 = N*N
@anupamsharma
anupamsharma / python_deco.py
Created December 15, 2013 13:44
Python decorator Examples with arguments example using Function
#Python decorator Example with arguments example using Function
def ed(arg1):
if arg1 == True:
print "decide1"
def real_d(f):
def rfunc(*args, **kwargs):
print "1", arg1
print args
print kwargs
f(args, kwargs)
import random
import math
import time
NUMBER_OF_SIMULATIONS = 1000
import math
def lognormal_mean(mu, sigma):
sigma_s = sigma * sigma
mu_s = mu * mu
@anupamsharma
anupamsharma / astronomy_course_ra.py
Last active December 10, 2015 18:28
Astronomy Course RA course formulas
import math
AU_METER = 1.49597870691e11 #MS
AU_KM = 1.49597870691e8
SEC = 1
MIN = SEC * 60
HOUR = MIN * 60
DAY = 24 * HOUR
YEAR = DAY * 365
G = 6.673e-11
print_seq(n, k):
@anupamsharma
anupamsharma / get_turbogears_exposed_method.py
Created October 17, 2012 16:22
Returns list of all the exposed method objects in the current controller for Python Turbogears
def _get_exposed_methods(self):
"""
Returns list of all the exposed method in the current controller.
"""
#
# TG controller class has _is_exposed method with same implementation but
# that method is private so we are not using it. One could say why we are
# not using the private method inside the class itself. That is because we
@anupamsharma
anupamsharma / pi_using_monte_carlo_simulation.py
Created October 17, 2012 15:04
This is a simple script which shows power of Monte Carlo Simulation. It calculates value of Pie using Monte Carlo Simulation.
import random
square_arm = 10000
radius = 50000
points_in_circle = []
import random
no_of_simulation = 1000000
counter = 0
random.seed(5000)
x_series = []
while (counter < no_of_simulation):
@anupamsharma
anupamsharma / ChessGame.java
Created October 10, 2012 11:59
Simple Multiplayer Chess Game...
public class AbstractPosition {}
protected class ChessPiece {
enum color
enum player
// not really needed
is_alive
protected boolean is_valid_move(int x1, int y1, int x2, int y2);
}
@anupamsharma
anupamsharma / MonteCarloSimulationHelper.py
Last active August 28, 2016 08:23
MonteCarloSimulation
"""
Simple python programme to do monte carlo simulation, calculate var and plot histogram given simple python equation.
Usage:
python mc_simulation_helper.py < sample_input.txt
Sample Input File:
a + 10
10000