a = [1, 2, 3]
a[1, 2, 3]
FINA 6333 for Spring 2024
There are no due dates this week, but please take note of a few due dates next week:
a = [1, 2, 3]
a[1, 2, 3]
b = a
b[1, 2, 3]
Python is zero-indexed!
b[0]1
b[0] = 2_001
b[2001, 2, 3]
So both variables reflect changes to the referenced object.
a[2001, 2, 3]
Variables a and b are two names for the same list in memory!
b is aTrue
We can also reassign a new object, which does not change the original object.
a = 'Och'
a'Och'
b[2001, 2, 3]
def square_root(x):
return x ** 0.5square_root(4)2.0
square_root(9)3.0
Note that white space is requried and not just cosmetic! Our square_root() function does not work without the white space!
# def square_root(x):
# return x ** 0.5
# Cell In[26], line 2
# return x ** 0.5
# ^
# IndentationError: expected an indented block after function definition on line 1if-elif-else blocks let us conditionally run code blocksAgain, note the white space is required and not just cosmetic.
x = 5
if x < 0:
print(f'{x} is negative')
elif x == 0:
print(f'{x} is zero')
else:
print(f'{x} is positive')5 is positive
// (integer division) and % (modulo division).Try 20080915.
1234 / 10012.34
1234 // 10012
1234 % 10034
12 / 52.4
12 // 52
12 % 52
lb_collapse = 20080915lb_collapse // 10_0002008
lb_collapse % 10_000 // 1009
lb_collapse % 10015
date that accepts an integer 8-digit date argument and returns a tuple of the year, month, and date (e.g., return (year, month, day)).def date(ymd):
year = ymd // 10_000
month = ymd % 10_000 // 100
day = ymd % 100
return (year, month, day)date(lb_collapse)(2008, 9, 15)
date(20240112)(2024, 1, 12)
date to accept an 8-digit date as an integer or string.def date_2(ymd):
if type(ymd) is str:
ymd = int(ymd)
year = ymd // 10_000
month = ymd % 10_000 // 100
day = ymd % 100
return (year, month, day)date_2('20080915')(2008, 9, 15)
date to accept a list of 8-digit dates as integers or strings.Return a list of tuples of year, month, and date.
ymds = [20080915, '20080916']def date_3(ymds):
dates = []
for ymd in ymds:
if type(ymd) is str:
ymd = int(ymd)
year = ymd // 10_000
month = ymd % 10_000 // 100
day = ymd % 100
dates.append((year, month, day))
return datesdate_3(ymds)[(2008, 9, 15), (2008, 9, 16)]
for i in range(1, 11):
print(i**2, end=' ')1 4 9 16 25 36 49 64 81 100
Above, I change the end argument to a space to shorten the output.
for i in range(1, 11):
if i%2 == 0:
print(i**2, end=' ')4 16 36 64 100
We can also change the arguments to range() to start at 2 and take steps of 2!
for i in range(2, 11, 2):
print(i**2, end=' ')4 16 36 64 100
sum = 0
for i in range(1, 11):
sum = sum + i**2
sum385
We can replace the sum = sum + i**2 with sum += i**2.
sum = 0
for i in range(1, 11):
sum += i**2
sum385
There are also -=, *=, and /= operators.
sum = 1
for i in range(1, 11):
sum *= i**2
sum13168189440000
total = 0
for i in range(1, 11):
if (total + i**2) > 50:
break
total += i**2
total30
Write a for loop that prints the numbers from 1 to 100. For multiples of three print “Fizz” instead of the number. For multiples of five print “Buzz”. For numbers that are multiples of both three and five print “FizzBuzz”. More here.
for i in range(1, 101):
if (i%3 == 0) and (i%5 == 0):
print('FizzBuzz', end=' ')
elif i%3 == 0:
print('Fizz', end=' ')
elif i%5 == 0:
print('Buzz', end=' ')
else:
print(i, end=' ')1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz 16 17 Fizz 19 Buzz Fizz 22 23 Fizz Buzz 26 Fizz 28 29 FizzBuzz 31 32 Fizz 34 Buzz Fizz 37 38 Fizz Buzz 41 Fizz 43 44 FizzBuzz 46 47 Fizz 49 Buzz Fizz 52 53 Fizz Buzz 56 Fizz 58 59 FizzBuzz 61 62 Fizz 64 Buzz Fizz 67 68 Fizz Buzz 71 Fizz 73 74 FizzBuzz 76 77 Fizz 79 Buzz Fizz 82 83 Fizz Buzz 86 Fizz 88 89 FizzBuzz 91 92 Fizz 94 Buzz Fizz 97 98 Fizz Buzz
'Fizz' + 'Buzz''FizzBuzz'
'Fizz' + 'Fizz''FizzFizz'
'Fizz' * 2'FizzFizz'
'Fizz' * 0''
'Fizz'*True + 'Buzz'*True'FizzBuzz'
for i in range(1, 101):
print('Fizz'*(i%3==0) + 'Buzz'*(i%5==0) if (i%3==0) or (i%5==0) else i, end=' ')1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz 16 17 Fizz 19 Buzz Fizz 22 23 Fizz Buzz 26 Fizz 28 29 FizzBuzz 31 32 Fizz 34 Buzz Fizz 37 38 Fizz Buzz 41 Fizz 43 44 FizzBuzz 46 47 Fizz 49 Buzz Fizz 52 53 Fizz Buzz 56 Fizz 58 59 FizzBuzz 61 62 Fizz 64 Buzz Fizz 67 68 Fizz Buzz 71 Fizz 73 74 FizzBuzz 76 77 Fizz 79 Buzz Fizz 82 83 Fizz Buzz 86 Fizz 88 89 FizzBuzz 91 92 Fizz 94 Buzz Fizz 97 98 Fizz Buzz
The solution above is shorter and uses from neat tricks, but I consider the previous solution easier to read and troubleshoot. The solution above uses the trick that we can multiply a string by True or False to return the string itself or an empty string.
'Hey!'*True'Hey!'
'Hey!'*False''
Someone pointed out an even shorter solution with or!
for i in range(1, 101):
print('Fizz'*(i%3==0) + 'Buzz'*(i%5==0) or i, end=' ')1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz 16 17 Fizz 19 Buzz Fizz 22 23 Fizz Buzz 26 Fizz 28 29 FizzBuzz 31 32 Fizz 34 Buzz Fizz 37 38 Fizz Buzz 41 Fizz 43 44 FizzBuzz 46 47 Fizz 49 Buzz Fizz 52 53 Fizz Buzz 56 Fizz 58 59 FizzBuzz 61 62 Fizz 64 Buzz Fizz 67 68 Fizz Buzz 71 Fizz 73 74 FizzBuzz 76 77 Fizz 79 Buzz Fizz 82 83 Fizz Buzz 86 Fizz 88 89 FizzBuzz 91 92 Fizz 94 Buzz Fizz 97 98 Fizz Buzz
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
def triangle(N):
for i in range(1, N):
print(str(i) * i)triangle(6)1
22
333
4444
55555
The solution above works because a multiplying a string by i concatenates i copies of the string.
'Test' + 'Test' + 'Test''TestTestTest'
'Test' * 3'TestTestTest'
Write a function two_sum that does the following.
Given an array 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.
def two_sum(nums, target):
for i in range(1, len(nums)):
for j in range(i):
if nums[i] + nums[j] == target:
return [j, i]two_sum(nums = [2,7,11,15], target = 9)[0, 1]
two_sum(nums = [3,2,4], target = 6)[1, 2]
two_sum(nums = [3,3], target = 6)[0, 1]
We can write more efficient code once we learn other data structures in chapter 3 of McKinney!
Write a function best_time that solves the following.
You are given an array 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.
def max_profit(prices):
min_price = prices[0]
max_profit = 0
for price in prices:
min_price = price if price < min_price else min_price
profit = price - min_price
max_profit = profit if profit > max_profit else max_profit
return max_profitmax_profit(prices=[7,1,5,3,6,4])5
max_profit(prices=[7,6,4,3,1])0