-
- Notifications
You must be signed in to change notification settings - Fork 49.2k
Create weight_conversion.py #3964
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
Changes from 5 commits
Commits
Show all changes
14 commits Select commit Hold shift + click to select a range
3f42e25 Create weight_conversion.py
AnubhavSolanki 4cdeeb0 Update weight_conversion.py
AnubhavSolanki 4fcf4bb Update weight_conversion.py
AnubhavSolanki 281e00e Update weight_conversion.py
AnubhavSolanki 36a0918 Update weight_conversion.py
AnubhavSolanki 7473bad Update weight_conversion.py
AnubhavSolanki 1d73d84 Update weight_conversion.py
AnubhavSolanki d024c52 Update weight_conversion.py
AnubhavSolanki c8362e5 Update weight_conversion.py
AnubhavSolanki e428493 Update weight_conversion.py
AnubhavSolanki 10f7804 Update weight_conversion.py
AnubhavSolanki 915bedc Update weight_conversion.py
AnubhavSolanki 40fe4b6 Update weight_conversion.py
AnubhavSolanki b19e9be Update weight_conversion.py
dhruvmanila 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,64 @@ | ||
| """ | ||
| Functions useful for conversion fo weight units | ||
| """ | ||
| | ||
| | ||
| def weightConversion(from_type: str, to_type: str, value: float) -> any: | ||
dhruvmanila marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| """ | ||
| Conversion of weight unit with the help of kilogram_chart | ||
| | ||
| "kilogram" : 1, | ||
| "gram" : pow(10, 3), | ||
| "milligram" : pow(10, 6), | ||
| "metric-ton" : pow(10, -3), | ||
| "long-ton" : 0.0009842073, | ||
| "short-ton" : 0.0011023122, | ||
| "pound" : 2.2046244202, | ||
| "ounce" : 35.273990723, | ||
| "carrat" : 5000, | ||
| "atomic-mass-unit" : 6.022136652E+26 | ||
| | ||
| Wikipedia reference: https://en.wikipedia.org/wiki/Conversion_of_units | ||
dhruvmanila marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| | ||
| | ||
| >>> weightConversion("kilogram","kilogram",5) | ||
dhruvmanila marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| 5 | ||
| >>> weightConversion("kilogram","carrat",5) | ||
dhruvmanila marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| 25000 | ||
| >>> weightConversion("ounce","short-ton",3) | ||
dhruvmanila marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| 9.37499991417e-05 | ||
| """ | ||
| kilogram_chart = { | ||
dhruvmanila marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| "kilogram": 1, | ||
| "gram": pow(10, 3), | ||
| "milligram": pow(10, 6), | ||
| "metric-ton": pow(10, -3), | ||
| "long-ton": 0.0009842073, | ||
| "short-ton": 0.0011023122, | ||
| "pound": 2.2046244202, | ||
| "ounce": 35.273990723, | ||
| "carrat": 5000, | ||
| "atomic-mass-unit": 6.022136652e26, | ||
| } | ||
| | ||
| weight_type_chart = { | ||
dhruvmanila marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| "kilogram": 1, | ||
| "gram": pow(10, -3), | ||
| "milligram": pow(10, -6), | ||
| "metric-ton": pow(10, 3), | ||
| "long-ton": 1016.04608, | ||
| "short-ton": 907.184, | ||
| "pound": 0.453592, | ||
| "ounce": 0.0283495, | ||
| "carrat": 0.0002, | ||
| "atomic-mass-unit": 1.660540199e-27, | ||
| } | ||
| | ||
| return value * kilogram_chart.get(to_type) * weight_type_chart.get(from_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.