Skip to content

Instantly share code, notes, and snippets.

@rahul8590
rahul8590 / simple_sampling.py
Last active December 19, 2015 10:59
In order to sample continuous stream of data (numbers). We need to pick up number whos probability of choosing is 1/ (n+1).
#sl is a sample list of numbers which is contiguously getting updated.
import random
def simple_sample(sl):
for x,y in enumerate(sl,1):
if random.random() < 1.0 / x:
sample_data = y
return sample_data
@rahul8590
rahul8590 / python_scale.py
Created June 20, 2013 07:11
This is the python program for the scale.cpp program.
#Run will execute the run function on scale.cpp
from ctypes import cdll
lib = cdll.LoadLibrary('./libscale.so')
class Foo(object):
def __init__(self):
self.obj = lib.Foo_new()
def run(self):
lib.Foo_run(self.obj)
f = Foo()
@rahul8590
rahul8590 / scale.cpp
Created June 20, 2013 07:05
The Scale program uses the Boost::ASIO libraries in order to execute the functions in async fashion. It also uses mutiple threads to have io.services run in each of them. I have Interfacing the C++ code with C wrapper .
#include <boost/asio.hpp>
#include <boost/thread.hpp>
#include <iostream>
class Foo {
@rahul8590
rahul8590 / sample_asio.cpp
Created June 18, 2013 14:42
Just goofing around Boost ASIO library . The following is the first attempt to mess with it.
// You also need to link pthread and boost_system in order to run it. The following will be the command , if you are too lazy to
// type
// $ g++ sample_asio.cpp -o asio -lpthread -lboost_system
//
#include <boost/asio.hpp>
#include <iostream>
using namespace std;
""" This Small snippet will list the set of functions present in the module
__doc__
__file__
__name__
__package__
information about the module.
Hello module used in here is a simple py file for Bottle framework.
"""
#I have written a small snippet to open a given url which doest open on first time and i need to
#Keep refreshing it.
import webbroswer
import time
url = "the url u want to enter"
for i in range(5):
webbrowser.open_new_tab(url)
@rahul8590
rahul8590 / rusmulti.cpp
Created March 8, 2013 16:49
Its a simple implementation of Russians Peasant multiplication algorithm.
#include<iostream>
using namespace std;
int main()
{
int a,b,c=0;
cin >> a >> b;
while(a)
{
if(a&1)
@rahul8590
rahul8590 / lettercount.py
Created February 27, 2013 13:50
Have the function LetterCountI(str) take the str parameter being passed and return the first word with the greatest number of repeated letters. For example: "Today, is the greatest day ever!" should return greatest because it has 2 e's (and 2 t's) and it comes before ever which also has 2 e's. If there are no words with repeating letters return …
def LetterCountI(str):
l = []
index = 0
for k,i in enumerate(str.split(' ')):
d = {}
for j,c in enumerate(i):
d[c] = d.get(c,0) + 1
d = {key: value for key, value in d.items() if value != 1}
if (not l):
@rahul8590
rahul8590 / repeated.py
Created February 27, 2013 13:01
If you want a dictonary consisting of repeating characters only for a given string in python.
# To get a dictionary of repeated characters in a given string
some_dict = {}
for i,c in enumerate("testing"):
some_dict[c] = some_dict.get(c,0) + 1
some_dict = {key: value for key, value in some_dict.items() if value != 1}
f = open('data.txt' ,'r')
import re
d = {}
title = ""
for i in f:
j = i.split("\n")
if j[0] != '#' and j[0] != '':
if re.match('#',j[0]):
title = j[0]
title_info = []