Skip to content

Instantly share code, notes, and snippets.

View ashwch's full-sized avatar
😎
Look at me!

Ashwini Chaudhary ashwch

😎
Look at me!
View GitHub Profile
@ashwch
ashwch / bubble_sort.py
Last active December 11, 2015 12:39
Bubble Sort implementation in python 2.7. Worst case: when the list is sorted in reversed order total swaps == total loops == n+(n-1)+(n-2)+...+1 i.e. (n(n+1))/2 complexity : O(n^2) Best case: sorted list total swaps =0 complexity sigma(n)
def bubble_sort(inp_lis):
"""Bubble Sort implementation in python 2.7.
Worst case: when the list is sorted in reversed order
total swaps == total loops == n+(n-1)+(n-2)+...+1 i.e. (n(n+1))/2
complexity : O(n^2)
Best case: sorted list
total swaps =0
complexity sigma(n)
"""
@ashwch
ashwch / pascaltri
Last active December 11, 2015 14:48
Pascal's Triangle
def func(n):
lis=[[1],[1,1]]
if n in (1,2):
return lis[n-1]
for _ in range(n-2):
lis.append([1]+map(sum,zip(lis[-1],lis[-1][1:]))+[1])
return lis[-1]
@ashwch
ashwch / quick_union.py
Created February 9, 2013 08:17
Quick Union algorithm : It is used to check whether two nodes are connected or not. If both nodes have the same root then they are connected else not connected. Connecting two nodes means make one's root point to other's root.
"""
Implementation of Quick Union algorithm in python.
Link: https://class.coursera.org/algs4partI-002/lecture/7
"""
def get_root(lis,item):
"""
return the root of an item
@ashwch
ashwch / weighed_quick_union.py
Last active December 12, 2015 08:29
Weighted quick-union algorithm : This is an improvement of quick union algorithm. In this we keep track of size of a tree and when we merge teo trees we always make the root of smaller tree to point at the root of bigger tree. complexity : O(lg N)
"""
This is an improvement over quick union algorithm. In this we keep track of size
of a tree and when we merge two trees we always make the root of smaller tree
to point at the root of bigger tree.
Link : https://class.coursera.org/algs4partI-002/lecture/8
"""
tree_sz=[1]*10 #initialize a size array
@ashwch
ashwch / sieve.py
Last active December 12, 2015 09:59
Sieve of Eratosthenes
"""
Implementation of Sieve of Eratosthenes in python.
Algorithm descripition : http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
"""
def primes(n):
if n<=2:
return []
sieve=[True]*(n+1)
@ashwch
ashwch / file_copier.py
Created February 11, 2013 18:21
File Copier ( Distributed Systems Lab)
"""
Program to copy the content of one file to another.
"""
def file_try():
filename =raw_input("Enter the name of the file: ")
try:
open(filename)
return filename
except:
@ashwch
ashwch / fibo_dp.cpp
Created February 14, 2013 17:54
Calculate Nth Fibonacci number using DP.
#include <iostream>
using namespace std;
long double fibo_dp(long double);
int main(void){
long double n;
cout <<"enter number :";
cin >> n;
cout <<fibo_dp(n)<<endl;
@ashwch
ashwch / fibo_fast.py
Last active August 9, 2016 07:30
Calculate Nth fibonacci number( Fast approach)
"""
This algorithm is based on the fact that :
F(2n) = F(n)^2 + F(n+1)^2
and similarly :
F(2n+1) = 2*F(n)*F(n+1) + F(n+1)^2
Total steps = lg(N) (base2)
"""
@ashwch
ashwch / second_max.py
Last active December 14, 2015 07:48
Program to find second largest element in a list
"""
Program to find second largest element in a list
"""
def second_max(lis):
if len(lis)==0:
return 'empty list'
if len(lis)==1:
return 'Only 1 item in the list'
if lis[1]>lis[0]:
@ashwch
ashwch / jolly_jumpers.cpp
Created March 3, 2013 00:51
Jolly Jumpers
/*
Author: Ashwini Chaudhary
Problem: Jolly Jumpers (http://goo.gl/O6I2Y)
UVA Online Judge Problem ID:10038
lang :c++
*/
#include<iostream>
#include<algorithm>