problems on nested datastructure

Python problems on Datastructure

January 14, 20248 min read

Problem 1: stocks

You are provided with a data structure named stocks which contains information about different companies.

stocks = [
    {
        "name": "Company A",
        "symbol": "CMPA",
        "sector": "Technology",
        "current_price": 100.0,
        "historical_data": [
            {
                "date": "2024-01-10",
                "prices": {
                    "open": 98.0,
                    "close": 100.0,
                    "high": 101.0,
                    "low": 97.0
                },
                "volume": 12000
            },
            {
                "date": "2024-01-09",
                "prices": {
                    "open": 97.0,
                    "close": 98.0,
                    "high": 99.0,
                    "low": 96.0
                },
                "volume": 15000
            }
        ],
        "locations": ["New York", "London"]
    },
    {
        "name": "Company B",
        "symbol": "CMPB",
        "sector": "Finance",
        "current_price": 200.0,
        "historical_data": [
            {
                "date": "2024-01-10",
                "prices": {
                    "open": 198.0,
                    "close": 200.0,
                    "high": 202.0,
                    "low": 196.0
                },
                "volume": 18000
            },
            {
                "date": "2024-01-09",
                "prices": {
                    "open": 196.0,
                    "close": 198.0,
                    "high": 199.0,
                    "low": 195.0
                },
                "volume": 17000
            }
        ],
        "locations": ["Tokyo", "Singapore"]
    },
    {
        "name": "Company C",
        "symbol": "CMPC",
        "sector": "Healthcare",
        "current_price": 300.0,
        "historical_data": [
            {
                "date": "2024-01-10",
                "prices": {
                    "open": 295.0,
                    "close": 300.0,
                    "high": 302.0,
                    "low": 294.0
                },
                "volume": 22000
            },
            {
                "date": "2024-01-09",
                "prices": {
                    "open": 294.0,
                    "close": 295.0,
                    "high": 296.0,
                    "low": 293.0
                },
                "volume": 21000
            }
        ],
        "locations": ["Berlin", "Paris"]
    }
]
  • Each stock is a dictionary with fields for name, symbol, sector, and current price.

  • historical_data is a list of dictionaries, each representing a day's data. Within this, prices is another dictionary with open, close, high, and low values.

  • volume represents the trading volume for that day.

  • locations is a list of strings indicating where the company operates.

Q 1: Read Data

Task: Read and display the current price of "Company B".

Q2: Write Data

Task: Add a new company "Company D" with some initial data to the stocks list.

Q 3: Update Data

Task: Update the current price of "Company C" to 310.0.

Q 4: Delete Data

Task: Remove the historical data for "Company A" on "2024-01-09".

Q5: Read Nested Data

Task: Read and display the closing price of "Company B" on "2024-01-10".

Q6: Update Nested Data

Task: Change the opening price of "Company A" on "2024-01-10" to 99.0.

Q 7: Add Nested Data

Task: Add a new historical data entry for "Company C" on "2024-01-11".

Q 8: Delete an Item from a List Inside a Dictionary

Task: Remove "Singapore" from the locations of "Company B".

Problem 2 :Investment Portfolio

This data structure represents an investment portfolio, including various assets like stocks, bonds, and mutual funds, along with their respective holdings and performance metrics.

investment_portfolio = {
    "investor_name": "Jane Doe",
    "portfolio_id": "JD1234",
    "assets": {
        "stocks": [
            {
                "ticker": "AAPL",
                "quantity": 50,
                "purchase_price": 120.00,
                "current_price": 130.00
            },
            {
                "ticker": "MSFT",
                "quantity": 30,
                "purchase_price": 200.00,
                "current_price": 210.00
            }
        ],
        "bonds": [
            {
                "identifier": "US123456",
                "quantity": 100,
                "purchase_price": 1000.00,
                "current_price": 1020.00,
                "maturity_date": "2030-01-01"
            }
        ],
        "mutual_funds": [
            {
                "name": "XYZ Growth Fund",
                "quantity": 200,
                "purchase_price": 15.00,
                "current_price": 15.50
            }
        ]
    },
    "cash_holdings": 10000.00,
    "investment_goals": {"retirement": 2035, "education": 2025}
}

