From 7bd8d29cb440bbc098e789763a56a2568aab7d52 Mon Sep 17 00:00:00 2001 From: Brittney Huttner Date: Wed, 20 Jan 2021 08:12:15 -0800 Subject: [PATCH 1/2] Fixed typo --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 8e94b53..9f627ac 100644 --- a/README.md +++ b/README.md @@ -39,7 +39,7 @@ turns into: 2) Now write a separate function to do the reverse. -We want you to fork and then create a pull-reqest against this repository and we'll review it. +We want you to fork and then create a pull-request against this repository and we'll review it. Thanks and good luck! From 8df9092e1cc17023cf1c25c8f52db1acd139580e Mon Sep 17 00:00:00 2001 From: Brittney Huttner Date: Wed, 20 Jan 2021 08:34:13 -0800 Subject: [PATCH 2/2] Completed functions for review --- Ontraport.py | 94 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 Ontraport.py diff --git a/Ontraport.py b/Ontraport.py new file mode 100644 index 0000000..7e693fb --- /dev/null +++ b/Ontraport.py @@ -0,0 +1,94 @@ +### TAKE ONE DICT AS INPUT, OUTPUT THE OTHER DICT, THEN VICE VERSA FOR PART TWO ### +# { +# 'one': +# { +# 'two': 3, +# 'four': [ 5,6,7] +# }, +# 'eight': +# { +# 'nine': +# { +# 'ten':11 +# } +# } +# } + +# { +# 'one/two':3, +# 'one/four/0':5, +# 'one/four/1':6, +# 'one/four/2':7, +# 'eight/nine/ten':11 +# } + +### PART ONE - FLATTEN ### +def flatten(c, char_key=''): + """Take a nested dictionary and return a flattened dictionary""" + #return dictionary + ret = {} + #loop over key, value pairs, the initial key is stored by joining a default char_key & key + for k,v in c.items(): + key = char_key+k + #if value is dictionary, recursively update ret using a new char_key seperator until a non-dict value is found + if isinstance(v, dict): + ret.update(flatten(v, key+'/')) + #values are assigned based on the type, list or int + elif isinstance(v, list): + for idx, num in enumerate(v): + u=({key+'/'+str(idx):num}) + ret.update(u) + else: + ret[key] = v + return ret + +container1 = { + 'one': + { + 'two':3, + 'four':[5,6,7] + }, + 'eight': + { + 'nine': + { + 'ten':11 + } + } + } +print (flatten(container1)) + + +### PART TWO - EXPAND ### +def expand(c): + """Take a flattened dictionary and return a nested dictionary""" + #return dictionary + ret = {} + for k, v in c.items(): + cur = ret + #create list of keys, iterate through all keys except the last bc value assignment is handled later + keys = k.split('/') + for idx, key in enumerate(keys[:-1]): + if key not in cur: + if keys[idx+1].isnumeric(): + cur[key]=[] + else: + cur[key]={} + #update current location in dict and keep previous copy to use for isinstance(v, list) + prev=cur + cur=cur[key] + #assign values + if keys[-1].isalpha(): + cur[keys[-1]]=v + else: + prev[key].append(v) + return ret + +container2 = { + 'one/two':3, + 'one/four/0':5, + 'one/four/1':6, + 'one/four/2':7, + 'eight/nine/ten':11 + } +print(expand(container2))