# Format: 2023-04-13_all_my_.txt_files.txt
def fileYear(filename: str) -> int:
"""
Get the year out of a filename in GKR Standard Naming Format.
"""
return int(filename[0:4])
def fileMonth(filename: str) -> int:
"""
Get the month out of a filename in GKR Standard Naming Format.
"""
return int(filename[5:7])
def fileDay(filename: str) -> int:
"""
Get the day out of a filename in GKR Standard Naming Format.
"""
return int(filename[8:10])
def fileSubject(filename: str) -> str:
"""
Get the subject out of a filename in GKR Standard Naming Format.
"""
return filename[11:-4]
example = "2023-04-13_finances.txt"
print(fileYear(example), fileMonth(example), fileDay(example), fileSubject(example))
2023 4 13 finances
lst = [2, 4, 6, 0, 1]
x = lst
print("x before sort:", x)
print(lst.sort())
print(lst)
print("x after sort:", x)
x before sort: [2, 4, 6, 0, 1] None [0, 1, 2, 4, 6] x after sort: [0, 1, 2, 4, 6]
lst = [99, "bottles of beer on the wall"]
lst.sort()
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Input In [11], 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'
lst = ["ninety-nine", "bottles"]
lst.sort()
print(lst)
['bottles', 'ninety-nine']
lst = [1j, 2j]
lst.sort()
print(lst)
<cell>2: error: Missing named argument "key" for "sort" of "list" [call-arg]
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Input In [15], in <cell line: 2>() 1 lst = [1j, 2j] ----> 2 lst.sort() 3 print(lst) TypeError: '<' not supported between instances of 'complex' and 'complex'
print([2, 3] < [2, 1])
x = [[3], [2, 1]]
x.sort()
print(x)
for l in x:
l.sort()
print(x)
False [[2, 1], [3]] [[1, 2], [3]]
x = sorted("hello, world!")
print(x)
x = list("hello, world!")
x.sort()
print(x)
#def sorted(s: typing.Sequence):
# lst = list(s)
# lst.sort()
# return lst
[' ', '!', ',', 'd', 'e', 'h', 'l', 'l', 'l', 'o', 'o', 'r', 'w'] [' ', '!', ',', 'd', 'e', 'h', 'l', 'l', 'l', 'o', 'o', 'r', 'w']
def lgg(lst: list[float]) -> tuple[float, float, float]:
"""
Return the least, greatest, and greatest gap in the list lst.
"""
s = sorted(lst)
#print(s)
least = s[0]
greatest = s[-1]
greatestGap = 0.0
for idx in range(len(s) - 1):
# Look at idx and idx + 1
gap = s[idx+1] - s[idx]
if gap > greatestGap:
greatestGap = gap
return (least, greatest, greatestGap)
lst = [3.0, 1, 4, 1, 5, 9, 2, 6, 5]
print(lgg(lst))
(1, 9, 3)
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.
x = [2, 4, 6, 0, 1]
x.sort(reverse=True)
print(x)
[6, 4, 2, 1, 0]
x = ["an", "excellent", "list", "of", "strings"]
#x.sort()
#print(x)
x.sort(key=len)
print(x)
['an', 'of', 'list', 'strings', 'excellent']
lst = [8, 6, 7, 5, 3, 0, 9]
def parity(val: int) -> int:
return val%2
s = sorted(lst, key=parity)
print("After sorted, lst is", lst)
print("Sorted:", s)
After sorted, lst is [8, 6, 7, 5, 3, 0, 9] Sorted: [8, 6, 0, 7, 5, 3, 9]