GrPA 1 Dictionary Basics

GrPA 1 Dictionary Basics


Week 5 GrPA


GrPA 1 Dictionary Basics 👨‍💻

Instructions

Question ❓

🫵 You are tasked with implementing a series of functions that perform various operations on dictionaries in Python. These functions will manipulate dictionaries that represent fruit prices and perform different operations as specified.

dictionary_operations(fruit_prices: dict, fruits: list)

Perform a series of operations on the given fruit_prices dictionary based on the fruits list:

  • 👉 Add fruits[0] with a cost of 3.
  • 👉 Modify the cost of fruits[1] to 2.
  • 👉 Increase the cost of fruits[2] by 2.
  • 👉 Delete fruits[3] from fruit_prices.
  • 👉 Print the price of fruits[4].
  • 👉 Print the names of fruits in fruit_prices as a sorted list.
  • 👉 Print the prices of fruits in fruit_prices as a sorted list.
  • 👉 increase_prices(fruit_prices: dict) -> None

💹 Increase the prices of every fruit by 20% and round to two decimal places. Modify the dictionary in place.

dict_from_string(string: str, key_type, value_type)

🔁 Convert a string with comma-separated key-value pairs into a dictionary, converting the keys and values to the specified types.

dict_to_string(D: dict) -> str

🔁 Convert a dictionary back into a string with each key-value pair on a new line, using comprehensions.

Python Code 🐍

Python Code Solution ✅