This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | |
| """ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| 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: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| 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) | |
| """ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| 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]: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| Author: Ashwini Chaudhary | |
| Problem: Jolly Jumpers (http://goo.gl/O6I2Y) | |
| UVA Online Judge Problem ID:10038 | |
| lang :c++ | |
| */ | |
| #include<iostream> | |
| #include<algorithm> |
OlderNewer