From 1005e309ddf251bf5383dd9a020fae582c54c198 Mon Sep 17 00:00:00 2001 From: Tao Sophia Date: Sat, 14 Sep 2019 15:36:10 -0400 Subject: [PATCH] k --- python_solution.py | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/python_solution.py b/python_solution.py index 2c14cc2..fe6cb7a 100644 --- a/python_solution.py +++ b/python_solution.py @@ -7,6 +7,34 @@ def refine_parameters(data: dict): ''' clean_dict = {} - # Write your code here. + for key, value in data.items(): + clean_dict[key] = convert(value) return clean_dict + +def convert(value): + new_value = value + if isinstance(value, list): + new_value = [] + for nested_value in value: + new_value.append(convert(nested_value)) + elif isinstance(value, dict): + new_value = {} + for nested_key, nested_value in value.items(): + new_value[nested_key] = convert(nested_value) + else: + new_value = type_cast(value) + return new_value + +def type_cast(value: str): + if value.lower() == 'true': + return True + if value.lower() == 'false': + return False + try: + return int(value) + except ValueError: + try: + return float(value) + except ValueError: + return value