-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
Energy conversions #8801
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Energy conversions #8801
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
37f5771
Create TestShiva
ShivaDahal99 baf8d49
Delete TestShiva
ShivaDahal99 99013f8
Merge branch 'TheAlgorithms:master' into master
jlhuhn 6660871
Create energy_conversions.py
jlhuhn 9dc97e0
Update conversions/energy_conversions.py
jlhuhn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
""" | ||
Conversion of energy units. | ||
|
||
Available units: joule, kilojoule, megajoule, gigajoule,\ | ||
wattsecond, watthour, kilowatthour, newtonmeter, calorie_nutr,\ | ||
kilocalorie_nutr, electronvolt, britishthermalunit_it, footpound | ||
|
||
USAGE : | ||
-> Import this file into their respective project. | ||
-> Use the function energy_conversion() for conversion of energy units. | ||
-> Parameters : | ||
-> from_type : From which type you want to convert | ||
-> to_type : To which type you want to convert | ||
-> value : the value which you want to convert | ||
|
||
REFERENCES : | ||
-> Wikipedia reference: https://en.wikipedia.org/wiki/Units_of_energy | ||
-> Wikipedia reference: https://en.wikipedia.org/wiki/Joule | ||
-> Wikipedia reference: https://en.wikipedia.org/wiki/Kilowatt-hour | ||
-> Wikipedia reference: https://en.wikipedia.org/wiki/Newton-metre | ||
-> Wikipedia reference: https://en.wikipedia.org/wiki/Calorie | ||
-> Wikipedia reference: https://en.wikipedia.org/wiki/Electronvolt | ||
-> Wikipedia reference: https://en.wikipedia.org/wiki/British_thermal_unit | ||
-> Wikipedia reference: https://en.wikipedia.org/wiki/Foot-pound_(energy) | ||
-> Unit converter reference: https://www.unitconverters.net/energy-converter.html | ||
""" | ||
|
||
ENERGY_CONVERSION: dict[str, float] = { | ||
"joule": 1.0, | ||
"kilojoule": 1_000, | ||
"megajoule": 1_000_000, | ||
"gigajoule": 1_000_000_000, | ||
"wattsecond": 1.0, | ||
"watthour": 3_600, | ||
"kilowatthour": 3_600_000, | ||
"newtonmeter": 1.0, | ||
"calorie_nutr": 4_186.8, | ||
"kilocalorie_nutr": 4_186_800.00, | ||
"electronvolt": 1.602_176_634e-19, | ||
"britishthermalunit_it": 1_055.055_85, | ||
"footpound": 1.355_818, | ||
} | ||
|
||
|
||
def energy_conversion(from_type: str, to_type: str, value: float) -> float: | ||
""" | ||
Conversion of energy units. | ||
>>> energy_conversion("joule", "joule", 1) | ||
1.0 | ||
>>> energy_conversion("joule", "kilojoule", 1) | ||
0.001 | ||
>>> energy_conversion("joule", "megajoule", 1) | ||
1e-06 | ||
>>> energy_conversion("joule", "gigajoule", 1) | ||
1e-09 | ||
>>> energy_conversion("joule", "wattsecond", 1) | ||
1.0 | ||
>>> energy_conversion("joule", "watthour", 1) | ||
0.0002777777777777778 | ||
>>> energy_conversion("joule", "kilowatthour", 1) | ||
2.7777777777777776e-07 | ||
>>> energy_conversion("joule", "newtonmeter", 1) | ||
1.0 | ||
>>> energy_conversion("joule", "calorie_nutr", 1) | ||
0.00023884589662749592 | ||
>>> energy_conversion("joule", "kilocalorie_nutr", 1) | ||
2.388458966274959e-07 | ||
>>> energy_conversion("joule", "electronvolt", 1) | ||
6.241509074460763e+18 | ||
>>> energy_conversion("joule", "britishthermalunit_it", 1) | ||
0.0009478171226670134 | ||
>>> energy_conversion("joule", "footpound", 1) | ||
0.7375621211696556 | ||
>>> energy_conversion("joule", "megajoule", 1000) | ||
0.001 | ||
>>> energy_conversion("calorie_nutr", "kilocalorie_nutr", 1000) | ||
1.0 | ||
>>> energy_conversion("kilowatthour", "joule", 10) | ||
36000000.0 | ||
>>> energy_conversion("britishthermalunit_it", "footpound", 1) | ||
778.1692306784539 | ||
>>> energy_conversion("watthour", "joule", "a") # doctest: +ELLIPSIS | ||
Traceback (most recent call last): | ||
... | ||
TypeError: unsupported operand type(s) for /: 'str' and 'float' | ||
>>> energy_conversion("wrongunit", "joule", 1) # doctest: +ELLIPSIS | ||
Traceback (most recent call last): | ||
... | ||
ValueError: Incorrect 'from_type' or 'to_type' value: 'wrongunit', 'joule' | ||
Valid values are: joule, ... footpound | ||
>>> energy_conversion("joule", "wrongunit", 1) # doctest: +ELLIPSIS | ||
Traceback (most recent call last): | ||
... | ||
ValueError: Incorrect 'from_type' or 'to_type' value: 'joule', 'wrongunit' | ||
Valid values are: joule, ... footpound | ||
>>> energy_conversion("123", "abc", 1) # doctest: +ELLIPSIS | ||
Traceback (most recent call last): | ||
... | ||
ValueError: Incorrect 'from_type' or 'to_type' value: '123', 'abc' | ||
Valid values are: joule, ... footpound | ||
""" | ||
if to_type not in ENERGY_CONVERSION or from_type not in ENERGY_CONVERSION: | ||
msg = ( | ||
f"Incorrect 'from_type' or 'to_type' value: {from_type!r}, {to_type!r}\n" | ||
f"Valid values are: {', '.join(ENERGY_CONVERSION)}" | ||
) | ||
raise ValueError(msg) | ||
return value * ENERGY_CONVERSION[from_type] * 1 / ENERGY_CONVERSION[to_type] | ||
|
||
|
||
if __name__ == "__main__": | ||
import doctest | ||
|
||
doctest.testmod() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.