Python Cheat Sheet
Python Cheat Sheet
1
E.g. N × N × N is countable. 3-tuple (i, j, k).
This function has two cases: fi (0) → N and fi (1) → N . If a = 1, there are a3 tuples (finite) such that
|N | = ℵ0 which means the set of NS is countable. 1 ≤ i ≤ a, 1 ≤ j ≤ a, 1 ≤ k ≤ a. Finite if a = 2, 3, · · ·
f : {0, 1} → N is equivalent to f : {0} → N f : {1} → N .
TheS union of two countable sets results in a countable set, E.g. |(0, 1)| = 2N = ℵ1 = uncountable. (ℵ0 is countable)
N N = N thus f : {0, 1} → N is countably infinite.
E.g. Set of binary functions f : N → {0, 1} is un-
One-to-one and Onto Functions countable.
• A onto B ⇒ every element in B is mapped
• A 1-1 B ⇒ every element in A has a unique mapping Languages
• 1-1 correspondence ⇒ bijective (1-1 and onto). i.e. No • An alphabet Σ is a finite set of symbols, e.g. Σ = {0, 1}
two values map to the same value. Every element in the • A language over alphabet Σ is a set of strings, each
codomain is mapped. |A| = |B|, same cardinality. having its characters drawn from Σ
• Identity function ⇒ input parameter is the same as the • Length of a string s, |s|, is the # of symbols in string s
output value. • Empty string has length 0
• A function has a single outcome for each parameter • Substrings of abc = a, b, c, ab, bc, abc
• Prefix is any # of leading symbols, e.g. , a, ab, abc
E.g. functions f : N → N where N = {1, 2, 3, . . . } • Postfix is any # of trailing symbols, e.g. , c, bc, abc
• Reversal is the string in reverse, e.g. s = abc, sR = cba
1. f is 1-1 but not onto: Special languages:
f (x) = x + 1
f (1) = 2 1. ∅ is the empty language
f (2) = 3
f (3) = 4 2. {} language that contains empty string
..
. 3. Σ∗ (Kleene Closure) contains all strings over Σ
2. f is onto but not 1-1: 4. Σ+ (Positive Closure) contains all strings over Σ except
f (x) = d x2 e the empty string
f (1) = 1 Concatenation {a} · {b} = {a}{b} = {ab}
f (2) = 1
Union {a} ∪ or + or | {b} = {a, b}
f (3) = 2 Operations
Kleene Closure {a}∗ = {, a, aa, aaa, · · · }
f (4) = 2
Positive Closure {a}+ = {a, aa, aaa, · · · }
f (5) = 3
E.g. First 5 strings of language L = {x ∈ {a, b, c}∗ : x
f (6) = 3
.. contains at least one a and at least one b} in lexicographical
. order. ⇒ ab, ba, aab, aba, abb
3. f is a 1-1 correspondence and not an identity function: E.g. Let X = {aa, bb} and Y = {, b, ab}.
f (x) = x − (x + 2 mod 3) + (x mod 3)
f (1) = 2 1. List the strings in the set XY .
f (2) = 3 XY = X · Y = {aa, bb}{, b, ab}
f (3) = 1 = {aa, aab, aaab, bb, bbb, bbab}
f (4) = 5
f (5) = 6 2. List the strings of the set Y ∗ of length three or less.
f (6) = 4 Y ∗ = {, b, ab}∗ of length 3 or less
.. = {, b, bb, bbb, ab, bab, abb}
.
Conditional functions would also work. 3. How many strings of length 6 are there in X ∗ ?
X ∗ = {aa, bb}∗ of exactly length 6
Countability Each symbol has length 2, so there have to be 3 symbols
Set A is countable if ∃ a 1-1 correspondence between A in each string. 23 = 8 strings.
and N . i.e. Can systematically enumerate all elements in
A eventually, labeling each with a unique natural number. (a) 000 → aaaaaa
You might have to find a creative pattern to list them 1-by-1. (b) 001 → aaaabb
(c) 010 → aabbaa
E.g. Set of ± rational numbers is countable if you
enumerate as x1 , −x1 , x2 , −x2 , · · · (d) 011 → aabbbb
(e) 100 → bbaaaa
2
S
(f) 101 → bbaabb 1. L1 is regular, L2 is nonregular, and L1 S L2 is regular.
(g) 110 → bbbbaa L1 = {a, b}∗ , L2 = {an bn |n ≥ 0} → LS
1 L2 = a∗ b∗
∗ i
L1 = a , L2 = {a | i is prime} → L1 L2 = L1
(h) 111 → bbbbbb S
2. L1 is regular, L2 is nonregular, and L1 L2 is nonreg-
E.g. Let L1 = {aaa}∗ , L2 = {a, b}{a, b}{a, b}{a, b}, and ular.
L3 = L∗2 . Describe the strings that are in the languages. L1 = {a∗ }, L2 = {an bn |n ≥ 0} → L1 LS2
S
regular languages: an bn or languages with similar dependencies. DFA’s are 5-tuple: M = (Q, Σ, δ, q0 , F )
3
Q finite set of states that L(M ) = L(M 0 ).
Σ finite alphabet
q0 ∈ Q initial state M = (Q, Σ, δ, q0 , F )
F ⊆Q final states Q = {q0 , q1 , q2 , q3 }
δ transition function Q × Σ → Q
Σ = {a, b}
δ(q, a) = p where q, P are states in Q and a is symbol in Σ
L(M ) language accepted by machine M F = {q0 , q2 , q3 }
δ a b
• Deterministic means you can predict the path it
will take (unlike in NFA). q0 {q1 , q3 } ∅
• Every DFA is an NFA. q1 ∅ {q2 }
• Easy method to make a DFA that cannot contain a q2 {q1 } ∅
certain substring, is to make a DFA that has to accept that q3 {q3 } ∅
substring, and then complement the DFA (non-final states
become final and vice-versa: F̄ = Q − F ). Each state in M 0 corresponds to a subset of states from
M.
Nondeterministic Finite Automata (NFA)
NFA is a finite state machine where for each pair of state M0 = (Q0 , Σ, δ 0 , q00 , F 0 )
0
and input symbol there may be several possible next states. Q = 2Q = {∅, [q3 ], [q2 ], [q2 , q3 ], [q1 ], [q1 , q3 ], [q1 , q2 ],
This distinguishes it from DFA, where the next possible [q1 , q2 , q3 ], [q0 ], [q0 , q3 ], [q0 , q2 ], [q0 , q2 , q3 ], [q0 , q1 ],
state is uniquely determined. Although DFA and NFA
[q0 , q1 , q3 ], [q0 , q1 , q2 ], [q0 , q1 , q2 , q3 ]}
have distinct definitions, they are equivalent, in that, for 0
any given NFA, one may construct an equivalent DFA, and F = all elements in 2Q that contain a state in F
vice-versa (powerset construction). Both types of automata = {[q3 ], [q2 ], [q2 , q3 ], [q1 , q3 ], [q1 , q2 ], [q1 , q2 , q3 ], [q0 ],
recognize only regular languages. [q0 , q3 ], [q0 , q2 ], [q0 , q2 , q3 ], [q0 , q1 ], [q0 , q1 , q3 ],
[q0 , q1 , q2 ], [q0 , q1 , q2 , q3 ]}
An extension of NFA is NFA with -moves, which al-
lows a transformation to a new state without consuming
any input symbols (using as the symbol in the transition). Now the most important part, the transition func-
tion. To complete this part, we look at the NFA state
Accepting an input is similar to that for DFA. When transitions. For example, δ 0 ([q0 ], a) = [q1 , q3 ] because
the last input symbol is consumed, the NFA accepts if and δ(q0 , a) = {q1 , q3 }.
only if there is some set of transitions that will take it to
δ 0 ([q0 ], a) = [q1 , q3 ] ...new state!
an accepting state (it’s allowed to reach a stuck state).
Equivalently, it rejects, if, no matter what transitions are δ 0 ([q0 ], b) = ∅
applied, it would not end in an accepting state. δ 0 ([q1 , q3 ], a) = [q3 ] ...new state!
0
δ ([q1 , q3 ], b) = [q2 ] ...new state!
E.g. Give the state diagram of an NFA (without - 0
δ ([q3 ], a) = [q3 ]
moves) that accepts the language (ab)∗ + a∗ and convert it
to a DFA using the standard algorithm. δ 0 ([q3 ], b) = ∅
δ 0 ([q2 ], a) = [q1 ] ...new state!
0
δ ([q2 ], b) = ∅
0
δ ([q1 ], a) = ∅
δ 0 ([q1 ], b) = [q2 ]
For an NFA M , there exists a DFA M 0 such Proof by contradiction (using pumping lemma):
4
Select z = an bn c2n ∈ L = uvw.
|uv| ≤ n so v consists of a’s.
u = aq , v = ar , w = as bn c2n .
q+r+s=n
Find an i such uv i w ∈/ L.
i = n + 1 → uv n+1 w = q + (n + 1)r + s + n + 2n =
q + r + s + n + 2n + nr = n + n + 2n + nr
Which is not in L because then there would be more
a’s than c’s. Thus L is not regular.
Figure 2: DFA converted from NFA
4. The set of nonpalindromes over {a, b}.
−
Step 1: Assume L is regular, then let n be the constant If L is regular, then so is L (the set of palin-
in the lemma. dromes).
−
Step 2: Select a specific string z ∈ L such that L= {w | w ∈ {a, b}∗ , w = wR }. Use pumping lemma.
|z| ≥ n. Select z = an ban = uvw. u = ai , v = aj , w = ak ban .
z = an+1 bn i+j+k =n
i+k <n
Step 3: Split z into uvw. |uv| ≤ n
By the lemma, |uv| ≤ n, thus v appears within i + j ≤ n, so n ≥ j ≥ 1.
the first n characters (v consists only of a’s). Pump v 0 times (i = 0), we get ai+k ban ∈ / L.
−
z = uvw = aq ar as bn where u = aq , v = ar , and Therefore L is not regular and thus neither is L.
as bn = w.
From the lemma we know: Arbitrary
q+r+s=n+1 Prove false: Show a single example where it’s false (proof
0 ≤ |q| < n by contradiction).
1 ≤ |r| ≤ n because |v| ≥ 1
0 ≤ |s| Cantor’s Theorem: For any set A, |A| < |2A |
q+s<n+1
Sufficient & Necessary Conditions: P ⇒ Q: If
Step 4: Find an i such that uv w ∈ i
/ L, violating P , then Q (P iff Q). P is a sufficient condition for Q to be
the necessary condition. true. Q is a necessary condition for P to be true.
The lemma states that for L to be regular, uv w ∈ L. • If Q is false, we can say P is false.
i
5
(start w/ base case & use as many variables as needed) Leftmost derivation of string aaabb
6
E.g. Show that the following languages over Σ = {a, b} are Push Down Automata (PDA)
not CFL (using pumping lemma). NOTE: Have to prove A PDA is a stack with operations push & pop. It models
for every case it can violate (not done here). a parser. In language-defining power, it’s equivalent to a
CFG. You can only have a single stack, which limits the
1. The set of strings of a’s, b’s, and c’s, with an equal power of PDA. The moves for a PDA are determined by
number of each. (1) the current state, (2) the current input symbol, and
(3) the current symbol at the top of the stack. PDA’s are
Intuition tells us it is not CFL because you would non-deterministic, so it can have a choice of next moves. In
need to count, which we cannot do. So we should with each choice, the PDA can (1) change state, and (2) change
pumping lemma that it is not CFL. There are many the top stack symbol.
cases where it will violate the language property, we M = {Q, Σ, Γ, δ, q0 , Z0 , F }
just need one. Q : finite set of states
Σ : input alphabet
Let z = an bn cn
Γ : stack alphabet
= uv i wxi y δ: transition function
|vwx| ≤ n q0 : start state (q0 ∈ Q)
|vx| ≥ 1 Z 0 : start symbol (Z0 ∈ Γ )
F : set of final states (F ⊆ Q)
v, x contains a0 s :
n n n
E.g. Construct PDA for the following languages and
z }| { z }| { z }| { state if “accept by empty stack” or “accept by final state.”
|a ·{z
· · a} b · · · b c · · · c
TIP: draw out a sample string and corresponding stack.
uvwxy
uv wxi y ∈
i
/ L if i ≥ 2 1. {w|w ∈ (a + b)+ and contains the same number of a’s
and b’s}
2. {wwR w|w ∈ Σ∗ }
We match the a’s and b’s by pushing and pop-
Intuition tells us it is not CFL, because we would need ping. If the stack ever becomes empty, we have the
more than one stack to match. So we try disproving same numbers of a’s and b’s. We will accept this
with pumping lemma. language by empty stack, so the stack will start out
with empty stack symbol Z0 and finish with no symbols.
Let z = an an an
= uv i wxi y
|vwx| ≤ n δ(q0 , a, Z0 ) = {(q0 , AZ0 )} push A on empty stack
|vx| ≥ 1 δ(q0 , b, Z0 ) = {(q0 , BZ0 )} push B on empty stack
v, x contains a s : 0 δ(q0 , a, A) = {(q0 , AA)} keep pushing A
n n n δ(q0 , b, B) = {(q0 , BB)} keep pushing B
z }| { z }| { z }| {
|a ·{z
· · a} a · · · a a · · · a δ(q0 , a, B) = {(q0 , )} ‘a’ encountered, pop B
uvwxy δ(q0 , b, A) = {(q0 , )} ‘b’ encountered, pop A
uv i wxi y ∈
/ L if i ≥ 2 δ(q0 , , Z0 ) = {(q0 , )} end of input, pop Z0
3. {ap |p is a prime}
2. {wwR |w ∈ (a + b)+ }
Intuition says it is not CFL because we would
need to calculate primes. If |Σ| = 1, then both versions This PDA will recognize the language of even
of the pumping lemma (CFL and regular languages) length palindromes (with length greater than 0) over
become the same, and we already proved it in class for Σ = {a, b}. This PDA pushes the input symbols on
the regular language example. So we can safely say the stack until it guesses that it is in the middle and
that this is not CFL. then it compares the input with what is on the stack,
popping off symbols from the stack as it goes. If it
Grammar G = (V, Σ, P, S) reaches the end of the input precisely at the time when
type 0 (unrestricted): α → β (e.g. aAa → aa) the stack is empty, it accepts. Thus it accepts by
type 1 (context-sensitive): α → β and |α| ≤ |β| iff β 6= empty stack, and it is non-deterministic. We need two
type 2 (context-free): α is a single variable (e.g. A → ...) states (q0 for pushing, q1 for popping) because we need
type 3 (regular grammar): A → aB | a | the same number of a’s and b’s (like in the previous
7
language), and it must be a palindrome. 1. Kleene closure (L∗ of L)