mirror of
https://github.com/TheAlgorithms/Python.git
synced 2026-03-13 09:50:19 +08:00
refactor: Indent ... for visual purposes (#7744)
This commit is contained in:
committed by
GitHub
parent
e8915097c4
commit
9bba42eca8
@@ -12,15 +12,15 @@ def bin_to_decimal(bin_string: str) -> int:
|
||||
0
|
||||
>>> bin_to_decimal("a")
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
...
|
||||
ValueError: Non-binary value was passed to the function
|
||||
>>> bin_to_decimal("")
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
...
|
||||
ValueError: Empty string was passed to the function
|
||||
>>> bin_to_decimal("39")
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
...
|
||||
ValueError: Non-binary value was passed to the function
|
||||
"""
|
||||
bin_string = str(bin_string).strip()
|
||||
|
||||
@@ -30,11 +30,11 @@ def bin_to_hexadecimal(binary_str: str) -> str:
|
||||
'-0x1d'
|
||||
>>> bin_to_hexadecimal('a')
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
...
|
||||
ValueError: Non-binary value was passed to the function
|
||||
>>> bin_to_hexadecimal('')
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
...
|
||||
ValueError: Empty string was passed to the function
|
||||
"""
|
||||
# Sanitising parameter
|
||||
|
||||
@@ -9,11 +9,11 @@ The function below will convert any binary string to the octal equivalent.
|
||||
|
||||
>>> bin_to_octal("")
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
...
|
||||
ValueError: Empty string was passed to the function
|
||||
>>> bin_to_octal("a-1")
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
...
|
||||
ValueError: Non-binary value was passed to the function
|
||||
"""
|
||||
|
||||
|
||||
@@ -29,32 +29,32 @@ def decimal_to_any(num: int, base: int) -> str:
|
||||
>>> # negatives will error
|
||||
>>> decimal_to_any(-45, 8) # doctest: +ELLIPSIS
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
...
|
||||
ValueError: parameter must be positive int
|
||||
>>> # floats will error
|
||||
>>> decimal_to_any(34.4, 6) # doctest: +ELLIPSIS
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
...
|
||||
TypeError: int() can't convert non-string with explicit base
|
||||
>>> # a float base will error
|
||||
>>> decimal_to_any(5, 2.5) # doctest: +ELLIPSIS
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
...
|
||||
TypeError: 'float' object cannot be interpreted as an integer
|
||||
>>> # a str base will error
|
||||
>>> decimal_to_any(10, '16') # doctest: +ELLIPSIS
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
...
|
||||
TypeError: 'str' object cannot be interpreted as an integer
|
||||
>>> # a base less than 2 will error
|
||||
>>> decimal_to_any(7, 0) # doctest: +ELLIPSIS
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
...
|
||||
ValueError: base must be >= 2
|
||||
>>> # a base greater than 36 will error
|
||||
>>> decimal_to_any(34, 37) # doctest: +ELLIPSIS
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
...
|
||||
ValueError: base must be <= 36
|
||||
"""
|
||||
if isinstance(num, float):
|
||||
|
||||
@@ -19,12 +19,12 @@ def decimal_to_binary(num: int) -> str:
|
||||
>>> # other floats will error
|
||||
>>> decimal_to_binary(16.16) # doctest: +ELLIPSIS
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
...
|
||||
TypeError: 'float' object cannot be interpreted as an integer
|
||||
>>> # strings will error as well
|
||||
>>> decimal_to_binary('0xfffff') # doctest: +ELLIPSIS
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
...
|
||||
TypeError: 'str' object cannot be interpreted as an integer
|
||||
"""
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@ def binary_recursive(decimal: int) -> str:
|
||||
'1001000'
|
||||
>>> binary_recursive("number")
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
...
|
||||
ValueError: invalid literal for int() with base 10: 'number'
|
||||
"""
|
||||
decimal = int(decimal)
|
||||
@@ -30,11 +30,11 @@ def main(number: str) -> str:
|
||||
'-0b101000'
|
||||
>>> main(40.8)
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
...
|
||||
ValueError: Input value is not an integer
|
||||
>>> main("forty")
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
...
|
||||
ValueError: Input value is not an integer
|
||||
"""
|
||||
number = str(number).strip()
|
||||
|
||||
@@ -46,12 +46,12 @@ def decimal_to_hexadecimal(decimal: float) -> str:
|
||||
>>> # other floats will error
|
||||
>>> decimal_to_hexadecimal(16.16) # doctest: +ELLIPSIS
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
...
|
||||
AssertionError
|
||||
>>> # strings will error as well
|
||||
>>> decimal_to_hexadecimal('0xfffff') # doctest: +ELLIPSIS
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
...
|
||||
AssertionError
|
||||
>>> # results are the same when compared to Python's default hex function
|
||||
>>> decimal_to_hexadecimal(-256) == hex(-256)
|
||||
|
||||
@@ -21,11 +21,11 @@ def hex_to_bin(hex_num: str) -> int:
|
||||
-1111111111111111
|
||||
>>> hex_to_bin("F-f")
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
...
|
||||
ValueError: Invalid value was passed to the function
|
||||
>>> hex_to_bin("")
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
...
|
||||
ValueError: No value was passed to the function
|
||||
"""
|
||||
|
||||
|
||||
@@ -18,15 +18,15 @@ def hex_to_decimal(hex_string: str) -> int:
|
||||
-255
|
||||
>>> hex_to_decimal("F-f")
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
...
|
||||
ValueError: Non-hexadecimal value was passed to the function
|
||||
>>> hex_to_decimal("")
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
...
|
||||
ValueError: Empty string was passed to the function
|
||||
>>> hex_to_decimal("12m")
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
...
|
||||
ValueError: Non-hexadecimal value was passed to the function
|
||||
"""
|
||||
hex_string = hex_string.strip().lower()
|
||||
|
||||
@@ -4,27 +4,27 @@ def oct_to_decimal(oct_string: str) -> int:
|
||||
|
||||
>>> oct_to_decimal("")
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
...
|
||||
ValueError: Empty string was passed to the function
|
||||
>>> oct_to_decimal("-")
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
...
|
||||
ValueError: Non-octal value was passed to the function
|
||||
>>> oct_to_decimal("e")
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
...
|
||||
ValueError: Non-octal value was passed to the function
|
||||
>>> oct_to_decimal("8")
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
...
|
||||
ValueError: Non-octal value was passed to the function
|
||||
>>> oct_to_decimal("-e")
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
...
|
||||
ValueError: Non-octal value was passed to the function
|
||||
>>> oct_to_decimal("-8")
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
...
|
||||
ValueError: Non-octal value was passed to the function
|
||||
>>> oct_to_decimal("1")
|
||||
1
|
||||
@@ -38,7 +38,7 @@ def oct_to_decimal(oct_string: str) -> int:
|
||||
-37
|
||||
>>> oct_to_decimal("-")
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
...
|
||||
ValueError: Non-octal value was passed to the function
|
||||
>>> oct_to_decimal("0")
|
||||
0
|
||||
@@ -46,15 +46,15 @@ def oct_to_decimal(oct_string: str) -> int:
|
||||
-2093
|
||||
>>> oct_to_decimal("2-0Fm")
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
...
|
||||
ValueError: Non-octal value was passed to the function
|
||||
>>> oct_to_decimal("")
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
...
|
||||
ValueError: Empty string was passed to the function
|
||||
>>> oct_to_decimal("19")
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
...
|
||||
ValueError: Non-octal value was passed to the function
|
||||
"""
|
||||
oct_string = str(oct_string).strip()
|
||||
|
||||
@@ -23,7 +23,7 @@ def celsius_to_fahrenheit(celsius: float, ndigits: int = 2) -> float:
|
||||
104.0
|
||||
>>> celsius_to_fahrenheit("celsius")
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
...
|
||||
ValueError: could not convert string to float: 'celsius'
|
||||
"""
|
||||
return round((float(celsius) * 9 / 5) + 32, ndigits)
|
||||
@@ -47,7 +47,7 @@ def celsius_to_kelvin(celsius: float, ndigits: int = 2) -> float:
|
||||
313.15
|
||||
>>> celsius_to_kelvin("celsius")
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
...
|
||||
ValueError: could not convert string to float: 'celsius'
|
||||
"""
|
||||
return round(float(celsius) + 273.15, ndigits)
|
||||
@@ -71,7 +71,7 @@ def celsius_to_rankine(celsius: float, ndigits: int = 2) -> float:
|
||||
563.67
|
||||
>>> celsius_to_rankine("celsius")
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
...
|
||||
ValueError: could not convert string to float: 'celsius'
|
||||
"""
|
||||
return round((float(celsius) * 9 / 5) + 491.67, ndigits)
|
||||
@@ -101,7 +101,7 @@ def fahrenheit_to_celsius(fahrenheit: float, ndigits: int = 2) -> float:
|
||||
37.78
|
||||
>>> fahrenheit_to_celsius("fahrenheit")
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
...
|
||||
ValueError: could not convert string to float: 'fahrenheit'
|
||||
"""
|
||||
return round((float(fahrenheit) - 32) * 5 / 9, ndigits)
|
||||
@@ -131,7 +131,7 @@ def fahrenheit_to_kelvin(fahrenheit: float, ndigits: int = 2) -> float:
|
||||
310.93
|
||||
>>> fahrenheit_to_kelvin("fahrenheit")
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
...
|
||||
ValueError: could not convert string to float: 'fahrenheit'
|
||||
"""
|
||||
return round(((float(fahrenheit) - 32) * 5 / 9) + 273.15, ndigits)
|
||||
@@ -161,7 +161,7 @@ def fahrenheit_to_rankine(fahrenheit: float, ndigits: int = 2) -> float:
|
||||
559.67
|
||||
>>> fahrenheit_to_rankine("fahrenheit")
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
...
|
||||
ValueError: could not convert string to float: 'fahrenheit'
|
||||
"""
|
||||
return round(float(fahrenheit) + 459.67, ndigits)
|
||||
@@ -185,7 +185,7 @@ def kelvin_to_celsius(kelvin: float, ndigits: int = 2) -> float:
|
||||
42.35
|
||||
>>> kelvin_to_celsius("kelvin")
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
...
|
||||
ValueError: could not convert string to float: 'kelvin'
|
||||
"""
|
||||
return round(float(kelvin) - 273.15, ndigits)
|
||||
@@ -209,7 +209,7 @@ def kelvin_to_fahrenheit(kelvin: float, ndigits: int = 2) -> float:
|
||||
108.23
|
||||
>>> kelvin_to_fahrenheit("kelvin")
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
...
|
||||
ValueError: could not convert string to float: 'kelvin'
|
||||
"""
|
||||
return round(((float(kelvin) - 273.15) * 9 / 5) + 32, ndigits)
|
||||
@@ -233,7 +233,7 @@ def kelvin_to_rankine(kelvin: float, ndigits: int = 2) -> float:
|
||||
72.0
|
||||
>>> kelvin_to_rankine("kelvin")
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
...
|
||||
ValueError: could not convert string to float: 'kelvin'
|
||||
"""
|
||||
return round((float(kelvin) * 9 / 5), ndigits)
|
||||
@@ -257,7 +257,7 @@ def rankine_to_celsius(rankine: float, ndigits: int = 2) -> float:
|
||||
-97.87
|
||||
>>> rankine_to_celsius("rankine")
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
...
|
||||
ValueError: could not convert string to float: 'rankine'
|
||||
"""
|
||||
return round((float(rankine) - 491.67) * 5 / 9, ndigits)
|
||||
@@ -277,7 +277,7 @@ def rankine_to_fahrenheit(rankine: float, ndigits: int = 2) -> float:
|
||||
-144.17
|
||||
>>> rankine_to_fahrenheit("rankine")
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
...
|
||||
ValueError: could not convert string to float: 'rankine'
|
||||
"""
|
||||
return round(float(rankine) - 459.67, ndigits)
|
||||
@@ -297,7 +297,7 @@ def rankine_to_kelvin(rankine: float, ndigits: int = 2) -> float:
|
||||
22.22
|
||||
>>> rankine_to_kelvin("rankine")
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
...
|
||||
ValueError: could not convert string to float: 'rankine'
|
||||
"""
|
||||
return round((float(rankine) * 5 / 9), ndigits)
|
||||
@@ -316,7 +316,7 @@ def reaumur_to_kelvin(reaumur: float, ndigits: int = 2) -> float:
|
||||
323.15
|
||||
>>> reaumur_to_kelvin("reaumur")
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
...
|
||||
ValueError: could not convert string to float: 'reaumur'
|
||||
"""
|
||||
return round((float(reaumur) * 1.25 + 273.15), ndigits)
|
||||
@@ -335,7 +335,7 @@ def reaumur_to_fahrenheit(reaumur: float, ndigits: int = 2) -> float:
|
||||
122.0
|
||||
>>> reaumur_to_fahrenheit("reaumur")
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
...
|
||||
ValueError: could not convert string to float: 'reaumur'
|
||||
"""
|
||||
return round((float(reaumur) * 2.25 + 32), ndigits)
|
||||
@@ -354,7 +354,7 @@ def reaumur_to_celsius(reaumur: float, ndigits: int = 2) -> float:
|
||||
50.0
|
||||
>>> reaumur_to_celsius("reaumur")
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
...
|
||||
ValueError: could not convert string to float: 'reaumur'
|
||||
"""
|
||||
return round((float(reaumur) * 1.25), ndigits)
|
||||
@@ -373,7 +373,7 @@ def reaumur_to_rankine(reaumur: float, ndigits: int = 2) -> float:
|
||||
581.67
|
||||
>>> reaumur_to_rankine("reaumur")
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
...
|
||||
ValueError: could not convert string to float: 'reaumur'
|
||||
"""
|
||||
return round((float(reaumur) * 2.25 + 32 + 459.67), ndigits)
|
||||
|
||||
Reference in New Issue
Block a user