From 821615ba01c868498221ec2ac10a75ac0adea1e6 Mon Sep 17 00:00:00 2001 From: Akshay B Shetty Date: Thu, 5 Oct 2023 21:31:33 +0000 Subject: [PATCH 1/7] feat: :sparkles: calculating the resitance of resistor using color codes --- electronics/resistor_color_code.py | 373 +++++++++++++++++++++++++++++ 1 file changed, 373 insertions(+) create mode 100644 electronics/resistor_color_code.py diff --git a/electronics/resistor_color_code.py b/electronics/resistor_color_code.py new file mode 100644 index 000000000000..b0534b813def --- /dev/null +++ b/electronics/resistor_color_code.py @@ -0,0 +1,373 @@ +""" +Title : Calculating the resistance of a n band resistor using the color codes + +Description : + Resistors resist the flow of electrical current.Each one has a value that tells how + strongly it resists current flow.This value's unit is the ohm, often noted with the + Greek letter omega: Ω. + + The colored bands on a resistor can tell you everything you need to know about its + value and tolerance, as long as you understand how to read them. The order in which + the colors are arranged is very important, and each value of resistor has its own + unique combination. + + The color coding for resistors is an international standard that is defined in IEC + 60062. + + The number of bands present in a resistor varies from three to six. These represent + significant figures, multiplier, tolerance, reliability, and temperature coefficient + Each color used for a type of band has a value assigned to it. It is read from left + to right. + All resistors will have significant figures and multiplier bands. In a three band + resistor first two bands from the left represent significant figures and the third + represents the multiplier band. + + Significant figures - The number of significant figures band in a resistor can vary + from two to three. + Colors and values associated with significant figure bands - + (Black = 0, Brown = 1, Red = 2, Orange = 3, Yellow = 4, Green = 5, Blue = 6, + Violet = 7, Grey = 8, White = 9) + + Multiplier - There will be one multiplier band in a resistor. It is multiplied with + the significant figures obtained from previous bands. + Colors and values associated with multiplier band - + (Black = 100, Brown = 10^1, Red = 10^2, Orange = 10^3, Yellow = 10^4, Green = 10^5, + Blue = 10^6, Violet = 10^7, Grey = 10^8, White = 10^9, Gold = 10^-1, Silver = 10^-2) + Note that multiplier bands use Gold and Silver which are not used for significant + figure bands. + + Tolerance - The tolerance band is not always present. It can be seen in four band + resistors and above. This is a percentage by which the resistor value can vary. + Colors and values associated with tolerance band - + (Brown = 1%, Red = 2%, Orange = 0.05%, Yellow = 0.02%, Green = 0.5%,Blue = 0.25%, + Violet = 0.1%, Grey = 0.01%, Gold = 5%, Silver = 10%) + If no color is mentioned then by default tolerance is 20% + Note that tolerance band does not use Black and White colors. + + Temperature Coeffecient - Indicates the change in resistance of the component as + a function of ambient temperature in terms of ppm/K. + It is present in six band resistors. + Colors and values associated with Temperature coeffecient - + (Black = 250 ppm/K, Brown = 100 ppm/K, Red = 50 ppm/K, Orange = 15 ppm/K, + Yellow = 25 ppm/K, Green = 20 ppm/K, Blue = 10 ppm/K, Violet = 5 ppm/K, + Grey = 1 ppm/K) + Note that temperature coeffecient band does not use White, Gold, Silver colors. + +Sources : + https://www.calculator.net/resistor-calculator.html + https://learn.parallax.com/support/reference/resistor-color-codes + https://byjus.com/physics/resistor-colour-codes/ +""" +valid_colors: list = [ + "Black", + "Brown", + "Red", + "Orange", + "Yellow", + "Green", + "Blue", + "Violet", + "Grey", + "White", + "Gold", + "Silver", +] + +significant_figures_color_values: dict[str, int] = { + "Black": 0, + "Brown": 1, + "Red": 2, + "Orange": 3, + "Yellow": 4, + "Green": 5, + "Blue": 6, + "Violet": 7, + "Grey": 8, + "White": 9, +} + +multiplier_color_values: dict[str, float] = { + "Black": 10**0, + "Brown": 10**1, + "Red": 10**2, + "Orange": 10**3, + "Yellow": 10**4, + "Green": 10**5, + "Blue": 10**6, + "Violet": 10**7, + "Grey": 10**8, + "White": 10**9, + "Gold": 10**-1, + "Silver": 10**-2, +} + +tolerance_color_values: dict[str, float] = { + "Brown": 1, + "Red": 2, + "Orange": 0.05, + "Yellow": 0.02, + "Green": 0.5, + "Blue": 0.25, + "Violet": 0.1, + "Grey": 0.01, + "Gold": 5, + "Silver": 10, +} + +temperature_coeffecient_color_values: dict[str, int] = { + "Black": 250, + "Brown": 100, + "Red": 50, + "Orange": 15, + "Yellow": 25, + "Green": 20, + "Blue": 10, + "Violet": 5, + "Grey": 1, +} + +band_types: dict[int, dict[str, int]] = { + 3: {"significant": 2, "multiplier": 1}, + 4: {"significant": 2, "multiplier": 1, "tolerance": 1}, + 5: {"significant": 3, "multiplier": 1, "tolerance": 1}, + 6: {"significant": 3, "multiplier": 1, "tolerance": 1, "temp_coeffecient": 1}, +} + + +def get_significant_digits(colors: list) -> str: + """ + Function returns the digit associated with the color. Function takes a + list containing colors as input and returns digits as string + + >>> get_significant_digits(['Black','Blue']) + '06' + + >>> get_significant_digits(['Aqua','Blue']) + Traceback (most recent call last): + ... + ValueError: Aqua is not a valid color for significant figure bands + + """ + digit = "" + for color in colors: + if color not in significant_figures_color_values: + msg = f"{color} is not a valid color for significant figure bands" + raise ValueError(msg) + digit = digit + str(significant_figures_color_values[color]) + return str(digit) + + +def get_multiplier(color: str) -> float: + """ + Function returns the multiplier value associated with the color. + Function takes color as input and returns multiplier value + + >>> get_multiplier('Gold') + 0.1 + + >>> get_multiplier('Ivory') + Traceback (most recent call last): + ... + ValueError: Ivory is not a valid color for multiplier band + + """ + if color not in multiplier_color_values: + msg = f"{color} is not a valid color for multiplier band" + raise ValueError(msg) + return multiplier_color_values[color] + + +def get_tolerance(color: str) -> float: + """ + Function returns the tolerance value associated with the color. + Function takes color as input and returns tolerance value. + + >>> get_tolerance('Green') + 0.5 + + >>> get_tolerance('Indigo') + Traceback (most recent call last): + ... + ValueError: Indigo is not a valid color for tolerance band + + """ + if color not in tolerance_color_values: + msg = f"{color} is not a valid color for tolerance band" + raise ValueError(msg) + return tolerance_color_values[color] + + +def get_temperature_coeffecient(color: str) -> int: + """ + Function returns the temperature coeffecient value associated with the color. + Function takes color as input and returns temperature coeffecient value. + + >>> get_temperature_coeffecient('Yellow') + 25 + + >>> get_temperature_coeffecient('Cyan') + Traceback (most recent call last): + ... + ValueError: Cyan is not a valid color for temperature coeffecient band + + """ + if color not in temperature_coeffecient_color_values: + msg = f"{color} is not a valid color for temperature coeffecient band" + raise ValueError(msg) + return temperature_coeffecient_color_values[color] + + +def get_band_type_count(total_number_of_bands: int, type_of_band: str) -> int: + """ + Function returns the number of bands of a given type in a resistor with n bands + Function takes total_number_of_bands and type_of_band as input and returns + number of bands belonging to that type in the given resistor + + >>> get_band_type_count(3,'significant') + 2 + + >>> get_band_type_count(2,'significant') + Traceback (most recent call last): + ... + ValueError: 2 is not a valid number of bands + + >>> get_band_type_count(3,'sign') + Traceback (most recent call last): + ... + ValueError: sign is not valid for a 3 band resistor + + >>> get_band_type_count(3,'tolerance') + Traceback (most recent call last): + ... + ValueError: tolerance is not valid for a 3 band resistor + + >>> get_band_type_count(5,'temp_coeffecient') + Traceback (most recent call last): + ... + ValueError: temp_coeffecient is not valid for a 5 band resistor + + """ + if total_number_of_bands not in band_types: + msg = f"{total_number_of_bands} is not a valid number of bands" + raise ValueError(msg) + if type_of_band not in band_types[total_number_of_bands]: + msg = f"{type_of_band} is not valid for a {total_number_of_bands} band resistor" + raise ValueError(msg) + return band_types[total_number_of_bands][type_of_band] + + +def check_validity(number_of_bands: int, colors: list) -> bool: + """ + Function checks if the input provided is valid or not. + Function takes number_of_bands and colors as input and returns + True if it is valid + + >>> check_validity(3, ["Black","Blue","Orange"]) + True + + >>> check_validity(4, ["Black","Blue","Orange"]) + Traceback (most recent call last): + ... + ValueError: Expecting 4 colors, provided 3 colors + + >>> check_validity(3, ["Cyan","Red","Yellow"]) + Traceback (most recent call last): + ... + ValueError: Cyan is not a valid color + + """ + if number_of_bands >= 3 and number_of_bands <= 6: + if number_of_bands == len(colors): + for color in colors: + if color not in valid_colors: + msg = f"{color} is not a valid color" + raise ValueError(msg) + return True + else: + msg = f"Expecting {number_of_bands} colors, provided {len(colors)} colors" + raise ValueError(msg) + else: + msg = "Invalid number of bands. Resistor bands must be 3 to 6" + raise ValueError(msg) + + +def calculate_resistance(number_of_bands: int, color_code_list: list) -> dict: + """ + Function calculates the total resistance of the resistor using the color codes. + Function takes number_of_bands, color_code_list as input and returns + resistance + + >>> calculate_resistance(3, ["Black","Blue","Orange"]) + {'resistance': '6000Ω ±20% '} + + >>> calculate_resistance(4, ["Orange","Green","Blue","Gold"]) + {'resistance': '35000000Ω ±5% '} + + >>> calculate_resistance(5, ["Violet","Brown","Grey","Silver","Green"]) + {'resistance': '7.18Ω ±0.5% '} + + >>> calculate_resistance(6, ["Red","Green","Blue","Yellow","Orange","Grey"]) + {'resistance': '2560000Ω ±0.05% 1 ppm/K'} + + >>> calculate_resistance(0, ["Violet","Brown","Grey","Silver","Green"]) + Traceback (most recent call last): + ... + ValueError: Invalid number of bands. Resistor bands must be 3 to 6 + + >>> calculate_resistance(4, ["Violet","Brown","Grey","Silver","Green"]) + Traceback (most recent call last): + ... + ValueError: Expecting 4 colors, provided 5 colors + + >>> calculate_resistance(4, ["Violet","Silver","Brown","Grey"]) + Traceback (most recent call last): + ... + ValueError: Silver is not a valid color for significant figure bands + + >>> calculate_resistance(4, ["Violet","Blue","Lime","Grey"]) + Traceback (most recent call last): + ... + ValueError: Lime is not a valid color + + """ + is_valid = check_validity(number_of_bands, color_code_list) + if is_valid: + number_of_significant_bands = get_band_type_count( + number_of_bands, "significant" + ) + significant_colors = color_code_list[:number_of_significant_bands] + significant_digits = int(get_significant_digits(significant_colors)) + multiplier_color = color_code_list[number_of_significant_bands] + multiplier = get_multiplier(multiplier_color) + if number_of_bands == 3: + tolerance_color = None + else: + tolerance_color = color_code_list[number_of_significant_bands + 1] + tolerance = ( + 20 if tolerance_color is None else get_tolerance(str(tolerance_color)) + ) + if number_of_bands != 6: + temperature_coeffecient_color = None + else: + temperature_coeffecient_color = color_code_list[ + number_of_significant_bands + 2 + ] + temperature_coeffecient = ( + 0 + if temperature_coeffecient_color is None + else get_temperature_coeffecient(str(temperature_coeffecient_color)) + ) + resisitance = significant_digits * multiplier + if temperature_coeffecient == 0: + answer = f"{resisitance}Ω ±{tolerance}% " + else: + answer = f"{resisitance}Ω ±{tolerance}% {temperature_coeffecient} ppm/K" + return {"resistance": answer} + else: + raise ValueError("Input is invalid") + + +if __name__ == "__main__": + import doctest + + doctest.testmod() From 925b1edbdd9c15e9bffbb4d7986b8cc8d65549cd Mon Sep 17 00:00:00 2001 From: Akshay B Shetty Date: Fri, 6 Oct 2023 12:24:56 +0000 Subject: [PATCH 2/7] feat: :sparkles: implementation of boyle's law --- physics/boyles_law.py | 181 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 181 insertions(+) create mode 100644 physics/boyles_law.py diff --git a/physics/boyles_law.py b/physics/boyles_law.py new file mode 100644 index 000000000000..500a1c2e324b --- /dev/null +++ b/physics/boyles_law.py @@ -0,0 +1,181 @@ +""" +Title : Implementation of Boyle's law. + +Description : + Boyle's law, also referred to as the Boyle-Mariotte law, or Mariotte's law + (especially in France), is an experimental gas law that describes the relationship + between pressure and volume of a confined gas. + + Boyle's law is a gas law which states that the pressure exerted by a gas + (of a given mass, kept at a constant temperature) is inversely proportional to the + volume occupied by it. + + In other words, the pressure and volume of a gas are inversely proportional to each + other as long as the temperature and the quantity of gas are kept constant. + Boyle's law was put forward by the Anglo-Irish chemist Robert Boyle in the year 1662 + + For a gas, the relationship between volume and pressure (at constant mass and + temperature) can be expressed mathematically as follows. + + P ∝ (1/V) + + Where P is the pressure exerted by the gas and V is the volume occupied by it. This + proportionality can be converted into an equation by adding a constant, k. + + P = k*(1/V) ⇒ PV = k + + Boyle's law states that when the temperature of a given mass of confined gas is + constant,the product of its pressure and volume is also constant. When comparing the + same substance under two different sets of conditions, the law can be expressed as: + + P1V1 = P2V2 + + Where, + + P1 is the initial pressure exerted by the gas in Pascals (P) + V1 is the initial volume occupied by the gas Litres (L) + P2 is the final pressure exerted by the gas Pascals (P) + V2 is the final volume occupied by the gas Litres (L) + + This equation can be used to predict the increase in the pressure exerted by a gas + on the walls of its container when the volume of its container is decreased + (and its quantity and absolute temperature remain unchanged). + +Sources : + https://en.wikipedia.org/wiki/Boyle%27s_law + https://byjus.com/chemistry/boyles-law/ +""" + +valid_variables: list[str] = ["v1", "v2", "p1", "p2"] + + +def check_validity(values: dict[str, float]) -> bool: + """ + + Function takes dictionary as an input and returns True if the input + is valid + + >>> check_validity({}) + Traceback (most recent call last): + ... + ValueError: Invalid input expected 3 items got 0 + + >>> check_validity({'v1':2,'v2':4,'k':6}) + Traceback (most recent call last): + ... + ValueError: Invalid input k is not a valid variable + + >>> check_validity({'v1':2,'v2':4,'p1':6}) + True + + """ + if len(values) == 3: + for value in values: + if value not in valid_variables: + msg = f"Invalid input {value} is not a valid variable" + raise ValueError(msg) + return True + else: + msg = f"Invalid input expected {3} items got {len(values)}" + raise ValueError(msg) + + +def find_target_variable(values: dict[str, float]) -> str: + """ + + Function is used to get the valid target variable whose value needs to be found + using Boyle's Law. + Function takes a dictionary as an input and returns a string + + >>> find_target_variable({}) + Traceback (most recent call last): + ... + ValueError: Invalid input expected 3 items got 0 + + >>> find_target_variable({'v1':1,'v2':2,'p2':4}) + 'p1' + + >>> find_target_variable({'v1':1,'v2':2,'k':4}) + Traceback (most recent call last): + ... + ValueError: Invalid input k is not a valid variable + + """ + is_valid = check_validity(values) + if is_valid: + for variable in valid_variables: + if variable not in values: + return variable + raise ValueError("Input is invalid") + else: + raise ValueError("Input is invalid") + + +def boyles_law(values: dict[str, float]) -> dict[str, str]: + """ + + Function calculates the the unknown pressure or volume using Boyle's law. + Function takes a dictionary as an input. It contains values for respective + pressure and volumes and computes the required value and returns it as + output + + >>> boyles_law({'p1':2,'v2':1}) + Traceback (most recent call last): + ... + ValueError: Invalid input expected 3 items got 2 + + >>> boyles_law({}) + Traceback (most recent call last): + ... + ValueError: Invalid input expected 3 items got 0 + + >>> boyles_law({'p1':2,'v2':1, 'k':6}) + Traceback (most recent call last): + ... + ValueError: Invalid input k is not a valid variable + + >>> boyles_law({'p1':100,'v2':150, 'v1':120}) + {'p2': '80.0 Pa'} + + >>> boyles_law({'p1':10,'v1':20, 'p2':20}) + {'v2': '10.0 L'} + + >>> boyles_law({'v1':13,'p2':17, 'v2':19}) + {'p1': '24.846 Pa'} + + >>> boyles_law({'v2':27,'p1':25, 'p2':29}) + {'v1': '31.32 L'} + + """ + is_valid = check_validity(values) + if is_valid: + target = find_target_variable(values) + float_precision = ".3f" + if target == "p1": + p1 = float( + format((values["p2"] * values["v2"]) / values["v1"], float_precision) + ) + return {"p1": f"{p1} Pa"} + elif target == "v1": + v1 = float( + format((values["p2"] * values["v2"]) / values["p1"], float_precision) + ) + return {"v1": f"{v1} L"} + elif target == "p2": + p2 = float( + format((values["p1"] * values["v1"]) / values["v2"], float_precision) + ) + return {"p2": f"{p2} Pa"} + else: + v2 = float( + format((values["p1"] * values["v1"]) / values["p2"], float_precision) + ) + return {"v2": f"{v2} L"} + else: + raise ValueError("Input is invalid") + + +if __name__ == "__main__": + import doctest + + doctest.testmod() From e533a07c745be1d9b855435d648fc7dc9c7f82cc Mon Sep 17 00:00:00 2001 From: Akshay B Shetty Date: Tue, 10 Oct 2023 04:57:04 +0000 Subject: [PATCH 3/7] docs: :memo: removed redundant information in description --- physics/boyles_law.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/physics/boyles_law.py b/physics/boyles_law.py index 500a1c2e324b..de1fd7a11750 100644 --- a/physics/boyles_law.py +++ b/physics/boyles_law.py @@ -10,10 +10,6 @@ (of a given mass, kept at a constant temperature) is inversely proportional to the volume occupied by it. - In other words, the pressure and volume of a gas are inversely proportional to each - other as long as the temperature and the quantity of gas are kept constant. - Boyle's law was put forward by the Anglo-Irish chemist Robert Boyle in the year 1662 - For a gas, the relationship between volume and pressure (at constant mass and temperature) can be expressed mathematically as follows. From 20f9adf4dcd1c202973b4027fbae65090dc562ed Mon Sep 17 00:00:00 2001 From: Akshay B Shetty Date: Tue, 10 Oct 2023 04:59:36 +0000 Subject: [PATCH 4/7] docs: :memo: updated description of boyle's law as suggested --- physics/boyles_law.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/physics/boyles_law.py b/physics/boyles_law.py index de1fd7a11750..f75e8733050f 100644 --- a/physics/boyles_law.py +++ b/physics/boyles_law.py @@ -3,11 +3,8 @@ Description : Boyle's law, also referred to as the Boyle-Mariotte law, or Mariotte's law - (especially in France), is an experimental gas law that describes the relationship - between pressure and volume of a confined gas. - - Boyle's law is a gas law which states that the pressure exerted by a gas - (of a given mass, kept at a constant temperature) is inversely proportional to the + (especially in France), is a gas law which states that the pressure exerted + by a gas of a fixed mass and temperature is inversely proportional to the volume occupied by it. For a gas, the relationship between volume and pressure (at constant mass and From 7f689e4f5c976a5c474918ffd0c03c44ea2832f3 Mon Sep 17 00:00:00 2001 From: Akshay B Shetty Date: Tue, 10 Oct 2023 05:58:10 +0000 Subject: [PATCH 5/7] refactor: :recycle: refactored code as suggested in review --- physics/boyles_law.py | 81 ++++++++++++++++++++----------------------- 1 file changed, 37 insertions(+), 44 deletions(-) diff --git a/physics/boyles_law.py b/physics/boyles_law.py index f75e8733050f..f3957fd6f554 100644 --- a/physics/boyles_law.py +++ b/physics/boyles_law.py @@ -42,7 +42,7 @@ valid_variables: list[str] = ["v1", "v2", "p1", "p2"] -def check_validity(values: dict[str, float]) -> bool: +def check_validity(values: dict[str, float]): """ Function takes dictionary as an input and returns True if the input @@ -51,7 +51,7 @@ def check_validity(values: dict[str, float]) -> bool: >>> check_validity({}) Traceback (most recent call last): ... - ValueError: Invalid input expected 3 items got 0 + ValueError: Invalid input expected 3 items, got 0 >>> check_validity({'v1':2,'v2':4,'k':6}) Traceback (most recent call last): @@ -59,18 +59,17 @@ def check_validity(values: dict[str, float]) -> bool: ValueError: Invalid input k is not a valid variable >>> check_validity({'v1':2,'v2':4,'p1':6}) - True """ - if len(values) == 3: + if len(values) != 3: + msg = f"Invalid input expected {3} items, got {len(values)}" + raise ValueError(msg) + else: for value in values: if value not in valid_variables: msg = f"Invalid input {value} is not a valid variable" raise ValueError(msg) - return True - else: - msg = f"Invalid input expected {3} items got {len(values)}" - raise ValueError(msg) + return def find_target_variable(values: dict[str, float]) -> str: @@ -83,7 +82,7 @@ def find_target_variable(values: dict[str, float]) -> str: >>> find_target_variable({}) Traceback (most recent call last): ... - ValueError: Invalid input expected 3 items got 0 + ValueError: Invalid input expected 3 items, got 0 >>> find_target_variable({'v1':1,'v2':2,'p2':4}) 'p1' @@ -94,14 +93,11 @@ def find_target_variable(values: dict[str, float]) -> str: ValueError: Invalid input k is not a valid variable """ - is_valid = check_validity(values) - if is_valid: - for variable in valid_variables: - if variable not in values: - return variable - raise ValueError("Input is invalid") - else: - raise ValueError("Input is invalid") + check_validity(values) + for variable in valid_variables: + if variable not in values: + return variable + raise ValueError("Input is invalid") def boyles_law(values: dict[str, float]) -> dict[str, str]: @@ -115,12 +111,12 @@ def boyles_law(values: dict[str, float]) -> dict[str, str]: >>> boyles_law({'p1':2,'v2':1}) Traceback (most recent call last): ... - ValueError: Invalid input expected 3 items got 2 + ValueError: Invalid input expected 3 items, got 2 >>> boyles_law({}) Traceback (most recent call last): ... - ValueError: Invalid input expected 3 items got 0 + ValueError: Invalid input expected 3 items, got 0 >>> boyles_law({'p1':2,'v2':1, 'k':6}) Traceback (most recent call last): @@ -140,32 +136,29 @@ def boyles_law(values: dict[str, float]) -> dict[str, str]: {'v1': '31.32 L'} """ - is_valid = check_validity(values) - if is_valid: - target = find_target_variable(values) - float_precision = ".3f" - if target == "p1": - p1 = float( - format((values["p2"] * values["v2"]) / values["v1"], float_precision) - ) - return {"p1": f"{p1} Pa"} - elif target == "v1": - v1 = float( - format((values["p2"] * values["v2"]) / values["p1"], float_precision) - ) - return {"v1": f"{v1} L"} - elif target == "p2": - p2 = float( - format((values["p1"] * values["v1"]) / values["v2"], float_precision) - ) - return {"p2": f"{p2} Pa"} - else: - v2 = float( - format((values["p1"] * values["v1"]) / values["p2"], float_precision) - ) - return {"v2": f"{v2} L"} + check_validity(values) + target = find_target_variable(values) + float_precision = ".3f" + if target == "p1": + p1 = float( + format((values["p2"] * values["v2"]) / values["v1"], float_precision) + ) + return {"p1": f"{p1} Pa"} + elif target == "v1": + v1 = float( + format((values["p2"] * values["v2"]) / values["p1"], float_precision) + ) + return {"v1": f"{v1} L"} + elif target == "p2": + p2 = float( + format((values["p1"] * values["v1"]) / values["v2"], float_precision) + ) + return {"p2": f"{p2} Pa"} else: - raise ValueError("Input is invalid") + v2 = float( + format((values["p1"] * values["v1"]) / values["p2"], float_precision) + ) + return {"v2": f"{v2} L"} if __name__ == "__main__": From 812ae42274ae9211adf680111ba90bbe18d4a459 Mon Sep 17 00:00:00 2001 From: Akshay B Shetty Date: Tue, 10 Oct 2023 06:06:37 +0000 Subject: [PATCH 6/7] fix: :adhesive_bandage: added return type for check_validity function --- physics/boyles_law.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/physics/boyles_law.py b/physics/boyles_law.py index f3957fd6f554..868da6df2292 100644 --- a/physics/boyles_law.py +++ b/physics/boyles_law.py @@ -42,10 +42,10 @@ valid_variables: list[str] = ["v1", "v2", "p1", "p2"] -def check_validity(values: dict[str, float]): +def check_validity(values: dict[str, float]) -> None: """ - Function takes dictionary as an input and returns True if the input + Function takes dictionary as an input and returns nothing if the input is valid >>> check_validity({}) From c66ced016341b0ac72ac9c8a003afff2090c54b2 Mon Sep 17 00:00:00 2001 From: Akshay B Shetty Date: Tue, 10 Oct 2023 07:17:34 +0000 Subject: [PATCH 7/7] test: :white_check_mark: handled input validation for values less than 0 --- physics/boyles_law.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/physics/boyles_law.py b/physics/boyles_law.py index 868da6df2292..81d5059e6686 100644 --- a/physics/boyles_law.py +++ b/physics/boyles_law.py @@ -58,6 +58,11 @@ def check_validity(values: dict[str, float]) -> None: ... ValueError: Invalid input k is not a valid variable + >>> check_validity({'v1':2,'v2':4,'p1':-6}) + Traceback (most recent call last): + ... + ValueError: Invalid input p1 must be greater than 0 + >>> check_validity({'v1':2,'v2':4,'p1':6}) """ @@ -69,6 +74,9 @@ def check_validity(values: dict[str, float]) -> None: if value not in valid_variables: msg = f"Invalid input {value} is not a valid variable" raise ValueError(msg) + if values[value] <= 0: + msg = f"Invalid input {value} must be greater than 0" + raise ValueError(msg) return @@ -92,6 +100,11 @@ def find_target_variable(values: dict[str, float]) -> str: ... ValueError: Invalid input k is not a valid variable + >>> find_target_variable({'v1':1,'v2':-2,'p2':4}) + Traceback (most recent call last): + ... + ValueError: Invalid input v2 must be greater than 0 + """ check_validity(values) for variable in valid_variables: @@ -123,6 +136,11 @@ def boyles_law(values: dict[str, float]) -> dict[str, str]: ... ValueError: Invalid input k is not a valid variable + >>> boyles_law({'p1':2,'v2':1, 'v1':-6}) + Traceback (most recent call last): + ... + ValueError: Invalid input v1 must be greater than 0 + >>> boyles_law({'p1':100,'v2':150, 'v1':120}) {'p2': '80.0 Pa'}