This commit is contained in:
bluepython508
2024-01-22 10:56:12 +00:00
parent d320a956f8
commit dc42625cf1
20 changed files with 293 additions and 10 deletions

1
CS1029/notes-2023-11-15 Normal file
View File

@@ -0,0 +1 @@

0
CS1029/notes-2023-11-21 Normal file
View File

1
CS1029/notes-2023-11-28 Normal file
View File

@@ -0,0 +1 @@

View File

@@ -0,0 +1,37 @@
1. a) 5 8 7 * * = 280
b) 6 1 + 6 * 1 + 6 * = 258
c) 26 2 ^ 36 4 ^ * = 1 135 420 416
d) 17 3 P = 4080
e) 26 2 P 34 4 P * = 723 465 600
f) 8 5 C 11 7 C * = 18 480
2. a) 8 2 C 5 2 C 3 2 C * * = 840
b) 8 2 C 14 4 C * = 28 028
3. a) (sides + 1), or 7 for the likely indented d6
b) (sides_1 + sides_2), or 1 for the likely intended d6
c) 5
4. (Assuming the unaccented latin alphabet only)
26 3 ^ = 17 576
5. 26 2 ^ 10 4 ^ 10 2 ^ + 26 4 ^ * * = 3 120 049 337 600
6. 99 50 * 1 + = 4 951
7. a) 10 4 C = 210
b) 10 4 C 10 3 C 10 2 C 10 1 C + + + = 385
c) 10 4 C 10 5 C 10 6 C 10 7 C 10 8 C 10 9 C 1 + + + + + + = 848
8. a) 100 4 P = 94 109 400
b) 99 3 P = 941 094
c) 99 4 P = 90 345 024
d) 97 4 ! * = 2 328
9. a) 25 4 C = 12 650
b) 25 4 P = 303 600
10. 3 5 ^ = 243
11. 5 3 ^ 3 ! / = 20

View File

@@ -0,0 +1,24 @@
01. 4 52 / = 1/13
02. 50 100 / = 1/2
03. 1 1 52 5 C / - = 2598959/2598960
04. 4 1 C 52 5 C / 4 2 C 52 5 C / 4 3 C 52 5 C / 1 52 5 C / sum = 1/173264
05.
a) 1 50 5 C / = 1/2118760
b) 1 52 5 C / = 1/2598960
c) 1 56 5 C / = 1/3819816
d) 1 60 5 C / = 1/5461512
06.
a) 1 200 3 P / = 1/7880400
b) 1 200 / 3 ^ = 1/8000000
07.
a) 1 26 ! / = 1/403291461126605635584000000
b) 1 26 / = 1/26
08. 4 3 C 1 2 / 4 ^ * = 1/4
09. 6 1 C 1 2 / 6 ^ * = 3/32
10. 2 5 / 1 2 / * 1 3 / / = 3/5
11. P(U|P) = P(P|U)P(U) / P(P|U)P(U) + P(P|U')P(U')
) 96 % 8 % * 96 % 8 % * 1 8 % - 9 % * + / = 64/133
12.
a) 8 % 98 % * 8 % 98 % * 1 8 % - 3 % * + / = 196/265
b) 1 196 265 / - = 69/265
c) 2 % 8 % * 2 % 8 % * 92 % 97 % * + / = 4/2235

View File

@@ -0,0 +1,17 @@
from hashlib import sha256
from itertools import cycle
import base64
def crypt(msg: bytes, key: str) -> bytes:
key_h = sha256()
key_h.update(key.encode('utf8'))
key = key_h.digest()
return bytes(p ^ k for (p, k) in zip(msg, cycle(key)))
def encrypt(msg: str, key: str) -> str:
return base64.b64encode(crypt(bytes(msg, 'utf8'), key)).decode('utf8')
def decrypt(msg: str, key: str) -> str:
return crypt(base64.b64decode(msg), key).decode('utf8')

View File

@@ -0,0 +1,19 @@
safe(Q1X-Q1Y, Q2X-Q2Y) :-
between(0, 7, Q1Y),
between(0, 7, Q2Y),
Q1X =\= Q2X,
Q1Y =\= Q2Y,
abs(Q1X - Q2X) =\= abs(Q1Y - Q2Y).
safe(_, []).
safe(QN, [Q | QS]) :- safe(QN, [Q | QS], 1).
safe(_, [], _).
safe(QN, [Q | QS], QX) :- safe(0-QN, QX-Q), safe(QN, QS, QX + 1).
add(Qs, QsN) :-
QsN = [Q | Qs],
safe(Q, Qs).
add(1, Qs, QsN) :- add(Qs, QsN).
add(N, Qs, QsN) :- add(Qs, QA), add(M, QA, QsN), succ(M, N).
solution(Qs) :- add(8, [], Qs).

