Coder Social home page Coder Social logo

python-algorithms-exercise-practice's Introduction

Python Algorithms Exercise Practice

๐Ÿ Exercise solutions for chapter 1.1 written in Python

Algorithms Implemented

  1. is_between_zero_and_one()
  2. to_binary_string()
  3. print_two_dm_boolean_array()
  4. print_two_dm_int_array()
  5. print_int_array()
  6. matrix_transposition()
  7. lg()
  8. Fibonacci.fib()
  9. Fibonacci.fast_fib()
  10. fact()
  11. binary_search()
  12. brute_force_search()

Code Examples

One interesting implementation is this algorithm to compute fibonacci sequences (exercise 1.1.19). It runs in O(1) time provided that the cache contains the previous two sequences. Generating the previous sequences is usually done using loops.

    @staticmethod
    def fast_fib(n: int, cache: {int: int}) -> int:
        """Fibonacci sequence algorithm utilizing dynamic programing and memoization"""
        if n == 0:
            cache[n] = 0
            return 0

        if n == 1:
            cache[n] = 1
            return 1

        cache[n] = cache[n - 2] + cache[n - 1]
        return cache[n]

It's also worth noting that I've implemented brute force search recursively here as opposed to the iterative approach in the Java source

    def brute_force_search(key: int, the_array: [int], index: int) -> int:
        """Returns the index of the key if present, otherwise -1 using brute force search"""
        if index == len(the_array):
            return -1

        if the_array[index] == key:
            return index

        return brute_force_search(key, the_array, index + 1)

Java Implementation

These were originally implemented in Java. You can find the source here

python-algorithms-exercise-practice's People

Contributors

dev-xero avatar

Stargazers

 avatar  avatar

Watchers

 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.