Mortgage Function in Python

How can the mortgage function be extended to return a dictionary of address:mortgage?

What modifications need to be made to the filtering code from 1.4.1 to create three separate dictionaries based on mortgage amounts?

Is it possible to modify one value in the jumboMortgages dictionary without changing the original dict? Why or why not?

How can lists of amounts be extracted from each separate dictionary and can the miniMortgages dictionary be changed without affecting the original dict?

Extending the Mortgage Function

To extend the mortgage function and return a dictionary of address:mortgage, we can modify the existing code as follows:

def mortgage():
    randomlist = []
    mortgage_dict = {}
    for i in range(0, 11):
        n = random.randint(100, 1000)
        randomlist.append(n)
    for i in randomlist:
        address = ''.join(random.choices(string.ascii_uppercase + string.digits, k=6))
        mortgage_dict[address] = i
    return mortgage_dict

This modified code will generate a random list of mortgage amounts (`randomlist`) and create a dictionary (`mortgage_dict`) where the address is a unique six-character string and the mortgage amount is the value.

Each address will have a corresponding mortgage amount from the `randomlist`.

Modifying the Filtering Code

To modify the filtering code to create three separate dictionaries based on mortgage amounts, you can add the following code after the `mortgage()` function:

miniMortgages = {}
standardMortgages = {}
jumboMortgages = {}
for address, amount in mortgage().items():
    if amount < 200:
        miniMortgages[address] = amount
    elif 200 <= amount <= 467:
        standardMortgages[address] = amount
    else:
        jumboMortgages[address] = amount
print(miniMortgages)
print(standardMortgages)
print(jumboMortgages)

This code will iterate through the dictionary returned by the `mortgage()` function and assign addresses and mortgage amounts to the respective dictionaries based on the amount range.

Modifying Values and Lists

To modify one value in the `jumboMortgages` dictionary and check if the original dictionary remained intact or changed, you can add the following code:

jumboMortgages['address'] = 500
print(jumboMortgages)
print(jumboMortgages is original_jumboMortgages)

To extract the lists of amounts from each separate dictionary and modify one value in the `miniMortgages` list, you can add the following code:

miniMortgages_list = list(miniMortgages.values())
standardMortgages_list = list(standardMortgages.values())
jumboMortgages_list = list(jumboMortgages.values())
miniMortgages_list[0] = 150
print(miniMortgages_list)
print(miniMortgages)

The `miniMortgages` dictionary will not change when modifying the `miniMortgages_list` because the list is a separate copy of the values from the original dictionary.

← The importance of documentation in business practice The divide and conquer method in troubleshooting complex problems →