-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
Create speed_conversions.py #7128
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 17 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
fcc6460
Update README.md
Abinashbunty fe80790
Create barcode_validator.py
Abinashbunty 775e9f7
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] bf66579
Update barcode_validator.py
Abinashbunty 9bdb492
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 57247b6
Update barcode_validator.py
Abinashbunty a4019a5
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 947fa4a
Update barcode_validator.py
Abinashbunty e11313a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 70084a0
Update barcode_validator.py
Abinashbunty 6d347ab
Merge branch 'master' of https://github.com/Abinashbunty/Python
Abinashbunty 842b173
Merge branch 'TheAlgorithms:master' into master
Abinashbunty b973148
Update barcode_validator.py
cclauss f0ea90b
Merge branch 'TheAlgorithms:master' into master
Abinashbunty 633361a
Update volume_conversions.py
Abinashbunty 820be69
Create speed_conversions.py
Abinashbunty e70f471
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] f1c0837
Update speed_conversions.py
Abinashbunty ed8c7f3
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 5b1ac9c
Merge branch 'TheAlgorithms:master' into speed
Abinashbunty 5f84d0b
Update speed_conversions.py
Abinashbunty c5153ff
Update volume_conversions.py
Abinashbunty 1e3def7
Merge pull request #1 from Abinashbunty/Abinashbunty-patch-1
Abinashbunty 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,172 @@ | ||
""" | ||
Convert speed units | ||
|
||
https://en.wikipedia.org/wiki/Kilometres_per_hour | ||
https://en.wikipedia.org/wiki/Miles_per_hour | ||
https://en.wikipedia.org/wiki/Knot_(unit) | ||
https://en.wikipedia.org/wiki/Metre_per_second | ||
|
||
""" | ||
|
||
|
||
def convert_speed(speed: float, unit_from: str, unit_to: str) -> float: | ||
""" | ||
Return speed units based on input | ||
|
||
>>> convert_speed(10, 'm/s', 'km/h') | ||
36.0 | ||
|
||
>>> convert_speed(10, 'km/h', 'm/s') | ||
2.78 | ||
|
||
>>> convert_speed(10, 'm/s', 'mph') | ||
22.37 | ||
|
||
>>> convert_speed(10, 'mph', 'm/s') | ||
4.47 | ||
|
||
>>> convert_speed(10, 'km/h', 'mph') | ||
6.21 | ||
|
||
>>> convert_speed(10, 'mph', 'km/h') | ||
16.09 | ||
|
||
>>> convert_speed(10, 'm/s', 'knots') | ||
19.44 | ||
|
||
>>> convert_speed(10, 'knots', 'm/s') | ||
5.14 | ||
|
||
>>> convert_speed(10, 'km/h', 'knots') | ||
5.4 | ||
|
||
>>> convert_speed(10, 'knots', 'km/h') | ||
18.52 | ||
|
||
>>> convert_speed(10, 'mph', 'knots') | ||
8.69 | ||
|
||
>>> convert_speed(10, 'knots', 'mph') | ||
11.51 | ||
|
||
>>> convert_speed(abc, 'm/s', 'km/h') | ||
Traceback (most recent call last): | ||
... | ||
NameError: name 'abc' is not defined | ||
|
||
Abinashbunty marked this conversation as resolved.
Show resolved
Hide resolved
|
||
>>> convert_speed(10, 'abc', 'km/h') | ||
Traceback (most recent call last): | ||
... | ||
ValueError: invalid literal for int() with base 10: abc | ||
""" | ||
if unit_from == "m/s": | ||
if unit_to == "km/h": | ||
return round(speed * 3.6, 2) | ||
elif unit_to == "mph": | ||
return round(speed * 2.23694, 2) | ||
elif unit_to == "knots": | ||
return round(speed * 1.94384, 2) | ||
else: | ||
raise ValueError("invalid literal for int() with base 10: " + unit_to) | ||
|
||
elif unit_from == "km/h": | ||
if unit_to == "m/s": | ||
return round(speed / 3.6, 2) | ||
elif unit_to == "mph": | ||
return round(speed * 0.621371, 2) | ||
elif unit_to == "knots": | ||
return round(speed * 0.539957, 2) | ||
else: | ||
raise ValueError("invalid literal for int() with base 10: " + unit_to) | ||
|
||
elif unit_from == "mph": | ||
if unit_to == "m/s": | ||
return round(speed / 2.23694, 2) | ||
elif unit_to == "km/h": | ||
return round(speed / 0.621371, 2) | ||
elif unit_to == "knots": | ||
return round(speed * 0.868976, 2) | ||
else: | ||
raise ValueError("invalid literal for int() with base 10: " + unit_to) | ||
|
||
elif unit_from == "knots": | ||
if unit_to == "m/s": | ||
return round(speed / 1.94384, 2) | ||
elif unit_to == "km/h": | ||
return round(speed / 0.539957, 2) | ||
elif unit_to == "mph": | ||
return round(speed / 0.868976, 2) | ||
else: | ||
raise ValueError("invalid literal for int() with base 10: " + unit_to) | ||
else: | ||
raise ValueError("invalid literal for int() with base 10: " + unit_from) | ||
|
||
|
||
if __name__ == "__main__": | ||
""" | ||
Run doctests | ||
|
||
>>> round(convert_speed(10, 'm/s', 'km/h'), 2) | ||
36.0 | ||
|
||
>>> round(convert_speed(10, 'km/h', 'm/s'), 2) | ||
2.78 | ||
|
||
>>> round(convert_speed(10, 'm/s', 'mph'), 2) | ||
22.37 | ||
|
||
>>> round(convert_speed(10, 'mph', 'm/s'), 2) | ||
4.47 | ||
|
||
>>> round(convert_speed(10, 'km/h', 'mph'), 2) | ||
6.21 | ||
|
||
>>> round(convert_speed(10, 'mph', 'km/h'), 2) | ||
16.09 | ||
|
||
>>> round(convert_speed(10, 'm/s', 'knots'), 2) | ||
19.44 | ||
|
||
>>> round(convert_speed(10, 'knots', 'm/s'), 2) | ||
5.14 | ||
|
||
>>> round(convert_speed(10, 'km/h', 'knots'), 2) | ||
5.4 | ||
|
||
>>> round(convert_speed(10, 'knots', 'km/h'), 2) | ||
18.52 | ||
|
||
>>> round(convert_speed(10, 'mph', 'knots'), 2) | ||
8.69 | ||
|
||
>>> round(convert_speed(10, 'knots', 'mph'), 2) | ||
11.55 | ||
|
||
>>> round(convert_speed(10, 'abc', 'km/h'), 2) | ||
Traceback (most recent call last): | ||
... | ||
ValueError: invalid literal for int() with base 10: 'abc' | ||
|
||
>>> round(convert_speed(abc, 'm/s', 'km/h'), 2) | ||
Traceback (most recent call last): | ||
... | ||
ValueError: could not convert string to float: 'abc' | ||
|
||
""" | ||
import doctest | ||
|
||
doctest.testmod() | ||
|
||
print("----Codes for speed units----") | ||
print("m/s: meters per second") | ||
print("km/h: kilometers per hour") | ||
print("mph: miles per hour") | ||
print("knots: nautical miles per hour") | ||
|
||
from_unit = input("From unit code: ") | ||
to_unit = input("To unit code: ") | ||
speed = input("Speed: ") | ||
|
||
speed_converted = round(convert_speed(float(speed), from_unit, to_unit), 2) | ||
|
||
print(f"{speed} {from_unit} is {speed_converted} {to_unit}") |
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
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.