From d54b76982cf1b49fc9bf6d9a0b86cb187355a387 Mon Sep 17 00:00:00 2001 From: Prakhar Gurunani Date: Tue, 6 Oct 2020 23:37:59 +0530 Subject: [PATCH 1/8] Create molecular_chemistry.py --- conversions/molecular_chemistry.py | 76 ++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 conversions/molecular_chemistry.py diff --git a/conversions/molecular_chemistry.py b/conversions/molecular_chemistry.py new file mode 100644 index 000000000000..bd374aad467d --- /dev/null +++ b/conversions/molecular_chemistry.py @@ -0,0 +1,76 @@ +def molarity_to_normality(nfactor: int, moles: float, volume: float) -> float: + """ + Convert molarity to normality. + Volume is taken in litres. + + Wikipedia reference: https://en.wikipedia.org/wiki/Equivalent_concentration + Wikipedia reference: https://en.wikipedia.org/wiki/Molar_concentration + + >>> molarity_to_normality(2, 3.1, 0.31) + 20.0 + >>> molarity_to_normality(4, 11.4, 5.7) + 8.0 + + """ + return (float(moles/volume) * nfactor) + +def moles_to_pressure(volume: float, moles: float, temperature: float) -> float: + """ + Convert moles to pressure. + Ideal gas laws are used. + Temperature is taken in kelvin. + Volume is taken in litres. + Pressure has atm as SI unit. + + Wikipedia reference: https://en.wikipedia.org/wiki/Gas_laws + + >>> moles_to_pressure(0.82, 3, 300) + 90 + >>> moles_to_pressure(8.2, 5, 200) + 10 + + """ + return float((moles * 0.0821 * temperature)/(volume)) + +def moles_to_volume(pressure: float, moles: float, temperature: float) -> float: + """ + Convert moles to volume. + Ideal gas laws are used. + Temperature is taken in kelvin. + Volume is taken in litres. + Pressure has atm as SI unit. + + Wikipedia reference: https://en.wikipedia.org/wiki/Gas_laws + + >>> moles_to_volume(0.82, 3, 300) + 90 + >>> moles_to_volume(8.2, 5, 200) + 10 + + """ + return float((moles * 0.0821 * temperature)/(pressure)) + +def pressure_and_volume_to_temperature(pressure: float, moles: float, volume: float) -> float: + """ + Convert pressure and volume to temperature. + Ideal gas laws are used. + Temperature is taken in kelvin. + Volume is taken in litres. + Pressure has atm as SI unit. + + Wikipedia reference: https://en.wikipedia.org/wiki/Gas_laws + + >>> pressure_and_volume_to_temperature(0.82, 1, 2) + 20 + >>> pressure_and_volume_to_temperature(8.2, 5, 3) + 60 + + """ + return float((pressure * volume)/(0.0821 * moles)) + + +if __name__ == "__main__": + + import doctest + doctest.testmod() + From 7b3758d8f94a0815d596b7cc702b50e43df7d48e Mon Sep 17 00:00:00 2001 From: Prakhar Gurunani Date: Tue, 6 Oct 2020 23:48:09 +0530 Subject: [PATCH 2/8] round up outputs --- conversions/molecular_chemistry.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/conversions/molecular_chemistry.py b/conversions/molecular_chemistry.py index bd374aad467d..2a9e5e512e73 100644 --- a/conversions/molecular_chemistry.py +++ b/conversions/molecular_chemistry.py @@ -12,7 +12,7 @@ def molarity_to_normality(nfactor: int, moles: float, volume: float) -> float: 8.0 """ - return (float(moles/volume) * nfactor) + return round((float(moles/volume) * nfactor)) def moles_to_pressure(volume: float, moles: float, temperature: float) -> float: """ @@ -30,7 +30,7 @@ def moles_to_pressure(volume: float, moles: float, temperature: float) -> float: 10 """ - return float((moles * 0.0821 * temperature)/(volume)) + return round(float((moles * 0.0821 * temperature)/(volume))) def moles_to_volume(pressure: float, moles: float, temperature: float) -> float: """ @@ -48,7 +48,7 @@ def moles_to_volume(pressure: float, moles: float, temperature: float) -> float: 10 """ - return float((moles * 0.0821 * temperature)/(pressure)) + return round(float((moles * 0.0821 * temperature)/(pressure))) def pressure_and_volume_to_temperature(pressure: float, moles: float, volume: float) -> float: """ @@ -66,7 +66,7 @@ def pressure_and_volume_to_temperature(pressure: float, moles: float, volume: fl 60 """ - return float((pressure * volume)/(0.0821 * moles)) + return round(float((pressure * volume)/(0.0821 * moles))) if __name__ == "__main__": From 0a01a3c43f777debf961335c27e532894e38c6af Mon Sep 17 00:00:00 2001 From: Prakhar Gurunani Date: Tue, 6 Oct 2020 23:53:01 +0530 Subject: [PATCH 3/8] Remove floating point --- conversions/molecular_chemistry.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/conversions/molecular_chemistry.py b/conversions/molecular_chemistry.py index 2a9e5e512e73..618f062ff15d 100644 --- a/conversions/molecular_chemistry.py +++ b/conversions/molecular_chemistry.py @@ -7,9 +7,9 @@ def molarity_to_normality(nfactor: int, moles: float, volume: float) -> float: Wikipedia reference: https://en.wikipedia.org/wiki/Molar_concentration >>> molarity_to_normality(2, 3.1, 0.31) - 20.0 + 20 >>> molarity_to_normality(4, 11.4, 5.7) - 8.0 + 8 """ return round((float(moles/volume) * nfactor)) From b460da4edeab4721e95a8a31a4cc6e9d44328ada Mon Sep 17 00:00:00 2001 From: Prakhar Gurunani Date: Wed, 7 Oct 2020 08:16:56 +0530 Subject: [PATCH 4/8] Add Wikipedia references --- conversions/molecular_chemistry.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/conversions/molecular_chemistry.py b/conversions/molecular_chemistry.py index 618f062ff15d..7d85a5c000c0 100644 --- a/conversions/molecular_chemistry.py +++ b/conversions/molecular_chemistry.py @@ -23,6 +23,8 @@ def moles_to_pressure(volume: float, moles: float, temperature: float) -> float: Pressure has atm as SI unit. Wikipedia reference: https://en.wikipedia.org/wiki/Gas_laws + Wikipedia reference: https://en.wikipedia.org/wiki/Pressure + Wikipedia reference: https://en.wikipedia.org/wiki/Temperature >>> moles_to_pressure(0.82, 3, 300) 90 @@ -41,6 +43,8 @@ def moles_to_volume(pressure: float, moles: float, temperature: float) -> float: Pressure has atm as SI unit. Wikipedia reference: https://en.wikipedia.org/wiki/Gas_laws + Wikipedia reference: https://en.wikipedia.org/wiki/Pressure + Wikipedia reference: https://en.wikipedia.org/wiki/Temperature >>> moles_to_volume(0.82, 3, 300) 90 @@ -59,6 +63,8 @@ def pressure_and_volume_to_temperature(pressure: float, moles: float, volume: fl Pressure has atm as SI unit. Wikipedia reference: https://en.wikipedia.org/wiki/Gas_laws + Wikipedia reference: https://en.wikipedia.org/wiki/Pressure + Wikipedia reference: https://en.wikipedia.org/wiki/Temperature >>> pressure_and_volume_to_temperature(0.82, 1, 2) 20 From 965e85a1bb4ee2bb0bb66461f4c07e86ae04e6ee Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Wed, 7 Oct 2020 02:48:10 +0000 Subject: [PATCH 5/8] fixup! Format Python code with psf/black push --- conversions/molecular_chemistry.py | 137 +++++++++++++++-------------- 1 file changed, 71 insertions(+), 66 deletions(-) diff --git a/conversions/molecular_chemistry.py b/conversions/molecular_chemistry.py index 7d85a5c000c0..2c15a58e3a3a 100644 --- a/conversions/molecular_chemistry.py +++ b/conversions/molecular_chemistry.py @@ -1,82 +1,87 @@ def molarity_to_normality(nfactor: int, moles: float, volume: float) -> float: - """ - Convert molarity to normality. - Volume is taken in litres. + """ + Convert molarity to normality. + Volume is taken in litres. - Wikipedia reference: https://en.wikipedia.org/wiki/Equivalent_concentration - Wikipedia reference: https://en.wikipedia.org/wiki/Molar_concentration + Wikipedia reference: https://en.wikipedia.org/wiki/Equivalent_concentration + Wikipedia reference: https://en.wikipedia.org/wiki/Molar_concentration - >>> molarity_to_normality(2, 3.1, 0.31) - 20 - >>> molarity_to_normality(4, 11.4, 5.7) - 8 + >>> molarity_to_normality(2, 3.1, 0.31) + 20 + >>> molarity_to_normality(4, 11.4, 5.7) + 8 + + """ + return round((float(moles / volume) * nfactor)) - """ - return round((float(moles/volume) * nfactor)) def moles_to_pressure(volume: float, moles: float, temperature: float) -> float: - """ - Convert moles to pressure. - Ideal gas laws are used. - Temperature is taken in kelvin. - Volume is taken in litres. - Pressure has atm as SI unit. + """ + Convert moles to pressure. + Ideal gas laws are used. + Temperature is taken in kelvin. + Volume is taken in litres. + Pressure has atm as SI unit. + + Wikipedia reference: https://en.wikipedia.org/wiki/Gas_laws + Wikipedia reference: https://en.wikipedia.org/wiki/Pressure + Wikipedia reference: https://en.wikipedia.org/wiki/Temperature - Wikipedia reference: https://en.wikipedia.org/wiki/Gas_laws - Wikipedia reference: https://en.wikipedia.org/wiki/Pressure - Wikipedia reference: https://en.wikipedia.org/wiki/Temperature + >>> moles_to_pressure(0.82, 3, 300) + 90 + >>> moles_to_pressure(8.2, 5, 200) + 10 - >>> moles_to_pressure(0.82, 3, 300) - 90 - >>> moles_to_pressure(8.2, 5, 200) - 10 + """ + return round(float((moles * 0.0821 * temperature) / (volume))) - """ - return round(float((moles * 0.0821 * temperature)/(volume))) def moles_to_volume(pressure: float, moles: float, temperature: float) -> float: - """ - Convert moles to volume. - Ideal gas laws are used. - Temperature is taken in kelvin. - Volume is taken in litres. - Pressure has atm as SI unit. - - Wikipedia reference: https://en.wikipedia.org/wiki/Gas_laws - Wikipedia reference: https://en.wikipedia.org/wiki/Pressure - Wikipedia reference: https://en.wikipedia.org/wiki/Temperature - - >>> moles_to_volume(0.82, 3, 300) - 90 - >>> moles_to_volume(8.2, 5, 200) - 10 - - """ - return round(float((moles * 0.0821 * temperature)/(pressure))) - -def pressure_and_volume_to_temperature(pressure: float, moles: float, volume: float) -> float: - """ - Convert pressure and volume to temperature. - Ideal gas laws are used. - Temperature is taken in kelvin. - Volume is taken in litres. - Pressure has atm as SI unit. - - Wikipedia reference: https://en.wikipedia.org/wiki/Gas_laws - Wikipedia reference: https://en.wikipedia.org/wiki/Pressure - Wikipedia reference: https://en.wikipedia.org/wiki/Temperature - - >>> pressure_and_volume_to_temperature(0.82, 1, 2) - 20 - >>> pressure_and_volume_to_temperature(8.2, 5, 3) - 60 - - """ - return round(float((pressure * volume)/(0.0821 * moles))) + """ + Convert moles to volume. + Ideal gas laws are used. + Temperature is taken in kelvin. + Volume is taken in litres. + Pressure has atm as SI unit. + + Wikipedia reference: https://en.wikipedia.org/wiki/Gas_laws + Wikipedia reference: https://en.wikipedia.org/wiki/Pressure + Wikipedia reference: https://en.wikipedia.org/wiki/Temperature + + >>> moles_to_volume(0.82, 3, 300) + 90 + >>> moles_to_volume(8.2, 5, 200) + 10 + + """ + return round(float((moles * 0.0821 * temperature) / (pressure))) + + +def pressure_and_volume_to_temperature( + pressure: float, moles: float, volume: float +) -> float: + """ + Convert pressure and volume to temperature. + Ideal gas laws are used. + Temperature is taken in kelvin. + Volume is taken in litres. + Pressure has atm as SI unit. + + Wikipedia reference: https://en.wikipedia.org/wiki/Gas_laws + Wikipedia reference: https://en.wikipedia.org/wiki/Pressure + Wikipedia reference: https://en.wikipedia.org/wiki/Temperature + + >>> pressure_and_volume_to_temperature(0.82, 1, 2) + 20 + >>> pressure_and_volume_to_temperature(8.2, 5, 3) + 60 + + """ + return round(float((pressure * volume) / (0.0821 * moles))) if __name__ == "__main__": - import doctest - doctest.testmod() + import doctest + doctest.testmod() From 474de77ab6efe6640df05d30230333c7ed5a48d4 Mon Sep 17 00:00:00 2001 From: Prakhar Gurunani Date: Wed, 7 Oct 2020 08:29:45 +0530 Subject: [PATCH 6/8] Add Conversions/Molecular Chemistry --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 6a3d31709ed6..1224b2e38c66 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -97,6 +97,7 @@ * [Prefix Conversions](https://github.com/TheAlgorithms/Python/blob/master/conversions/prefix_conversions.py) * [Roman To Integer](https://github.com/TheAlgorithms/Python/blob/master/conversions/roman_to_integer.py) * [Temperature Conversions](https://github.com/TheAlgorithms/Python/blob/master/conversions/temperature_conversions.py) + * [Molecular Chemistry](https://github.com/TheAlgorithms/Python/blob/master/conversions/molecular_chemistry.py) ## Data Structures * Binary Tree From c37694103bd03391e6b2241b1f9673d848e56e13 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Wed, 7 Oct 2020 03:00:10 +0000 Subject: [PATCH 7/8] updating DIRECTORY.md --- DIRECTORY.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index 1224b2e38c66..f3fcd4a23786 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -94,10 +94,10 @@ * [Decimal To Hexadecimal](https://github.com/TheAlgorithms/Python/blob/master/conversions/decimal_to_hexadecimal.py) * [Decimal To Octal](https://github.com/TheAlgorithms/Python/blob/master/conversions/decimal_to_octal.py) * [Hexadecimal To Decimal](https://github.com/TheAlgorithms/Python/blob/master/conversions/hexadecimal_to_decimal.py) + * [Molecular Chemistry](https://github.com/TheAlgorithms/Python/blob/master/conversions/molecular_chemistry.py) * [Prefix Conversions](https://github.com/TheAlgorithms/Python/blob/master/conversions/prefix_conversions.py) * [Roman To Integer](https://github.com/TheAlgorithms/Python/blob/master/conversions/roman_to_integer.py) * [Temperature Conversions](https://github.com/TheAlgorithms/Python/blob/master/conversions/temperature_conversions.py) - * [Molecular Chemistry](https://github.com/TheAlgorithms/Python/blob/master/conversions/molecular_chemistry.py) ## Data Structures * Binary Tree From 47ba8e5011ba8c557729651c1681bc3d1ccf60c3 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Tue, 10 Nov 2020 08:13:39 +0100 Subject: [PATCH 8/8] Update molecular_chemistry.py --- conversions/molecular_chemistry.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/conversions/molecular_chemistry.py b/conversions/molecular_chemistry.py index 2c15a58e3a3a..8c68459965b0 100644 --- a/conversions/molecular_chemistry.py +++ b/conversions/molecular_chemistry.py @@ -1,3 +1,12 @@ +""" +Functions useful for doing molecular chemistry: +* molarity_to_normality +* moles_to_pressure +* moles_to_volume +* pressure_and_volume_to_temperature +""" + + def molarity_to_normality(nfactor: int, moles: float, volume: float) -> float: """ Convert molarity to normality. @@ -10,7 +19,6 @@ def molarity_to_normality(nfactor: int, moles: float, volume: float) -> float: 20 >>> molarity_to_normality(4, 11.4, 5.7) 8 - """ return round((float(moles / volume) * nfactor)) @@ -31,7 +39,6 @@ def moles_to_pressure(volume: float, moles: float, temperature: float) -> float: 90 >>> moles_to_pressure(8.2, 5, 200) 10 - """ return round(float((moles * 0.0821 * temperature) / (volume))) @@ -52,7 +59,6 @@ def moles_to_volume(pressure: float, moles: float, temperature: float) -> float: 90 >>> moles_to_volume(8.2, 5, 200) 10 - """ return round(float((moles * 0.0821 * temperature) / (pressure))) @@ -75,7 +81,6 @@ def pressure_and_volume_to_temperature( 20 >>> pressure_and_volume_to_temperature(8.2, 5, 3) 60 - """ return round(float((pressure * volume) / (0.0821 * moles)))