author | Christian Urban <christian.urban@kcl.ac.uk> |
Tue, 30 May 2023 16:58:06 +0100 | |
changeset 909 | a04efdd5e7a3 |
parent 878 | 6722cd24c784 |
child 916 | 10f834eb0a9e |
permissions | -rw-r--r-- |
631 | 1 |
% !TEX program = xelatex |
0 | 2 |
\documentclass{article} |
249
377c59df7297
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
3 |
\usepackage{../style} |
0 | 4 |
|
876 | 5 |
\newcommand{\solution}[1]{% |
6 |
\begin{quote}\sf% |
|
7 |
#1% |
|
8 |
\end{quote}} |
|
878 | 9 |
\renewcommand{\solution}[1]{} |
876 | 10 |
|
0 | 11 |
\begin{document} |
12 |
||
13 |
\section*{Homework 1} |
|
14 |
||
331
a2c18456c6b7
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
294
diff
changeset
|
15 |
\HEADER |
a2c18456c6b7
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
294
diff
changeset
|
16 |
|
0 | 17 |
\begin{enumerate} |
249
377c59df7297
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
18 |
|
401
5d85dc9779b1
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
394
diff
changeset
|
19 |
\item {\bf (Optional)} If you want to run the code presented |
5d85dc9779b1
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
394
diff
changeset
|
20 |
in the lectures, install the Scala programming language |
5d85dc9779b1
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
394
diff
changeset
|
21 |
available (for free) from |
249
377c59df7297
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
22 |
|
743 | 23 |
\begin{center} |
24 |
\url{http://www.scala-lang.org} |
|
25 |
\end{center} |
|
26 |
||
27 |
and the Ammonite REPL from |
|
28 |
||
29 |
\begin{center} |
|
30 |
\url{https://ammonite.io} |
|
31 |
\end{center} |
|
0 | 32 |
|
401
5d85dc9779b1
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
394
diff
changeset
|
33 |
If you want to follow the code I present during the |
5d85dc9779b1
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
394
diff
changeset
|
34 |
lectures, read the handout about Scala. |
0 | 35 |
|
639 | 36 |
%\item {\bf (Optional)} Have a look at the crawler programs. |
37 |
% Can you find a usage for them in your daily programming |
|
38 |
% life? Can you improve them? For example in cases there |
|
39 |
% are links that appear on different recursion levels, the |
|
40 |
% crawlers visit such web-pages several times. Can this be |
|
41 |
% avoided? Also, the crawlers flag as problematic any page |
|
42 |
% that gives an error, but probably only 404 Not Found |
|
43 |
% errors should be flagged. Can you change that?) |
|
104
ffde837b1db1
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
102
diff
changeset
|
44 |
|
640 | 45 |
\item {\bf (Optional)} Have a look at the catastrophic backtracking |
46 |
programs uploaded on KEATS. Convince yourself that they really require |
|
47 |
a lot of computation time. If you have similar examples in your own |
|
48 |
favourite programming language, I am happy to hear about it. |
|
49 |
||
50 |
||
401
5d85dc9779b1
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
394
diff
changeset
|
51 |
\item Read the handout of the first lecture and the handout |
5d85dc9779b1
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
394
diff
changeset
|
52 |
about notation. Make sure you understand the concepts of |
498 | 53 |
strings and languages. In the context of the CFL-course, |
401
5d85dc9779b1
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
394
diff
changeset
|
54 |
what is meant by the term \emph{language}? |
9 | 55 |
|
876 | 56 |
\solution{A language - in this context - is just a set of |
57 |
strings. Some of these sets can actually not be described by |
|
58 |
regular expressions. Only regular​ languages can. This is |
|
59 |
something for lecture 3.} |
|
60 |
||
550 | 61 |
\item Give the definition for regular expressions---this is an |
498 | 62 |
inductive datatype. What is the |
355
a259eec25156
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
331
diff
changeset
|
63 |
meaning of a regular expression? (Hint: The meaning is |
a259eec25156
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
331
diff
changeset
|
64 |
defined recursively.) |
0 | 65 |
|
876 | 66 |
\solution{Here I would also expect the grammar for basic regular |
67 |
expressions and the definition of the recursive L-function. Discuss |
|
68 |
differences between $r_1 + r_2$ and $r^+$. Discuss differences between |
|
69 |
``real-life regexes'' and regexes in this module.} |
|
70 |
||
355
a259eec25156
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
331
diff
changeset
|
71 |
\item Assume the concatenation operation of two strings is |
a259eec25156
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
331
diff
changeset
|
72 |
written as $s_1 @ s_2$. Define the operation of |
a259eec25156
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
331
diff
changeset
|
73 |
\emph{concatenating} two sets of strings. This operation |
394
2f9fe225ecc8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
355
diff
changeset
|
74 |
is also written as $\_ \,@\, \_$. According to |
2f9fe225ecc8
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
355
diff
changeset
|
75 |
this definition, what is $A \,@\, \{\}$ equal to? |
498 | 76 |
Is in general $A\,@\,B$ equal to $B\,@\,A$? |
0 | 77 |
|
876 | 78 |
\solution{ What is $A @ {[]}$? Are there special cases |
79 |
where $A @ B = B @ A$? } |
|
80 |
||
355
a259eec25156
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
331
diff
changeset
|
81 |
\item Assume a set $A$ contains 4 strings and a set $B$ |
a259eec25156
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
331
diff
changeset
|
82 |
contains 7 strings. None of the strings is the empty |
a259eec25156
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
331
diff
changeset
|
83 |
string. How many strings are in $A \,@\, B$? |
249
377c59df7297
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
227
diff
changeset
|
84 |
|
876 | 85 |
\solution{28, but there are corner cases where there are fewer |
86 |
than 28 elements. Can students think of such corner cases? |
|
87 |
For example $A = \{a, ab, \ldots\}$, $B = \{bc, c,\ldots\}$ } |
|
88 |
||
267
a1544b804d1e
updated homeworks
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
258
diff
changeset
|
89 |
\item How is the power of a language defined? (Hint: There are two |
a1544b804d1e
updated homeworks
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
258
diff
changeset
|
90 |
rules, one for $\_^0$ and one for $\_^{n+1}$.) |
109
f2a90dda7e3b
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
104
diff
changeset
|
91 |
|
876 | 92 |
\solution{Two rules: 0-case and n+1 case.} |
93 |
||
355
a259eec25156
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
331
diff
changeset
|
94 |
\item Let $A = \{[a], [b], [c], [d]\}$. (1) How many strings |
438
84608b4b3578
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
416
diff
changeset
|
95 |
are in $A^4$? (2) Consider also the case of $A^4$ where one of |
355
a259eec25156
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
331
diff
changeset
|
96 |
the strings in $A$ is the empty string, for example $A = |
a259eec25156
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
331
diff
changeset
|
97 |
\{[a], [b], [c], []\}$. |
293
ca349cfe3474
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
267
diff
changeset
|
98 |
|
876 | 99 |
\solution{121 is correct. But make sure you understand why it is 121 |
100 |
in cases you do not have a computer at your fingertips.} |
|
101 |
||
507 | 102 |
\item (1) How many basic regular expressions are there to match |
776 | 103 |
\textbf{only} the string $abcd$? (2) How many if they cannot include |
444
3056a4c071b0
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
438
diff
changeset
|
104 |
$\ONE$ and $\ZERO$? (3) How many if they are also not |
3056a4c071b0
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
438
diff
changeset
|
105 |
allowed to contain stars? (4) How many if they are also |
401
5d85dc9779b1
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
394
diff
changeset
|
106 |
not allowed to contain $\_ + \_$? |
0 | 107 |
|
876 | 108 |
\solution{1-3 are infinite (tell the idea why - examples); 4 is five - remember regexes are trees.} |
109 |
||
355
a259eec25156
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
331
diff
changeset
|
110 |
\item When are two regular expressions equivalent? Can you |
a259eec25156
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
331
diff
changeset
|
111 |
think of instances where two regular expressions match |
a259eec25156
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
331
diff
changeset
|
112 |
the same strings, but it is not so obvious that they do? |
a259eec25156
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
331
diff
changeset
|
113 |
For example $a + b$ and $b + a$ do not count\ldots they |
a259eec25156
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
331
diff
changeset
|
114 |
obviously match the same strings, namely $[a]$ and |
a259eec25156
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
331
diff
changeset
|
115 |
$[b]$. |
403
564f7584eff1
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
401
diff
changeset
|
116 |
|
876 | 117 |
\solution{for example $r^* = 1 + r \cdot r^*$ for any regular expression $r$. |
118 |
Can students think about why this is the case?} |
|
119 |
||
416 | 120 |
\item What is meant by the notions \emph{evil regular expressions} |
726 | 121 |
and by \emph{catastrophic backtracking}? |
122 |
||
876 | 123 |
\solution{catastrophic backtracking also applies to other regexes, not just $(a^*)^*b$} |
124 |
||
726 | 125 |
\item Given the regular expression $(a + b)^* \cdot b \cdot (a + b)^*$, |
841 | 126 |
which of the following regular expressions are equivalent |
726 | 127 |
|
128 |
\begin{center} |
|
129 |
\begin{tabular}{ll} |
|
130 |
1) & $(ab + bb)^* \cdot (a + b)^*$\\ % no |
|
131 |
2) & $(a + b)^* \cdot (ba + bb + b) \cdot (a + b)^*$\\ % yes |
|
132 |
3) & $(a + b)^* \cdot (a + b) \cdot (a + b)^*$ % no |
|
133 |
\end{tabular} |
|
134 |
\end{center} |
|
876 | 135 |
|
136 |
\solution{no, yes (why?), no.} |
|
726 | 137 |
|
403
564f7584eff1
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
401
diff
changeset
|
138 |
|
564f7584eff1
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
401
diff
changeset
|
139 |
\item \POSTSCRIPT |
0 | 140 |
\end{enumerate} |
141 |
||
142 |
\end{document} |
|
143 |
||
144 |
%%% Local Variables: |
|
145 |
%%% mode: latex |
|
146 |
%%% TeX-master: t |
|
147 |
%%% End: |