Skip to content

Commit 0569c19

Browse files
FirePing32github-actionscclauss
authored andcommitted
Add molecular_chemistry.py (TheAlgorithms#2944)
* Create molecular_chemistry.py * round up outputs * Remove floating point * Add Wikipedia references * fixup! Format Python code with psf/black push * Add Conversions/Molecular Chemistry * updating DIRECTORY.md * Update molecular_chemistry.py Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Co-authored-by: Christian Clauss <[email protected]>
1 parent 17e9e6d commit 0569c19

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

Diff for: DIRECTORY.md

+1
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@
100100
* [Decimal To Hexadecimal](https://github.com/TheAlgorithms/Python/blob/master/conversions/decimal_to_hexadecimal.py)
101101
* [Decimal To Octal](https://github.com/TheAlgorithms/Python/blob/master/conversions/decimal_to_octal.py)
102102
* [Hexadecimal To Decimal](https://github.com/TheAlgorithms/Python/blob/master/conversions/hexadecimal_to_decimal.py)
103+
* [Molecular Chemistry](https://github.com/TheAlgorithms/Python/blob/master/conversions/molecular_chemistry.py)
103104
* [Prefix Conversions](https://github.com/TheAlgorithms/Python/blob/master/conversions/prefix_conversions.py)
104105
* [Roman To Integer](https://github.com/TheAlgorithms/Python/blob/master/conversions/roman_to_integer.py)
105106
* [Temperature Conversions](https://github.com/TheAlgorithms/Python/blob/master/conversions/temperature_conversions.py)

Diff for: conversions/molecular_chemistry.py

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
"""
2+
Functions useful for doing molecular chemistry:
3+
* molarity_to_normality
4+
* moles_to_pressure
5+
* moles_to_volume
6+
* pressure_and_volume_to_temperature
7+
"""
8+
9+
10+
def molarity_to_normality(nfactor: int, moles: float, volume: float) -> float:
11+
"""
12+
Convert molarity to normality.
13+
Volume is taken in litres.
14+
15+
Wikipedia reference: https://en.wikipedia.org/wiki/Equivalent_concentration
16+
Wikipedia reference: https://en.wikipedia.org/wiki/Molar_concentration
17+
18+
>>> molarity_to_normality(2, 3.1, 0.31)
19+
20
20+
>>> molarity_to_normality(4, 11.4, 5.7)
21+
8
22+
"""
23+
return round((float(moles / volume) * nfactor))
24+
25+
26+
def moles_to_pressure(volume: float, moles: float, temperature: float) -> float:
27+
"""
28+
Convert moles to pressure.
29+
Ideal gas laws are used.
30+
Temperature is taken in kelvin.
31+
Volume is taken in litres.
32+
Pressure has atm as SI unit.
33+
34+
Wikipedia reference: https://en.wikipedia.org/wiki/Gas_laws
35+
Wikipedia reference: https://en.wikipedia.org/wiki/Pressure
36+
Wikipedia reference: https://en.wikipedia.org/wiki/Temperature
37+
38+
>>> moles_to_pressure(0.82, 3, 300)
39+
90
40+
>>> moles_to_pressure(8.2, 5, 200)
41+
10
42+
"""
43+
return round(float((moles * 0.0821 * temperature) / (volume)))
44+
45+
46+
def moles_to_volume(pressure: float, moles: float, temperature: float) -> float:
47+
"""
48+
Convert moles to volume.
49+
Ideal gas laws are used.
50+
Temperature is taken in kelvin.
51+
Volume is taken in litres.
52+
Pressure has atm as SI unit.
53+
54+
Wikipedia reference: https://en.wikipedia.org/wiki/Gas_laws
55+
Wikipedia reference: https://en.wikipedia.org/wiki/Pressure
56+
Wikipedia reference: https://en.wikipedia.org/wiki/Temperature
57+
58+
>>> moles_to_volume(0.82, 3, 300)
59+
90
60+
>>> moles_to_volume(8.2, 5, 200)
61+
10
62+
"""
63+
return round(float((moles * 0.0821 * temperature) / (pressure)))
64+
65+
66+
def pressure_and_volume_to_temperature(
67+
pressure: float, moles: float, volume: float
68+
) -> float:
69+
"""
70+
Convert pressure and volume to temperature.
71+
Ideal gas laws are used.
72+
Temperature is taken in kelvin.
73+
Volume is taken in litres.
74+
Pressure has atm as SI unit.
75+
76+
Wikipedia reference: https://en.wikipedia.org/wiki/Gas_laws
77+
Wikipedia reference: https://en.wikipedia.org/wiki/Pressure
78+
Wikipedia reference: https://en.wikipedia.org/wiki/Temperature
79+
80+
>>> pressure_and_volume_to_temperature(0.82, 1, 2)
81+
20
82+
>>> pressure_and_volume_to_temperature(8.2, 5, 3)
83+
60
84+
"""
85+
return round(float((pressure * volume) / (0.0821 * moles)))
86+
87+
88+
if __name__ == "__main__":
89+
90+
import doctest
91+
92+
doctest.testmod()

0 commit comments

Comments
 (0)