Skip to content

Instantly share code, notes, and snippets.

@theelous3
Last active March 7, 2023 13:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save theelous3/410f57bf9aeae563ff259587d06ffc9c to your computer and use it in GitHub Desktop.
Save theelous3/410f57bf9aeae563ff259587d06ffc9c to your computer and use it in GitHub Desktop.
from typing import List
from dataclasses import dataclass
from numbers import Number
@dataclass
class Liquid:
volume: int
mg_per_ml: Number
def wat(*args: List[Liquid]) -> Liquid:
current_volume = 0
current_total_mg = 0
liquids_count = 0
for l_num, liquid in enumerate(args, start=1):
print(f"Liquid #{l_num} -> using {liquid.volume}ml @ {liquid.mg_per_ml}mg per ml.")
adding_mg = liquid.volume * liquid.mg_per_ml
current_volume += liquid.volume
current_total_mg += adding_mg
liquids_count += 1
print("Total volume:", current_volume)
print("Total mg:", current_total_mg)
print("Result:", current_total_mg / current_volume, "mg/ml")
l1 = Liquid(10, 6)
l2 = Liquid(15, 0)
wat(l1, l2)
l3 = Liquid(5, 20)
wat(l1, l2, l3)
>>> wat(l1, l2, l3)
Liquid #1 -> using 10ml @ 9mg per ml.
Liquid #2 -> using 15ml @ 0mg per ml.
Liquid #3 -> using 7ml @ 20mg per ml.
Total volume: 32
Total mg: 230
Result: 7.1875 mg/ml
^ last batch, poured 10ml (remove from each proportionately)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment