From 0535155a86053d2d894753ad4edb6718a472c092 Mon Sep 17 00:00:00 2001 From: ANDREYDEN Date: Sat, 14 Sep 2019 15:48:35 -0400 Subject: [PATCH] my solution --- .cache/v/cache/lastfailed | 1 + python_solution.py | 38 ++++++++++++++++++++++++++++------- tests/test_python_solution.py | 2 +- 3 files changed, 33 insertions(+), 8 deletions(-) create mode 100644 .cache/v/cache/lastfailed diff --git a/.cache/v/cache/lastfailed b/.cache/v/cache/lastfailed new file mode 100644 index 0000000..9e26dfe --- /dev/null +++ b/.cache/v/cache/lastfailed @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/python_solution.py b/python_solution.py index 2c14cc2..e56f8e1 100644 --- a/python_solution.py +++ b/python_solution.py @@ -1,12 +1,36 @@ - +def convert(string: str): + # check for bools + if string == 'true': + return True + if string == 'false': + return False + + # check for ints and strings + try: + number = int(string) + if str(number) == string: + return number + else: + return string + except (TypeError, ValueError) as e: + return string + def refine_parameters(data: dict): - '''Refine the parameters of a dictionary - - Returns: - Return a dictionary of cleaned input - ''' + clean_dict = {} - # Write your code here. + for key, val in data.items(): + # if we encounter a dictionary - refine that + if type(val) == dict: + clean_dict[key] = refine_parameters(val) + continue + + # if we encounter a list - convert each element + if type(val) == list: + clean_dict[key] = list(map(convert, val)) + continue + + # if we encounter anything else - convert that + clean_dict[key] = convert(val) return clean_dict diff --git a/tests/test_python_solution.py b/tests/test_python_solution.py index 37f1753..38c7be0 100644 --- a/tests/test_python_solution.py +++ b/tests/test_python_solution.py @@ -1,5 +1,5 @@ import json -from python_solution import refine_parameters +from ..python_solution import refine_parameters def test_refine_parameters():