Giter Site home page Giter Site logo

python-algorithms's Introduction

Python-algorithms

efficient algorithms for general tasks with good time complexity.

To send a Pull-request:

  • Check our contribution guidelines in CONTRIBUTING.md
  • Star and Fork this repo.
  • Clone the repo on your local machine using git clone https://github.com/{your-username}/python-algorithms.git.
  • Create a branch git checkout -b your_new_branch_name
  • Make required changes then add and commit
    git add.
    git commit -m "your commit message"
  • Push your changes git push origin your_new_branch_name
  • Merge and send a Pull-request.

Referance: Competetive programming with python


bitwise operator not {~} : (a = 10 => 1010 (Binary) => ~a = ~1010 = -(1010 + 1) = -(1011) = -11)

bitwise operator xor {^} : (n^n = 0), (n^0 = n)

bitwise operator rightshift {>>} : (100 >> 2 = 25)

bitwise operator leftshift {<<} : (100 << 2 = 400)


sum of n numbers:O(1)

def sum_total(n):
    return int(n*(n+1)/2)

LCM/GCD:(Euclid's algorithm)

def gcd(a,b):
    if a == 0:
        return b
    return gcd(b%a,a)

def lcm(a,b):
    prod = a*b
    hcf = gcd(a,b)
    return prod//hcf

Odd-Even:O(1)

if n&1 == 1:
    print('odd')
else:
    print('even')

Leftshift(multiply) / Rightshift(divide) by 2n:O(1)

def multpow(x,y):
    return x<<y  # x*(2^y)
def divpow(x,y):
    return x>>y # x/(2^y)

Check if a number is power of 2:O(1)

def ispow(n):
    if n <= 0:
        return False
    x = n
    y = not(n & (n-1))
    return x and y

count 1's in binary representation:O(log(n))

def cntbits(n):
    cnt = 0
    while n:
        cnt += 1
        n = n & (n-1)
    return cnt

convert int to binary / binary to int:O(1)

def inttobin(n):
    return str(bin(n))[2:]

def bintoint(m):
    return int(m,2)

check which number occurs once(or odd number of times/doesn't has it's unique identical element) in an array:O(n)

def checkpair(arr): # n -> aray
    temp = arr[0]
    for i in range(1,len(arr)):
        temp = temp ^ arr[i]
    return temp

python-algorithms's People

Contributors

tanmay-901 avatar x0rd3v1l avatar hardikkhurana avatar aakash1234567 avatar d3xt3r0 avatar kelvinchanwh avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.