View File

@@ -0,0 +1,18 @@
def safe(q1, q2):
(q1x, q1y) = q1
(q2x, q2y) = q2
return q1x != q2x and q1y != q2y and abs(q1x - q2x) != abs(q1y - q2y)
def possible_positions(current, n):
return ((*current, y) for y in range(n) if all(safe((len(current), y), q) for q in enumerate(current)))
def solutions_r(current, n = 8):
if len(current) == n:
yield current
return
for pos in possible_positions(current, n):
yield from solutions_r(pos, n)
def solutions(n = 8):
return solutions_r((), n)

View File

@@ -0,0 +1,7 @@
def l(n=9):
for x in range(1, n):
print(" " * (x % 2), "*")
def r(n=5):
for x in range(n, -1, -1):
print("*" * (x * 2 + 1))

1
CS1032/notes-2023-11-16 Normal file
View File

@@ -0,0 +1 @@

0
CS1032/notes-2023-11-17 Normal file
View File

View File

@@ -0,0 +1,4 @@
import sys
for line in sys.stdin:
print(line.split('#')[0].rstrip())

View File

@@ -0,0 +1,11 @@
#! /usr/bin/env python3
import sys, random
with open(sys.argv[1]) as words_f:
words = [word for line in words_f if 3 <= len(word := line.strip()) < 10 and word.lower() == word]
pair = []
while not 8 <= sum(map(len, pair)) <= 10:
pair = random.choices(words, k=2)
print(''.join(w.title() for w in pair))

View File

@@ -0,0 +1,6 @@
#! /usr/bin/env python3
import sys, re
redacted = re.compile(f"({'|'.join([s.strip() for s in open(sys.argv[1])])})")
for line in sys.stdin:
print(redacted.sub(lambda m: '*' * len(m.group(0)), line), end='')

View File

@@ -0,0 +1,9 @@
#! /usr/bin/env python3
sum = 0
while True:
i = input(f"[{sum:g}]: ")
if not i: break
try:
sum += float(i)
except ValueError: print(f"\t'{i}' is not a number")

View File

@@ -0,0 +1,33 @@
#! /usr/bin/env python3
def sort(list):
if len(list) <= 1: return list
pivot = list.pop()
return sort([v for v in list if v < pivot]) + [v for v in list if v == pivot] + [pivot] + sort([v for v in list if v > pivot])
def search(list, elem, start=0):
if list == [elem]:
return start
elif len(list) <= 1:
return None
i = len(list) // 2
pivot = list[i]
if elem > pivot:
return search(list[i:], elem, start = i + start)
elif elem < pivot:
return search(list[:i], elem, start=start)
elif elem == pivot:
return i + start
raise ValueError("Got NaN!")
if __name__ == '__main__':
l = []
while line := input('> '):
l.append(line)
i = search(sort(l), input('Goal: '))
if i:
print(f"Found at {i}")
else:
print("Not present")

View File

