Skip to content

Commit

Permalink
Merge branch 'exercism:main' into parencontextmgr
Browse files Browse the repository at this point in the history
  • Loading branch information
brocla authored Mar 20, 2024
2 parents fdb01d0 + 0e67a90 commit c75581f
Show file tree
Hide file tree
Showing 6 changed files with 3,075 additions and 0 deletions.
10 changes: 10 additions & 0 deletions test/example-uniondict-normalization/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"authors": [
"brocla"
],
"files": {
"solution": [
"example_uniondict_normalization.py"
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
""" Examples adapted from Mecha Munch Management, Alphametrics, and ChatGPT
ChatGPT Prompt: Write a python function that
reads user settings from a toml file and
merges them with a dictionary of default
settings to create a single dictionary.
If there are any conflicts between the
two sources of settings, the data from
the toml file should be used.
"""


def update_recipes_tuple(ideas, recipe_updates):
"""Mecha Munch Management Example.
Update the recipe ideas dictionary.
:param ideas: dict - The "recipe ideas" dict.
:param recipe_updates: tuple - tuple with updates for the ideas section.
:return: dict - updated "recipe ideas" dict.
"""

# recipe_updates here is a tuple.
# Since this action updates the dict in place,
# the dict then needs to be returned separately, otherwise it is a syntax error.
ideas |= recipe_updates
return ideas


def update_recipes_dict(ideas, recipe_updates):
"""Second Mecha Munch Management Example.
Update the recipe ideas dictionary.
:param ideas: dict - The "recipe ideas" dict.
:param recipe_updates: dict - dictionary with updates for the ideas section.
:return: dict - updated "recipe ideas" dict.
"""

# Since this action returns a *new* dict, it can go directly on the return line.
return dict(ideas) | dict(recipe_updates)

##Example Usage##
ideas = {'Banana Bread' : {'Banana': 1, 'Apple': 1, 'Walnuts': 1, 'Flour': 1, 'Eggs': 2, 'Butter': 1}}

recipe_updates_tuple= (('Banana Bread', {'Banana': 4, 'Walnuts': 2, 'Flour': 1, 'Eggs': 2, 'Butter': 1, 'Milk': 2, 'Eggs': 3}),)

recipe_update_dict= {'Banana Bread': {'Banana': 4, 'Walnuts': 2, 'Flour': 1, 'Eggs': 2, 'Butter': 1, 'Milk': 2, 'Eggs': 3}}

update_recipes_tuple(ideas, recipe_updates_tuple)
# {'Banana Bread': {'Banana': 4, 'Walnuts': 2, 'Flour': 1, 'Eggs': 3, 'Butter': 1, 'Milk': 2}}

update_recipes_dict(ideas, recipe_update_dict)
# {'Banana Bread': {'Banana': 4, 'Walnuts': 2, 'Flour': 1, 'Eggs': 3, 'Butter': 1, 'Milk': 2}}


def assign(letters, selections, lefty, righty):
""" Example from `alphametrics` exercise """

while letters:
new_selections = []

for selection in selections:
slc, choices = selection

if letters[0] in [lefty, righty]:
curr_choices = choices - set([0])

else:
curr_choices = choices

for item in curr_choices:
actual = slc | {letters[0]: item} # combine two dictionaries
new_selections.append((actual, choices - set([item])))

selections = new_selections
letters = letters[1:]
return [slc for slc, _ in selections]


import tomlib

def merge_settings(default_settings, toml_file_path):
# Load settings from TOML file
with open(toml_file_path, 'r') as f:
toml_settings = tomlib.load(f)

# Merge default settings with settings from TOML file
merged_settings = dict(default_settings) | dict(toml_settings)

return merged_settings

# Example usage:
default_settings = {
'timeout': 30,
'retry_count': 3,
'log_level': 'INFO'
}
toml_file_path = 'settings.toml' # Path to the TOML file

final_settings = merge_settings(default_settings, toml_file_path)
print("Final Settings:", final_settings)
28 changes: 28 additions & 0 deletions test/example-uniondict-normalization/mapping.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"placeholder_0": "update_recipes_tuple",
"placeholder_1": "ideas",
"placeholder_2": "recipe_updates",
"placeholder_3": "update_recipes_dict",
"placeholder_4": "recipe_updates_tuple",
"placeholder_5": "recipe_update_dict",
"placeholder_6": "assign",
"placeholder_7": "letters",
"placeholder_8": "selections",
"placeholder_9": "lefty",
"placeholder_10": "righty",
"placeholder_11": "new_selections",
"placeholder_12": "selection",
"placeholder_13": "slc",
"placeholder_14": "choices",
"placeholder_15": "curr_choices",
"placeholder_16": "item",
"placeholder_17": "actual",
"placeholder_18": "_",
"placeholder_19": "merge_settings",
"placeholder_20": "default_settings",
"placeholder_21": "toml_file_path",
"placeholder_22": "f",
"placeholder_23": "toml_settings",
"placeholder_24": "merged_settings",
"placeholder_25": "final_settings"
}
3 changes: 3 additions & 0 deletions test/example-uniondict-normalization/representation.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"version": 2
}
Loading

0 comments on commit c75581f

Please sign in to comment.