Q 1: Read Data

Task: Display the current price of the mutual fund "XYZ Growth Fund".

Q 2: Write Data

Task: Add a new stock with ticker "GOOG", 40 shares, a purchase price of 1500.00, and a current price of 1520.00 to the stocks in the assets.

Q3: Update Data

Task: Update the quantity of "AAPL" stock to 60.

Q4: Delete Data

Task: Remove the bond with identifier "US123456" from the bonds in assets.

Q 5: Read Nested Data

Task: Read and display the maturity date of the bond in the portfolio.

Q 6: Update Nested Data

Task: Change the current price of "MSFT" stock to 215.00.

Q7: Add Nested Data

Task: Add a new goal "vacation" set for the year 2028 in the investment goals.

Q 8: Delete an Item from a List Inside a Dictionary

Task: Remove the mutual fund "XYZ Growth Fund" from the portfolio.

Problem 3: Financial Market Analysis

A data structure for analyzing financial markets, including sectors, companies within each sector, and their stock performance.

market_analysis = {
    "sectors": {
        "Technology": {
            "companies": [
                {"name": "Tech Corp A", "ticker": "TCA", "price_change": 5.2},
                {"name": "Tech Corp B", "ticker": "TCB", "price_change": -3.1}
            ]
        },
        "Healthcare": {
            "companies": [
                {"name": "Health Inc A", "ticker": "HIA", "price_change": 4.0},
                {"name": "Health Inc B", "ticker": "HIB", "price_change": 2.5}
            ]
        }
    },
    "index_performance": {
        "S&P 500": {"change": 1.5},
        "NASDAQ": {"change": 2.3},
        "DOW JONES": {"change": -1.2}
    },
    "notable_events": [
        ("2024-01-10", "Interest Rate Decision", "Fed raises rates by 0.25%"),
        ("2024-01-15", "Earnings Report", "Tech Corp A reports better than expected earnings")
    ]
}

Q 1: Read Data

Task: Display the price change of "Health Inc B".

Q 2: Write Data

Task: Add a new sector "Energy" with one company "Energy Co A" having a ticker "ECA" and a price change of -2.0.

Q 3: Update Data

Task: Update the price change of "Tech Corp A" to 6.0.

Q 4: Delete Data

Task: Remove the "DOW JONES" index from index_performance.

Q 5: Read Nested Data

Task: Read and display the change in "NASDAQ" index performance.

Q 6: Update Nested Data

Task: Change the name of "Health Inc A" to "Health Inc Alpha".

Q 7: Add Nested Data

Task: Add a new notable event on "2024-01-20" stating "Acquisition", with the description "Tech Corp B acquires smaller competitor".

Q 8: Delete an Item from a List Inside a Dictionary

Task: Remove the first notable event from the list.

Problem 4: Foreign Exchange Rates

A data structure for tracking foreign exchange rates and historical changes.

foreign_exchange = {
    "base_currency": "USD",
    "exchange_rates": {
        "EUR": {
            "current_rate": 0.85,
            "historical_rates": [
                {"date": "2024-01-10", "rate": 0.84},
                {"date": "2024-01-09", "rate": 0.85}
            ]
        },
        "JPY": {
            "current_rate": 110.00,
            "historical_rates": [
                {"date": "2024-01-10", "rate": 109.50},
                {"date": "2024-01-09", "rate": 110.20}
            ]
        }
    }
}

Q1: Read Data

Task: Display the current exchange rate for the Euro (EUR).

Q2: Write Data

Task: Add a new currency, the British Pound (GBP), with a current rate of 0.75 and two historical rates: 0.76 on "2024-01-10" and 0.77 on "2024-01-09".

Q 3: Update Data

Task: Update the current exchange rate of the Japanese Yen (JPY) to 111.00.

Q4: Delete Data

Task: Remove the historical rates for the Euro (EUR).

