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
imperative_binary_search(input_list, element_to_search)
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
recursive_binary_search(input_list, element_to_search)
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)}\).
Superfence
Flowchart
Sequence diagrams
State diagrams
Class diagrams
Entity-Relationship diagram
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
- I'm a code annotation! I can contain
code
, formatted text, images, ... basically anything that can be expressed in Markdown.