McKinney Chapter 3 - Practice - Blank

FINA 6333 for Spring 2025

Author

Richard Herron

Announcements

Five-Minute Review

Practice

Swap the values assigned to a and b using a third variable c.

a = 1
b = 2

Swap the values assigned to a and b without using a third variable c.

a = 1
b = 2

What is the output of the following code and why?

1, 1, 1 == (1, 1, 1)
(1, 1, False)

Create a list l1 of integers from 1 to 100.

Slice l1 to create a list l2 of integers from 60 to 50 (inclusive).

Create a list l3 of odd integers from 1 to 21.

Create a list l4 of the squares of integers from 1 to 100.

Create a list l5 that contains the squares of odd integers from 1 to 100.

Use a lambda function to sort strings by the last letter in each string.

strings = ['Clemson', 'Hinson', 'Pillsbury', 'Shubrick']

Given an integer array nums and an integer k, write a function to return the \(k^{th}\) largest element in the array.

Note that it is the \(k^{th}\) largest element in the sorted order, not the \(k^{th}\) distinct element.

Example 1:

Input: nums = [3,2,1,5,6,4], k = 2
Output: 5

Example 2:

Input: nums = [3,2,3,1,2,4,5,5,6], k = 4
Output: 4

I saw this question on LeetCode.

nums = [3,2,3,1,2,4,5,5,6]
k = 4

Given an integer array nums and an integer k, write a function to return the k most frequent elements.

You may return the answer in any order.

Example 1:

Input: nums = [1,1,1,2,2,3], k = 2
Output: [1,2]

Example 2:

Input: nums = [1], k = 1
Output: [1]

I saw this question on LeetCode.

nums = [1,1,1,2,2,3]
k = 2

Test whether the given strings are palindromes.

Input: ["aba", "no"]
Output: [True, False]

tickers = ["AAPL", "GOOG", "XOX", "XOM"]

Write a function calc_returns() that accepts lists of prices and dividends and returns a list of returns.

prices = [100, 150, 100, 50, 100, 150, 100, 150]
dividends = [1, 1, 1, 1, 2, 2, 2, 2]

Rewrite the function calc_returns() as calc_returns_2() so it returns lists of returns, capital gains yields, and dividend yields.

Write a function rescale() to rescale and shift numbers so that they cover the range [0, 1].

Input: [18.5, 17.0, 18.0, 19.0, 18.0]
Output: [0.75, 0.0, 0.5, 1.0, 0.5]

nums = [18.5, 17.0, 18.0, 19.0, 18.0]

Write a function calc_portval() that accepts a dictionary of prices and share holdings and returns the portfolio value

data = {
    "AAPL": (150.25, 10),  # (price, shares)
    "GOOGL": (2750.00, 2),
    "MSFT": (300.75, 5)
}