Skip to content

Data Structures and Algorithms in Python

For full documentation visit mkdocs.org.

Project layout

mkdocs.yml    # The configuration file.
docs/
    index.md  # The documentation homepage.
    ...       # Other markdown pages, images and other files.

Reference

Performs a binary search on the input_list to find the element_to_search, returns its index if found, else returns -1.

Calculate average temperature from multiple measurements

imperative_binary_search([10, 11, 13, 34, 91, 101, 137], 127) -1

imperative_binary_search([10, 11, 13, 34, 91, 127, 138], 127) 5

:param input_list: The list on which binary search is to be performed. :param element_to_search: Element to search for :return: Index of the element if found, otherwise -1.

Source code in dsa/algorithms/divide_and_conquer/binary_search.py
def imperative_binary_search(input_list: List[int], element_to_search: int) -> int:
    """
    Performs a binary search on the `input_list` to find the `element_to_search`,
    returns its index if found, else returns -1.

    Calculate average temperature from multiple measurements

    >>> imperative_binary_search([10, 11, 13, 34, 91, 101, 137], 127)
    -1

    >>> imperative_binary_search([10, 11, 13, 34, 91, 127, 138], 127)
    5

    :param input_list: The list on which binary search is to be performed.
    :param element_to_search: Element to search for
    :return: Index of the element if found, otherwise -1.
    """
    high = len(input_list)
    low = 0
    mid = (high + low) // 2

    while high - low > 1:
        if input_list[mid] == element_to_search:
            return mid

        if element_to_search < input_list[mid]:
            high = mid
            mid = (high + low) // 2
        elif element_to_search > input_list[mid]:
            low = mid
            mid = (high + low) // 2
    else:
        if input_list[low] == element_to_search:
            return low
        else:
            return -1

Performs a binary search on the input_list to find the element_to_search, returns its index if found, else returns -1.

Calculate average temperature from multiple measurements

imperative_binary_search([10, 11, 13, 34, 91, 101, 137], 127) -1

imperative_binary_search([10, 11, 13, 34, 91, 127, 138], 127) 5

:param input_list: The list on which binary search is to be performed. :param element_to_search: Element to search for :return: Index of the element if found, otherwise -1.

Source code in dsa/algorithms/divide_and_conquer/binary_search.py
def recursive_binary_search(input_list: List[int], element_to_search: int) -> int:
    """
    Performs a binary search on the `input_list` to find the `element_to_search`,
    returns its index if found, else returns -1.

    Calculate average temperature from multiple measurements

    >>> imperative_binary_search([10, 11, 13, 34, 91, 101, 137], 127)
    -1

    >>> imperative_binary_search([10, 11, 13, 34, 91, 127, 138], 127)
    5

    :param input_list: The list on which binary search is to be performed.
    :param element_to_search: Element to search for
    :return: Index of the element if found, otherwise -1.
    """

    def _binary_search(low: int, high: int) -> int:
        nonlocal element_to_search
        nonlocal input_list
        if low == high:
            if input_list[low] == element_to_search:
                return low
            else:
                return -1
        else:
            mid = (low + high) // 2
            if element_to_search == input_list[mid]:
                return mid
            if element_to_search < input_list[mid]:
                return _binary_search(low, mid - 1)
            else:
                return _binary_search(mid + 1, high)

    return _binary_search(0, len(input_list))

Details

Nested

Open styled details
Nested details!

And more content again.

Normal

Success

Content.

Warning

Content.

MathJax

\(p(x|y) = \frac{p(y|x)p(x)}{p(y)}\), \(p(x|y) = \frac{p(y|x)p(x)}{p(y)}\).

\[ E(\mathbf{v}, \mathbf{h}) = -\sum_{i,j}w_{ij}v_i h_j - \sum_i b_i v_i - \sum_j c_j h_j \]
\[3 < 4\]
\[\begin{align} p(v_i=1|\mathbf{h}) & = \sigma\left(\sum_j w_{ij}h_j + b_i\right) \\ p(h_j=1|\mathbf{v}) & = \sigma\left(\sum_i w_{ij}v_i + c_j\right) \end{align}\]

Superfence

Flowchart

graph LR A[Start] --> B{Error?}; B -->|Yes| C[Hmm...]; C --> D[Debug]; D --> B; B ---->|No| E[Yay!];

Sequence diagrams

sequenceDiagram Alice->>John: Hello John, how are you? loop Healthcheck John->>John: Fight against hypochondria end Note right of John: Rational thoughts! John-->>Alice: Great! John->>Bob: How about you? Bob-->>John: Jolly good!

State diagrams

stateDiagram-v2 [*] --> Active state Active { [*] --> NumLockOff NumLockOff --> NumLockOn : EvNumLockPressed NumLockOn --> NumLockOff : EvNumLockPressed -- [*] --> CapsLockOff CapsLockOff --> CapsLockOn : EvCapsLockPressed CapsLockOn --> CapsLockOff : EvCapsLockPressed -- [*] --> ScrollLockOff ScrollLockOff --> ScrollLockOn : EvScrollLockPressed ScrollLockOn --> ScrollLockOff : EvScrollLockPressed }

Class diagrams

classDiagram Person <|-- Student Person <|-- Professor Person : +String name Person : +String phoneNumber Person : +String emailAddress Person: +purchaseParkingPass() Address "1" <-- "0..1" Person:lives at class Student{ +int studentNumber +int averageMark +isEligibleToEnrol() +getSeminarsTaken() } class Professor{ +int salary } class Address{ +String street +String city +String state +int postalCode +String country -validate() +outputAsLabel() }

Entity-Relationship diagram

erDiagram CUSTOMER ||--o{ ORDER : places ORDER ||--|{ LINE-ITEM : contains CUSTOMER }|..|{ DELIVERY-ADDRESS : uses
sequenceDiagram participant Alice participant Bob Alice->>John: Hello John, how are you? loop Healthcheck John->>John: Fight against hypochondria end Note right of John: Rational thoughts <br/>prevail! John-->>Alice: Great! John->>Bob: How about you? Bob-->>John: Jolly good!

Large Diagram

  • https://mermaid-js.github.io/mermaid/#/examples?id=basic-flowchart
    graph TB sq[Square shape] --> ci((Circle shape)) subgraph A od>Odd shape]-- Two line<br/>edge comment --> ro di{Diamond with <br/> line break} -.-> ro(Rounded<br>square<br>shape) di==>ro2(Rounded square shape) end %% Notice that no text in shape are added here instead that is appended further down e --> od3>Really long text with linebreak<br>in an Odd shape] %% Comments after double percent signs e((Inner / circle<br>and some odd <br>special characters)) --> f(,.?!+-*ز) cyr[Cyrillic]-->cyr2((Circle shape Начало)); classDef green fill:#9f6,stroke:#333,stroke-width:2px; classDef orange fill:#f96,stroke:#333,stroke-width:4px; class sq,e green class di orange
Implementation in various languages
#include <stdio.h>

int main(void) {
  printf("Hello world!\n");
  return 0;
}
#include <iostream>

int main(void) {
  std::cout << "Hello world!" << std::endl;
  return 0;
}
def main() -> int:
  print("Hello world!")
  return 0
theme:
  features:
    - content.code.annotate # (1)
  1. 🙋‍♂️ I'm a code annotation! I can contain code, formatted text, images, ... basically anything that can be expressed in Markdown.