# ----------------------------------------------
# Examples: Working with Dictionaries
# ----------------------------------------------

# A dictionary in Python is a collection of key-value pairs.
# Keys and values are separated by a colon ':', and key-value pairs are separated by commas.

# Creating an empty dictionary
my_dict = {}

# Initializing a dictionary with key-value pairs
# Note: You don't need to create an empty dictionary first.
my_dict = {
    'a': 1,
    'b': 17,
    'c': -1,
    'd': 4
}
print(my_dict)
print(my_dict['a'])
# print(my_dict['e'])  # Uncomment to see: KeyError: 'e'

# Safely get the value for key 'c', returns None if key is not found
print(my_dict.get('c'))

# Writing to a dictionary
my_dict['e'] = 5
print(my_dict)  # Dictionary updated with a new key-value pair

# Extending a dictionary with another dictionary
more_data = {'t': [0, 2, 4], 'k': "cheese"}
my_dict.update(more_data)  # Updates/extends `my_dict` with `more_data`
print(my_dict)

# Clearing all entries in the dictionary
my_dict.clear()
print(my_dict)  # Dictionary is now empty

# Creating a dictionary using dict() with keyword arguments
# The keys become strings and must be valid Python identifiers.
students = dict(Victoria=286734, Jeremy=234809, Katie=256789)

print(students)
print(list(students.keys()))     # ['Victoria', 'Jeremy', 'Katie']
print(list(students.values()))   # [286734, 234809, 256789]

# Looping through dictionary keys and accessing their associated values
for name in students:
    print(f"Sciper number of {name} is {students[name]}")
# Sciper number of Victoria is 286734
# Sciper number of Jeremy is 234809
# Sciper number of Katie is 256789

# Removing an entry with `pop()` (returns the value of the removed key)
# print(students.pop("Lena"))  # Uncomment to see: KeyError: 'Lena'
print(students.pop("Victoria"))  # 286734
print(students)  # {'Jeremy': 234809, 'Katie': 256789}

# Resetting the dictionary for further examples
students = dict(Victoria=286734, Jeremy=234809, Katie=256789)
# Current state: {'Victoria': 286734, 'Jeremy': 234809, 'Katie': 256789}

# Deleting a key-value pair with `del`
del students['Katie']
print(students)  # {'Victoria': 286734, 'Jeremy': 234809}

# Clearing all entries in the dictionary
my_dict.clear()
print(my_dict)  # Dictionary is now empty

# ----------------------------------------------
# Using `fromkeys()` to create a new dictionary
# ----------------------------------------------

new_dict = dict.fromkeys(range(4), 0)
print("New dictionary with zeros as values:")
print(new_dict)  # {0: 0, 1: 0, 2: 0, 3: 0}

# ----------------------------------------------------------
# Given an input list of words, group words by their length.
# ----------------------------------------------------------

# Input list of words
words = ["cat", "dog", "elephant", "mouse", "rat", "lion"]

# Initialize an empty dictionary
length_groups = {}

# Group words by their length
for word in words:
    length = len(word)
    if length not in length_groups:
        length_groups[length] = []
    length_groups[length].append(word)

# Print the grouped words
print("Words grouped by length:", length_groups)
# Words grouped by length: {3: ['cat', 'dog', 'rat'], 8: ['elephant'],
#                           5: ['mouse'], 4: ['lion']}

# ----------------------------------------------------------
# Count how many times each word appears in a given string.
# ----------------------------------------------------------

# Input string
text = "apple banana apple orange banana apple"

# Initialize an empty dictionary
word_count = {}

# Process the string
for word in text.split():
    if word in word_count:
        word_count[word] += 1
    else:
        word_count[word] = 1

# Print the word frequency dictionary
print("Word count:", word_count)
# Word count: {'apple': 3, 'banana': 2, 'orange': 1}


# ----------------------------------------------------------
# Sorting a dictionary
# ----------------------------------------------------------

# A dictionary of items with their prices
fruits = {
    "date": 2.80,
    "banana": 1.20,
    "apple": 2.50,
    "elderberry": 1.50,
    "cherry": 3.00
}

print("Original dictionary:")
print(fruits)

# Create a new dictionary with keys in ascending order (alphabetically)
sorted_by_keys = {}
for fruit in sorted(fruits):
    sorted_by_keys[fruit] = fruits[fruit]
print("Dictionary sorted by keys:")
print(sorted_by_keys)
# {'apple': 2.5, 'banana': 1.2, 'cherry': 3.0, 'date': 2.8, 'elderberry': 1.5}

# sorted() on a dictionary returns a list of its sorted keys, not a dictionary.
print(sorted(fruits))
# ['apple', 'banana', 'cherry', 'date', 'elderberry']
