Skip to content
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
"githubPullRequests.ignoredPullRequestBranches": [
"master"
]

}
19 changes: 10 additions & 9 deletions other/lru_cache_pythonic.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
"""
This implementation of LRU Cache uses the in-built Python dictionary (dict) which from
Python 3.6 onward maintain the insertion order of keys and ensures O(1) operations on
insert, delete and access. https://docs.python.org/3/library/stdtypes.html#typesmapping
Python 3.6 onward maintains the insertion order of keys and ensures O(1) operations on
insert, delete, and access. https://docs.python.org/3/library/stdtypes.html#typesmapping
"""
from typing import Any, Hashable
from typing import Any


class LRUCache(dict):
def __init__(self, capacity: int) -> None:
"""
Initialize an LRU Cache with given capacity.
capacity : int -> the capacity of the LRU Cache
Initialize an LRU Cache with a given capacity.
capacity: the capacity of the LRU Cache
>>> cache = LRUCache(2)
>>> cache
{}
"""
self.remaining: int = capacity

def get(self, key: Hashable) -> Any:
def get(self, key: Any) -> Any | None:
"""
This method returns the value associated with the key.
key : A hashable object that is mapped to a value in the LRU cache.
key: A hashable object that is mapped to a value in the LRU cache.
return -> Any object that has been stored as a value in the LRU cache.

>>> cache = LRUCache(2)
Expand All @@ -33,15 +33,16 @@ def get(self, key: Hashable) -> Any:
KeyError: '2 not found.'
"""
if key not in self:
raise KeyError(f"{key} not found.")
msg = f"{key} not found."
raise KeyError(msg)
val = self.pop(key) # Pop the key-value and re-insert to maintain the order
self[key] = val
return val

def put(self, key: Hashable, value: Any) -> None:
"""
This method puts the value associated with the key provided in the LRU cache.
key : A hashable object that is mapped to a value in the LRU cache.
key: A hashable object that is mapped to a value in the LRU cache.
value: Any object that is to be associated with the key in the LRU cache.
>>> cache = LRUCache(2)
>>> cache.put(3,3)
Expand Down
40 changes: 40 additions & 0 deletions physics/altitude_pressure.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"""
Title : Calculate altitude using Pressure

Description :
The below algorithm approximates the altitude using Barometric formula


"""


def get_altitude_at_pressure(pressure: float) -> float:
"""
This method calculates the altitude from Pressure wrt to
Sea level pressure as reference .Pressure is in Pascals
https://en.wikipedia.org/wiki/Pressure_altitude
https://community.bosch-sensortec.com/t5/Question-and-answers/How-to-calculate-the-altitude-from-the-pressure-sensor-data/qaq-p/5702

H = 44330 * [1 - (P/p0)^(1/5.255) ]

Where :
H = altitude (m)
P = measured pressure
p0 = reference pressure at sea level 101325 Pa

Examples:

>>> get_altitude_at_pressure(pressure=100000)
105.47836610778828
Copy link
Member

@cclauss cclauss Jul 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please make tests for H ~= sea level, H ~= 100 meters (the current test), H ~= 1,750 (altitude of my house) also a zero pressure, a negative pressure, and a pressure greater than 101,325... The last few should prove that the code raises ValueErrors like...

>>> bisection(lambda x: x ** 3 - 1, 2, 1000)
Traceback (most recent call last):
...
ValueError: could not find root in given interval.

"""

if pressure > 101325:
raise ValueError("Value Higher than Pressure at Sea Level !")

return 44330 * (1 - (pressure / 101325) ** (1 / 5.5255))


if __name__ == "__main__":
import doctest

doctest.testmod()