@@ -45,7 +45,7 @@ Solutions invariant.
\end{align*} \end{align*}
\section*{As Matrices} \section*{As Matrices}
\begin{align*} \begin{align*}
\systeme{ & \systeme{
x + 2y = 1, x + 2y = 1,
2x - y = 3 2x - y = 3
} }
@@ -53,8 +53,8 @@ Solutions invariant.
\begin{pmatrix}[cc|c] \begin{pmatrix}[cc|c]
1 & 2 & 1 \\ 1 & 2 & 1 \\
2 & -1 & 3 2 & -1 & 3
\end{pmatrix} \end{pmatrix} \\
& \systeme{ & \systeme{
x - y + z = -2, x - y + z = -2,
2x + 3y + z = 7, 2x + 3y + z = 7,
x - 2y - z = -2 x - 2y - z = -2
@@ -347,12 +347,107 @@ Inversion of a linear transformation is equivalent to inversion of its represent
\[ \forall A: \text{$n$x$n$ matrix}.\quad P_A\paren{\lambda} = \det{\paren{A - \lambda I_n}} \tag{characteristic polynomial in $\lambda$}\] \[ \forall A: \text{$n$x$n$ matrix}.\quad P_A\paren{\lambda} = \det{\paren{A - \lambda I_n}} \tag{characteristic polynomial in $\lambda$}\]
Eigenvalues of $A$ are the solutions of $P_A\paren{\lambda} = 0$ Eigenvalues of $A$ are the solutions of $P_A\paren{\lambda} = 0$
\begin{align*} \begin{align*}
& A\vec{x} = \lambda\vec{x} & x \neq 0\\ & A\vec{x} = \lambda\vec{x} & x \neq 0 \\
\iff & A\vec{x} - \lambda\vec{x} = 0 \\ \iff & A\vec{x} - \lambda\vec{x} = 0 \\
\iff & (A - \lambda I_n)\vec{x} = 0 \\ \iff & (A - \lambda I_n)\vec{x} = 0 \\
\iff & \det{\paren{A - \lambda I_n}} = 0 \\ \iff & \det{\paren{A - \lambda I_n}} = 0 \\
& \quad \text{ or $\paren{A - \lambda I_n}$ is invertible and $x = 0$ } & \quad \text{ or $\paren{A - \lambda I_n}$ is invertible and $x = 0$ }
\end{align*} \end{align*}
\[ P_{R\theta}(\lambda) = \frac{2\cos{\theta} \pm \sqrt{-4\lambda^2\sin^2{\theta}}}{2}\] \[ P_{R\theta}(\lambda) \text{ has roots } \frac{2\cos{\theta} \pm \sqrt{-4\lambda^2\sin^2{\theta}}}{2}\]
\[ R_\theta \text{ has eigenvalues }\iff \sin{\theta} = 0 \] \[ R_\theta \text{ has eigenvalues }\iff \sin{\theta} = 0 \]
\end{document} \subsubsection*{Example}
\begin{align*}
A & = \begin{pmatrix} 4 & 0 & 1 \\ -2 & 1 & 0 \\ -2 & 0 & 1 \end{pmatrix} \\
P_A(\lambda) & = \det{A - \lambda I_3} = \det{\begin{pmatrix} 4 - \lambda & 0 & 1 \\ -2 & 1 - \lambda & 0 \\ -2 & 0 & 1 - \lambda \end{pmatrix}} = (1 - \lambda)\det{\begin{pmatrix}4 - \lambda & 1 \\ -2 & 1 - \lambda \end{pmatrix}} \\
& = (1 - \lambda)\paren{(4 - \lambda)(1 - \lambda) + 2} = (1 - \lambda)(\lambda^2 - 5\lambda + 6) \\
& = (1 - \lambda)(2 - \lambda)(3 - \lambda) \\
\lambda & = 1, 2, 3 \\
A\vec{x} & = \lambda\vec{x}~\forall. \text{ eigenvectors } \vec{x} \\
(A - \lambda I_n)\vec{x} & = 0 \\
& \begin{pmatrix} 3 & 0 & 1 \\ -2 & 0 & 0 \\ -2 & 0 & 0 \end{pmatrix}\vec{x} = \begin{pmatrix} 0 \\ 0 \\ 0 \end{pmatrix} \\
& \text{ eigenvectors with eigenvalue 1 are } s\vec{e}_2~~\forall~s \in \R, \neq 0 \\
(A - 2I_n)\vec{x} & = 0 \\
& \begin{pmatrix} 2 & 0 & 1 \\ -2 & -1 & 0 \\ -2 & 0 & -1 \end{pmatrix}\vec{x} = \begin{pmatrix} 0 \\ 0 \\ 0\end{pmatrix} \\
& \systeme{
2x_1 + x_3 = 0,
-2x_1 - x_2 = 0,
-2x_1 -x_3 = 0
}: s\begin{pmatrix} 1 \\ -2 \\ -2 \end{pmatrix} \\ \\
B & = \begin{pmatrix} 5 & 3 & 3 \\ -3 & -1 & -3 \\ -1 & -3 & -1 \end{pmatrix}
\end{align*}
Repeated roots of the characteristic polynomial lead to multiple variables.
\[ \forall \text{ matrices } A.~A \text{ is invertible } \iff 0 \text{ is not an eigenvalue }\]
\begin{align*}
\text{If $0$ is an eigenvalue, } P_A(0) = 0 \therefore \det{\paren{A - 0I_n}} = 0
\end{align*}
\[ P_A(\lambda) = \det{\paren{(-I_n)(\lambda I_n - A)}} = \det{-I_n}\det{\lambda I_n - A}\]
\[ = (-1)^n \lambda^n + c_{n-1}\lambda^{n-1} ... \]
\begin{description}
\item[Trace] The sum of the diagonal of a matrix
\[ c_{n - 1} = (-1)^{n+1}\operatorname{tr}A \]
\item[Cayley-Hamilton Theorem:] \( P_A(A) = 0_n \)
\end{description}
\subsubsection*{Example}
\begin{align*}
A & = \begin{pmatrix} 1 & 4 \\ 3 & 2 \end{pmatrix} \\
P_A(\lambda) & = (1 - \lambda)(2 - \lambda) - 12 \\
& = 2 - 3\lambda + \lambda^2 - 12 \\
& = \lambda^2 - 3\lambda - 10 \\
P_A & (5 \text{ or } -2) = 0 \\
P_A(A) & = 0 \\
A^2 & = 3A - 10I_2 \\
A^3 & = (3A - 10I_2)A \\
A^{n + 2} & = 3A^{n+1} + 10A^n
\end{align*}
\subsection*{Diagonalization}
\begin{description}
\item[Similarity of matrices] $A$ and $B$ are \emph{similar} iff there exists an invertible matrix $P$ such that $B = P^{-1}AP$
\[ T_\alpha \text{ is similar to } T_0 (P = R_{-\alpha}) \]
\end{description}
For similar $A$, $B$:
\begin{itemize}
\item $\det{A}$ = $\det{B}$
\item $P_A(\lambda) = P_B(\lambda)$
\[\det{\paren{A - \lambda I_n}} = \det{\paren{PBP^{-1} - \lambda PP^{-1}}} = \det{\paren{P(B - \lambda I_n)P^{-1}}}\]
\[ (A - \lambda I_n) \text{ and } (B - \lambda I_n) \text{ are similar }\]
\item eigenvalues are the same
\item trace is the same
\end{itemize}
\begin{description}
\item[Diagonalizable Matrix] a square matrix that is similar to a diagonal matrix
\[ P^{-1}AP = D \]
$P$ diagonalizes $A$ \quad
(P is not necessarily unique)
\end{description}
An $n$x$n$ matrix $A$ is dagonalizable iff there exists a matrix $P = (\vec{x}_1, \vec{x}_2, ...)$, where $\vec{x}_i$ is an eigenvector of $A$
\[ P^{-1}AP = \begin{pmatrix} \lambda_1 & 0 & \cdots & \cdots \\ 0 & \lambda_2 & 0 & \cdots \\ \vdots & \vdots & \ddots & \cdots \end{pmatrix} \]
i.e. it's diagonal, with the $ii^\text{th}$ element equal to the eigenvalue corresponding to $\vec{x}_i$
\subsubsection*{Example}
\begin{align*}
A & = \begin{pmatrix} 4 & 0 & 1 \\ -2 & 1 & 0 \\ -2 & 0 & 1 \end{pmatrix} \\
\lambda_1 & = \begin{pmatrix} 0 \\ s \\ 0 \end{pmatrix} \\ \lambda_2 & = \begin{pmatrix} t \\ -2t \\ -2t \end{pmatrix} \\ \lambda_3 & = \begin{pmatrix} u \\ -u \\ -u \end{pmatrix} \\
P & = \begin{pmatrix}
0 & 1 & 1 \\
1 & -2 & -1 \\
0 & -2 & -1
\end{pmatrix} \\
P^{-1} & = \begin{pmatrix}
0 & 1 & -1 \\ -1 & 0 & -1 \\ 2 & 0 & 1
\end{pmatrix} \\
P^{-1}AP & = \begin{pmatrix}
1 & 0 & 0 \\ 0 & 2 & 0 \\ 0 & 0 & 3
\end{pmatrix}
\end{align*}
\subsection*{Linear Independence}
For any set of vectors $V = { v_i }~\forall_{i<n}$ in $R^n$, $v_i$ are linearly independent if
\[ \sum k_i v_i = 0 \implies \forall i.~ k_i = 0 \quad (k_i \in \R) \]
This implies that no vector in $V$ can be written as a sum of scalar multiples of the others. \\
A square matrix is invertible iff its columns are linearly independent in $R^n$
\\ An $n$x$n$ matrix $A$ is diagonalizable iff it admits $n$ linearly independent eigenvectors \\
For $\vec{x}_i$ eigenvectors of $A$ corresponding to distinct eigenvalues $\lambda_i$, $\{ \vec{x}_i \}$ is linearly independent. \\
Note: the inverse is \emph{not} true: \( \exists \text{ matrices } $A$\) with repeated eigenvalues and linearly independent eigenvectors.
\[ \forall k \in \N, A = PDP^{-1}.~A^k = (PDP^{-1})^k = PD^kP^{-1} \]
Exponentiation of a diagonal matrix is elementwise
\end{document}