-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
Added length unit conversions #5373
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 4 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
7a4062a
Added length unit conversions
SabariGanesh-K 9c91d51
Formatted File
SabariGanesh-K 9686aeb
Reformatted file
SabariGanesh-K 7e7bc51
Reformatted code once again
SabariGanesh-K 8ea463c
Merge branch 'TheAlgorithms:master' into master
SabariGanesh-K b46266f
Added more test
SabariGanesh-K 0968b11
Update length_conversions.py
cclauss e17567d
Update length_conversions.py
cclauss a084635
Update length_conversions.py
cclauss 7955589
Update length_conversions.py
cclauss abe2ae1
Update length_conversions.py
cclauss ecb6c33
Update length_conversions.py
cclauss 14df863
Update length_conversions.py
cclauss d54012b
Fixed Minor errors in test
SabariGanesh-K 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,110 @@ | ||
""" | ||
Conversion of length units. | ||
Available Units:- Metre,Kilometre,Feet,Inch,Centimeter,Yard,Foot,Mile,Millimeter | ||
|
||
USAGE : | ||
-> Import this file into their respective project. | ||
-> Use the function length_conversion() for conversion of length 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/Meter | ||
-> Wikipedia reference: https://en.wikipedia.org/wiki/Kilometer | ||
-> Wikipedia reference: https://en.wikipedia.org/wiki/Feet | ||
-> Wikipedia reference: https://en.wikipedia.org/wiki/Inch | ||
-> Wikipedia reference: https://en.wikipedia.org/wiki/Centimeter | ||
-> Wikipedia reference: https://en.wikipedia.org/wiki/Yard | ||
-> Wikipedia reference: https://en.wikipedia.org/wiki/Foot | ||
-> Wikipedia reference: https://en.wikipedia.org/wiki/Mile | ||
-> Wikipedia reference: https://en.wikipedia.org/wiki/Millimeter | ||
""" | ||
|
||
METER_CHART: dict[str, float] = { | ||
"meter": 1, | ||
"kilometer": 0.001, | ||
"feet": 3.28084, | ||
"inch": 39.3701, | ||
"centimeter": 100, | ||
"yard": 1.09361, | ||
"foot": 3.28084, | ||
"mile": 0.000621371, | ||
"millimeter": 1000, | ||
} | ||
|
||
LENGTH_TYPE_CHART: dict[str, float] = { | ||
"meter": 1, | ||
"kilometer": 1000, | ||
"feet": 0.3048, | ||
"inch": 0.0254, | ||
"centimeter": 0.01, | ||
"yard": 0.9144, | ||
"foot": 0.3048, | ||
"mile": 1609.34, | ||
"millimeter": 0.001, | ||
} | ||
|
||
|
||
def length_conversion(from_type: str, to_type: str, value: float) -> float: | ||
""" | ||
Conversion of length unit with the help of LENGTH_CHART | ||
"meter": 1, | ||
"kilometer": 0.001, | ||
"feet": 3.28084, | ||
"inch": 39.3701, | ||
"centimeter": 100, | ||
"yard": 1.09361, | ||
"foot": 3.28084, | ||
"mile": 0.000621371, | ||
"millimeter": 1000, | ||
>>> length_conversion("meter","feet",4) | ||
13.12336 | ||
>>> length_conversion("meter","kilometer",1) | ||
0.001 | ||
>>> length_conversion("kilometer","inch",1) | ||
39370.1 | ||
>>> length_conversion("kilometer","mile",3) | ||
1.8641130000000001 | ||
>>> length_conversion("feet","meter",2) | ||
0.6096 | ||
>>> length_conversion("feet","yard",4) | ||
1.333329312 | ||
>>> length_conversion("inch","meter",1) | ||
0.0254 | ||
>>> length_conversion("inch","mile",2) | ||
3.15656468e-05 | ||
>>> length_conversion("centimeter","millimeter",2) | ||
20.0 | ||
>>> length_conversion("centimeter","yard",2) | ||
0.0218722 | ||
>>> length_conversion("yard","meter",4) | ||
3.6576 | ||
>>> length_conversion("yard","kilometer",4) | ||
0.0036576 | ||
>>> length_conversion("foot","meter",3) | ||
0.9144000000000001 | ||
>>> length_conversion("foot","inch",3) | ||
36.00001944 | ||
>>> length_conversion("mile","kilometer",4) | ||
6.43736 | ||
>>> length_conversion("mile","inch",2) | ||
126719.753468 | ||
>>> length_conversion("millimeter","centimeter",3) | ||
0.3 | ||
>>> length_conversion("millimeter","inch",3) | ||
0.1181103 | ||
""" | ||
if to_type not in METER_CHART or from_type not in LENGTH_TYPE_CHART: | ||
raise ValueError( | ||
f"Invalid 'from_type' or 'to_type' value: {from_type!r}, {to_type!r}\n" | ||
f"Supported values are: {', '.join(LENGTH_TYPE_CHART)}" | ||
) | ||
return value * METER_CHART[to_type] * LENGTH_TYPE_CHART[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.