Q 5: Read Nested Data

Task: Read and display the exchange rate of the Japanese Yen (JPY) on "2024-01-10".

Q 6: Update Nested Data

Task: Change the rate of the Euro (EUR) on "2024-01-10" to 0.83.

Q 7: Add Nested Data

Task: Add a new historical rate for the Japanese Yen (JPY) on "2024-01-11" with a rate of 109.00.

Q 8: Delete an Item from a List Inside a Dictionary

Task: Remove the historical rate of the Japanese Yen (JPY) on "2024-01-09".

Problem 5: Credit Risk Assessment

A complex structure for assessing credit risk of different individuals based on their financial profiles.

credit_risk_profiles = {
    "individuals": [
        {
            "name": "John Doe",
            "credit_score": 750,
            "outstanding_loans": {
                "auto_loan": {"amount": 20000, "interest_rate": 3.5},
                "home_loan": {"amount": 150000, "interest_rate": 2.8}
            },
            "payment_history": [("2024-01-10", "On Time"), ("2024-01-01", "Late")]
        },
        {
            "name": "Jane Smith",
            "credit_score": 680,
            "outstanding_loans": {
                "credit_card": {"amount": 5000, "interest_rate": 12.0}
            },
            "payment_history": [("2024-01-10", "On Time")]
        }
    ]
}

Q 1: Read Data

Task: Display the interest rate for John Doe's home loan.

Q 2: Write Data

Task: Add a new individual "Alice Johnson" with a credit score of 720, no outstanding loans, and no payment history.

Q 3: Update Data

Task: Update Jane Smith's credit score to 700.

Q 4: Delete Data

Task: Remove the auto loan entry from John Doe's outstanding loans.

Q 5: Read Nested Data

Task: Read and display the latest payment status of John Doe.

Q 6: Update Nested Data

Task: Change the amount of Jane Smith's credit card loan to 5500.

Q 7: Add Nested Data

Task: Add a new payment history entry for John Doe on "2024-01-15" marked as "On Time".

Q 8: Delete an Item from a List Inside a Dictionary

Task: Remove the first payment history entry of Jane Smith.

Problem 6: Hedge Fund Portfolio

A complex structure for a hedge fund's investment portfolio, detailing various types of investments and their performance.

hedge_fund_portfolio = {
    "fund_name": "Alpha Investments",
    "portfolio_value": 50000000,
    "investments": [
        {
            "type": "Equity",
            "holdings": [
                {"ticker": "AAPL", "quantity": 10000, "average_buy_price": 120},
                {"ticker": "TSLA", "quantity": 5000, "average_buy_price": 600}
            ]
        },
        {
            "type": "Fixed Income",
            "holdings": [
                {"bond_issue": "US Treasuries", "amount": 10000000, "yield": 1.5}
            ]
        },
        {
            "type": "Derivatives",
            "holdings": [
                {"instrument": "Options", "details": {"underlying": "GOOGL", "type": "Call", "strike_price": 1500}}
            ]
        }
    ],
    "performance_metrics": {
        "year_to_date_return": 5.2,
        "five_year_annualized_return": 7.1
    }
}

Q 1: Read Data

Task: Display the quantity of "TSLA" equity held in the portfolio.

Q 2: Write Data

Task: Add a new investment type "Real Estate" with one holding of a property "Downtown Complex" valued at 2000000.

Q 3: Update Data

Task: Update the portfolio value to 51000000.

Q 4: Delete Data

Task: Remove the "Derivatives" investment from the portfolio.

Q 5: Read Nested Data

Task: Read and display the yield of the "US Treasuries" bond.

Q 6: Update Nested Data

Task: Change the average buy price of "AAPL" equity to 125.

Q 7: Add Nested Data

Task: Add a new equity holding for "AMZN" with a quantity of 3000 and an average buy price of 3100.\

Q 8: Delete an Item from a List Inside a Dictionary

Task: Remove the first holding (AAPL) under the "Equity" type.

blog author image

sunil s

Quant Developer & Mentor

Back to Blog