# HG changeset patch # User Christian Urban # Date 1667395379 0 # Node ID 126d0e47ac85c7f3690d171078dbbde93eb8f42a # Parent cdfa6a293453edcfea411d9e840a11abc20bc324 updated diff -r cdfa6a293453 -r 126d0e47ac85 core_templates1/collatz.jar Binary file core_templates1/collatz.jar has changed diff -r cdfa6a293453 -r 126d0e47ac85 core_templates1/collatz.scala --- a/core_templates1/collatz.scala Tue Nov 01 15:03:48 2022 +0000 +++ b/core_templates1/collatz.scala Wed Nov 02 13:22:59 2022 +0000 @@ -25,3 +25,8 @@ +// This template code is subject to copyright +// owned by King's College London. Do not +// make this template code public in any shape or +// form, and do not exchange it with other students +// under any circumstance. diff -r cdfa6a293453 -r 126d0e47ac85 cws/core_cw01.pdf Binary file cws/core_cw01.pdf has changed diff -r cdfa6a293453 -r 126d0e47ac85 cws/core_cw01.tex --- a/cws/core_cw01.tex Tue Nov 01 15:03:48 2022 +0000 +++ b/cws/core_cw01.tex Wed Nov 02 13:22:59 2022 +0000 @@ -101,7 +101,7 @@ problems.'' and also offered a \$500 cash prize for its solution. Jeffrey Lagarias, another mathematician, claimed that based only on known information about this problem, ``this is an extraordinarily -difficult problem, completely out of reach of present day mathematics.'' +difficult problem, completely out of reach of present-day mathematics.'' There is also a \href{https://xkcd.com/710/}{xkcd} cartoon about this conjecture\here{https://xkcd.com/710/}). If you are able to solve this conjecture, you will definitely get famous.}\bigskip @@ -153,7 +153,7 @@ \end{itemize} \noindent -\textbf{Test Data:} Some test ranges and cases are: +\textbf{Test Data:} Some test cases are: \begin{itemize} \item 1 to 10 where $9$ takes 19 steps diff -r cdfa6a293453 -r 126d0e47ac85 cws/main_cw05.pdf Binary file cws/main_cw05.pdf has changed diff -r cdfa6a293453 -r 126d0e47ac85 cws/main_cw05.tex --- a/cws/main_cw05.tex Tue Nov 01 15:03:48 2022 +0000 +++ b/cws/main_cw05.tex Wed Nov 02 13:22:59 2022 +0000 @@ -15,7 +15,7 @@ \begin{document} -\section*{Main Part 5 (Scala, 9 Marks)} +\section*{Main Part 5 (Scala, 10 Marks)} \mbox{}\hfill\textit{``If there's one feature that makes Scala, `Scala',}\\ \mbox{}\hfill\textit{ I would pick implicits.''}\smallskip\\ @@ -89,7 +89,7 @@ \newpage -\subsection*{Part A (5 Marks)} +\subsection*{Part A (6 Marks)} Coming from Java or C++, you might think Scala is a rather esoteric programming language. But remember, some serious companies have built @@ -109,7 +109,7 @@ language has been shown to be Turing complete\ldots{}if this doesn't ring any bell with you: it roughly means that every(!) algorithm can, in principle, be implemented in brainf***. It just takes a lot of -determination and quite a lot of memory resources. +determination and quite a lot of memory and time. Some relatively sophisticated sample programs in brainf*** are given in the file \texttt{bf.scala}, including a brainf*** program for the @@ -391,8 +391,26 @@ program counter \texttt{pc}, the memory pointer \texttt{mp} and the memory \texttt{mem}.\label{comms}} \end{figure} -\end{itemize}\bigskip + +\item[(5)] Let us also generate some BF programs ourselves: + Write a function + \texttt{generate} which takes a list of characters as input and generates + a corresponding BF program that prints out this list of characters. One + way to generate such a program is to consider each character in the list, + add as many \texttt{"+"} given by the ASCII code of the character, then add the + \texttt{"."} command for printing and add the loop \texttt{"[-]"} for ``zero-ing'' the memory + cell; then go to the next character in the list. For example + the list \texttt{"ABC".toString} produces (as a single string) + + \begin{center} + \texttt{+++++++++++++++++++++++++++++++++++++++++++++++++++++} + \texttt{++++++++++++.[-]+++++++++++++++++++++++++++++++++++++} + \texttt{+++++++++++++++++++++++++++++.[-]++++++++++++++++++++} + \texttt{+++++++++++++++++++++++++++++++++++++++++++++++.[-]} + \end{center}\mbox{}\hfill[1 Mark] + +\end{itemize}\bigskip %%\newpage \subsection*{Part B (4 Marks)} @@ -405,7 +423,7 @@ \subsubsection*{Tasks (file bfc.scala)} \begin{itemize} -\item[(5)] Compilers, in general, attempt to make programs run +\item[(6)] Compilers, in general, attempt to make programs run faster by precomputing as much information as possible before running the program. In our case we can precompute the addresses where we need to jump at the beginning and end of @@ -448,7 +466,7 @@ the function \texttt{jumpLeft} and \texttt{jumpRight} for calculating the \texttt{jtable}.\hfill{[1 Mark]} -\item[(6)] Compilers try to eliminate any ``dead'' code that could +\item[(7)] Compilers try to eliminate any ``dead'' code that could slow down programs and also perform what is often called \emph{peephole optimisations}.\footnote{\url{https://en.wikipedia.org/wiki/Peephole_optimization}} @@ -476,7 +494,7 @@ with new strings.\\ \mbox{}\hfill{[1 Mark]} -\item[(7)] Finally, real compilers try to take advantage of modern +\item[(8)] Finally, real compilers try to take advantage of modern CPUs which often provide complex operations in hardware that can combine many smaller instructions into a single faster instruction. diff -r cdfa6a293453 -r 126d0e47ac85 main_solution5/bf.jar Binary file main_solution5/bf.jar has changed diff -r cdfa6a293453 -r 126d0e47ac85 main_solution5/bf.scala --- a/main_solution5/bf.scala Tue Nov 01 15:03:48 2022 +0000 +++ b/main_solution5/bf.scala Wed Nov 02 13:22:59 2022 +0000 @@ -111,7 +111,12 @@ def run(prog: String, m: Mem = Map()) = compute(prog, 0, 0, m) +def generate(msg: List[Char]) : String = msg match { + case Nil => "" + case c::cs => s"${"+" * c.toInt}.[-]" ++ generate(cs) +} +//println(generate("Hello World\n".toList)) // some sample bf/bf++-programs collected from the Internet @@ -212,4 +217,7 @@ //M5a.run(M5a.load_bff("mandelbrot.bf")) -M5a.run(M5a.load_bff("collatz.bf")) +//M5a.run(M5a.load_bff("collatz.bf")) + +println(M5a.generate("ABC".toList)) +M5a.run(M5a.generate("Hello World\n".toList)) \ No newline at end of file diff -r cdfa6a293453 -r 126d0e47ac85 main_solution5/bfc.jar Binary file main_solution5/bfc.jar has changed diff -r cdfa6a293453 -r 126d0e47ac85 main_templates5/bf.jar Binary file main_templates5/bf.jar has changed diff -r cdfa6a293453 -r 126d0e47ac85 main_templates5/bf.scala --- a/main_templates5/bf.scala Tue Nov 01 15:03:48 2022 +0000 +++ b/main_templates5/bf.scala Wed Nov 02 13:22:59 2022 +0000 @@ -47,6 +47,9 @@ def run(prog: String, m: Mem = Map()) = ??? +// (5) +def generate(msg: List[Char]) : String = ??? + // some sample bf-programs collected from the Internet diff -r cdfa6a293453 -r 126d0e47ac85 main_templates5/bfc.jar Binary file main_templates5/bfc.jar has changed diff -r cdfa6a293453 -r 126d0e47ac85 main_templates5/bfc.scala --- a/main_templates5/bfc.scala Tue Nov 01 15:03:48 2022 +0000 +++ b/main_templates5/bfc.scala Wed Nov 02 13:22:59 2022 +0000 @@ -36,7 +36,7 @@ // ADD YOUR CODE BELOW //====================== -// (5) +// (6) def jtable(pg: String) : Map[Int, Int] = ??? // testcase @@ -54,7 +54,7 @@ -// (6) +// (7) def optimise(s: String) : String = ??? @@ -72,7 +72,7 @@ -// (7) +// (8) def combine(s: String) : String = ??? // testcase