Skip to content

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 23 commits into from
Oct 14, 2022
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
fcc6460
Update README.md
Abinashbunty Oct 6, 2022
fe80790
Create barcode_validator.py
Abinashbunty Oct 7, 2022
775e9f7
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 7, 2022
bf66579
Update barcode_validator.py
Abinashbunty Oct 7, 2022
9bdb492
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 7, 2022
57247b6
Update barcode_validator.py
Abinashbunty Oct 7, 2022
a4019a5
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 7, 2022
947fa4a
Update barcode_validator.py
Abinashbunty Oct 12, 2022
e11313a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 12, 2022
70084a0
Update barcode_validator.py
Abinashbunty Oct 12, 2022
6d347ab
Merge branch 'master' of https://github.com/Abinashbunty/Python
Abinashbunty Oct 12, 2022
842b173
Merge branch 'TheAlgorithms:master' into master
Abinashbunty Oct 12, 2022
b973148
Update barcode_validator.py
cclauss Oct 12, 2022
f0ea90b
Merge branch 'TheAlgorithms:master' into master
Abinashbunty Oct 13, 2022
633361a
Update volume_conversions.py
Abinashbunty Oct 13, 2022
820be69
Create speed_conversions.py
Abinashbunty Oct 13, 2022
e70f471
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 13, 2022
f1c0837
Update speed_conversions.py
Abinashbunty Oct 13, 2022
ed8c7f3
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 13, 2022
5b1ac9c
Merge branch 'TheAlgorithms:master' into speed
Abinashbunty Oct 13, 2022
5f84d0b
Update speed_conversions.py
Abinashbunty Oct 14, 2022
c5153ff
Update volume_conversions.py
Abinashbunty Oct 14, 2022
1e3def7
Merge pull request #1 from Abinashbunty/Abinashbunty-patch-1
Abinashbunty Oct 14, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
172 changes: 172 additions & 0 deletions conversions/speed_conversions.py
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

>>> 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}")
6 changes: 1 addition & 5 deletions conversions/volume_conversions.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,7 @@ def volume_conversion(value: float, from_type: str, to_type: str) -> float:
0.000236588
>>> volume_conversion(4, "wrongUnit", "litre")
Traceback (most recent call last):
File "/usr/lib/python3.8/doctest.py", line 1336, in __run
exec(compile(example.source, filename, "single",
File "<doctest __main__.volume_conversion[7]>", line 1, in <module>
volume_conversion(4, "wrongUnit", "litre")
File "<string>", line 62, in volume_conversion
...
ValueError: Invalid 'from_type' value: 'wrongUnit' Supported values are:
cubicmeter, litre, kilolitre, gallon, cubicyard, cubicfoot, cup
"""
Expand Down