McKinney Chapter 2 - Practice - Blank

FINA 6333 for Spring 2025

Author

Richard Herron

Announcements

Five-Minute Review

Practice

Extract the year, month, and day from an 8-digit date (i.e., YYYYMMDD format) using // (integer division) and % (modulo division).

lb = 20080915

Write a function date that takes an 8-digit date argument and returns a year, month, and date tuple (e.g., return (year, month, day)).

Write a function date_2 that takes an 8-digit date as either integer or string.

Write a function date_3 that takes a list of 8-digit dates as integers or strings.

Write a for loop that prints the squares of integers from 1 to 10.

Write a for loop that prints the squares of even integers from 1 to 10.

Write a for loop that sums the squares of integers from 1 to 10.

Write a for loop that sums the squares of integers from 1 to 10 but stops before the sum exceeds 50.

FizzBuzz

Solve FizzBuzz.

Use ternary expressions to make your FizzBuzz solution more compact.

Triangle

Write a function triangle that accepts a positive integer \(N\) and prints a numerical triangle of height \(N-1\). For example, triangle(N=6) should print:

1
22
333
4444
55555

Two Sum

Write a function two_sum that does the following.

Given a list of integers nums and an integer target, return the indices of the two numbers that add up to target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

You can return the answer in any order.

Here are some examples:

Example 1:

Input: nums = [2,7,11,15], target = 9
Output: [0,1]
Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].

Example 2:

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

Example 3:

Input: nums = [3,3], target = 6
Output: [0,1]

I saw this question on LeetCode.

Best Time

Write a function best_time that solves the following.

You are given a list prices where prices[i] is the price of a given stock on the \(i^{th}\) day.

You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock.

Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return 0.

Here are some examples:

Example 1:

Input: prices = [7,1,5,3,6,4]
Output: 5
Explanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5. Note that buying on day 2 and selling on day 1 is not allowed because you must buy before you sell.

Example 2:

Input: prices = [7,6,4,3,1]
Output: 0
Explanation: In this case, no transactions are done and the max profit = 0.

I saw this question on LeetCode.