example = "2023-04-13_finances.txt"
def fileYear(filename: str) -> int:
return int(example[:4])
def fileMonth(filename: str) -> int:
return int(example[5:7])
def fileDay(filename: str) -> int:
return int(example[8:10])
def fileSubject(filename: str) -> str:
return filename[11:-4]
print(fileYear(example), fileMonth(example), fileDay(example), fileSubject(example))
waterloo = "1815-06-18_The real Waterloo.txt"
print(fileSubject(waterloo))
2023 4 13 finances The real Waterloo
def reverseInterleave(deck: list) -> list:
return deck[::2] + deck[1::2]
print(reverseInterleave([1, 2, 3, 4, 5, 6]))
[1, 3, 5, 2, 4, 6]
print(list("Hello!"))
print(list(range(42)))
['H', 'e', 'l', 'l', 'o', '!'] [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41]
board = [[" ", "x", " "],
[" ", "x", "o"],
[" ", " ", " "]]
board[2][1] = "o"
print(board)
clearRow = [" ", " ", " "]
for idx in range(len(board)):
board[idx] = clearRow #[:]
board[0][1] = "x"
print(board)
[[' ', 'x', ' '], [' ', 'x', 'o'], [' ', 'o', ' ']] [[' ', 'x', ' '], [' ', 'x', ' '], [' ', 'x', ' ']]
prisoner: tuple[int, str] = (24601, "Jean Valjean")
def longest(strs: list[str]) -> tuple[list[str], int]:
assert len(strs) > 0, "Must have at least one string"
r = [strs[0]]
for s in strs[1:]:
if len(s) > len(r[0]):
r = [s]
elif len(s) == len(r[0]):
r.append(s)
return (r, len(r[0]))
x = longest(["a", "blue", "duck"])
print(x[1])
(res, length) = longest(["a", "blue", "duck"])
print(res, "are of length", length)
4 ['blue', 'duck'] are of length 4
a: list = [1, 2, 3]
a.append(a)
print(a)
#a[3] = 42
a[3][3][3][3][3] = 42
print(a)
print(a[3][3])
[1, 2, 3, [...]] [1, 2, 3, 42]
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Input In [30], in <cell line: 7>() 5 a[3][3][3][3][3] = 42 6 print(a) ----> 7 print(a[3][3]) TypeError: 'int' object is not subscriptable
lst = [2, 4, 6, 0, 1]
x = lst
lst.sort()
print(lst)
print(x)
[0, 1, 2, 4, 6] [0, 1, 2, 4, 6]
lst = ["these", "are", "just", "some", "of", "my", "favorite", "strings"]
lst.sort()
print(lst)
['are', 'favorite', 'just', 'my', 'of', 'some', 'strings', 'these']
lst = [99, "bottles of beer on the wall"]
lst.sort()
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Input In [35], in <cell line: 2>() 1 lst = [99, "bottles of beer on the wall"] ----> 2 lst.sort() TypeError: '<' not supported between instances of 'str' and 'int'
print([3] < [2, 1])
x = [[3], [2, 4]]
x.sort()
print(x)
False [[2, 4], [3]]
x = ([3], [2, 4])
x.sort()
<cell>2: error: "tuple[list[int], list[int]]" has no attribute "sort" [attr-defined]
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) Input In [40], in <cell line: 2>() 1 x = ([3], [2, 4]) ----> 2 x.sort() AttributeError: 'tuple' object has no attribute 'sort'
x = sorted("hello, world!")
print(x)
[' ', '!', ',', 'd', 'e', 'h', 'l', 'l', 'l', 'o', 'o', 'r', 'w']
x = [2, 4, 6, 0, 1]
y = sorted(x)
print(x)
print(y)
[2, 4, 6, 0, 1] [0, 1, 2, 4, 6]
import math
def lgg(lst: list[float]) -> tuple[float, float, float]:
s = sorted(lst)
greatestGap = 0.0
for idx in range(0, len(lst)-1):
gap = s[idx+1] - s[idx]
if gap > greatestGap:
greatestGap = gap
return (s[0], s[-1], greatestGap)
print(lgg([math.pi, math.e, math.sqrt(2), -math.pi]))
(-3.141592653589793, 3.141592653589793, 4.555806215962888)
lst = [1, 2, 3]
help(lst.sort)
Help on built-in function sort:
sort(*, key=None, reverse=False) method of builtins.list instance
Sort the list in ascending order and return None.
The sort is in-place (i.e. the list itself is modified) and stable (i.e. the
order of two equal elements is maintained).
If a key function is given, apply it once to each list item and sort them,
ascending or descending, according to their function values.
The reverse flag can be set to sort in descending order.
lst = [2, 4, 6, 0, 1]
lst.sort(reverse=True)
print(lst)
[6, 4, 2, 1, 0]
x = ["an", "excellent", "list", "of", "strings"]
x.sort()
print(x)
x.sort(key=len)
print(x)
['an', 'excellent', 'list', 'of', 'strings'] ['an', 'of', 'list', 'strings', 'excellent']
def parity(v: int) -> int:
return v%2
x = [8, 6, 7, 5, 3, 0, 9]
y = sorted(x, key=parity)
print("x is still", x)
print("y is now", y)
x.sort(key=parity)
print(x)
x is still [8, 6, 7, 5, 3, 0, 9] y is now [8, 6, 0, 7, 5, 3, 9] [8, 6, 0, 7, 5, 3, 9]
VOWELS = "aeiou"
def vowels(s: str) -> int:
r = 0
for ch in s:
if ch in VOWELS:
r = r + 1
return r
lst = ["these", "are", "just", "some", "my", "favo[u]rite", "words"]
lst.sort(key=vowels)
print(lst)
['my', 'just', 'words', 'these', 'are', 'some', 'favo[u]rite']
lst = [1, 2, 3]
help(lst.reverse)
Help on built-in function reverse:
reverse() method of builtins.list instance
Reverse *IN PLACE*.