diff --git a/previous-exams/2020-final-exam/q10.md b/previous-exams/2020-final-exam/q10.md new file mode 100644 index 0000000000000000000000000000000000000000..80ee295c48d14e2fc69a1e0a7723dc1c86a47347 --- /dev/null +++ b/previous-exams/2020-final-exam/q10.md @@ -0,0 +1,71 @@ +# Setup + +You can use the following commands to make a fresh clone of your repository: + +``` +git clone -b q10 git@gitlab.epfl.ch:lamp/students-repositories-fall-2021/cs210-GASPAR.git cs210-q10 +cd cs210-q10 +``` + +You can always refer to: + * [the example guide](https://gitlab.epfl.ch/lamp/cs210/blob/master/labs/example-lab.md) on the development workflow. + * [this guide](https://gitlab.epfl.ch/lamp/cs210/blob/master/labs/grading-and-submission.md) for details on the submission system. + * [The documentation of the Scala standard library](https://www.scala-lang.org/files/archive/api/2.13.3) + * [The documentation of the Java standard + library](https://docs.oracle.com/en/java/javase/15/docs/api/index.html) + +**If you have issues with the IDE, try [reimporting the build](https://gitlab.epfl.ch/lamp/cs210/-/blob/master/labs/example-lab.md#ide-features-like-type-on-hover-or-go-to-definition-do-not-work), if you still have problems, use `compile` in sbt instead.** + +# Be functional! + +This course is about **functional** programming, therefore you're not allowed to use the following +constructs in this assignment: +- `var` +- `while` +- `return` +- Any class in the `scala.collection.mutable` package + +# Exercise + +You are given the method `fImperative`, which from a given list of tuples generates a list of integers, such that each tuple in the input list is transformed into several consecutive integer elements. The number and values of groups of consecutive elements are determined by the first and second element of the corresponding tuple, respectively. In cases where the specified number of consecutive elements is less than zero, it is considered to be zero. + +For example, for the given input list: `((-1,5),(2,7),(1,2))` the output list should be: `(7,7,2)`. + + +```scala + def fImperative(elems: List[(Int, Int)]): List[Int] = { + var i = 0 + var res: List[Int] = List() + + while (i < elems.size) { + var j = 0 + var cnt = l(i)._1 + while (j < cnt) { + res = elems(i)._2 :: res + j = j + 1 + } + i = i + 1 + } + res.reverse + } +``` + +This solution uses var-s and while loops so it is not in the spirit of functional programming. Your task is to write a new implementation of this method in a purely functional way, using pattern matching and a limited set of existing list methods given in the appendix. Your solution should produce the same result as the given imperative method, but without imperative constructs such as var and while. You may define and implement additional methods. Correct solutions will be given a full score even if they are not tail-recursive. + +In this exercise you are only allowed to use existing list methods which are listed in the appendix. + +`def f(elems: List[(Int, Int)]): List[Int] = ???` + +# Appendix + +Here are the methods that you are allowed to use on `List[A]`: + +`xs.head: A`: returns the first element of the list. Throws an exception if the list is empty. + +`xs.tail: List[A]`: returns the list `xs` without its first element. Throws an exception if the list is empty. + +`xs.isEmpty: Boolean`: returns `true` if the list does not contain any elements, `false` otherwise. + +`x :: (xs: List[A]): List[A]`: prepends the element `x` to the left of `xs`, returning a `List[A]`. + +`xs.reverse: List[A]`: reverses the elements of the list `xs`. \ No newline at end of file diff --git a/previous-exams/2020-final-exam/q16.md b/previous-exams/2020-final-exam/q16.md new file mode 100644 index 0000000000000000000000000000000000000000..1e85b74136d6a5e7de3a442268f6e570da5453a7 --- /dev/null +++ b/previous-exams/2020-final-exam/q16.md @@ -0,0 +1,48 @@ +# Setup + +You can use the following commands to make a fresh clone of your repository: + +``` +git clone -b q16 git@gitlab.epfl.ch:lamp/students-repositories-fall-2021/cs210-GASPAR.git cs210-q16 +cd cs210-q16 +``` + +You can always refer to: + * [the example guide](https://gitlab.epfl.ch/lamp/cs210/blob/master/labs/example-lab.md) on the development workflow. + * [this guide](https://gitlab.epfl.ch/lamp/cs210/blob/master/labs/grading-and-submission.md) for details on the submission system. + * [The documentation of the Scala standard library](https://www.scala-lang.org/files/archive/api/2.13.3) + * [The documentation of the Java standard + library](https://docs.oracle.com/en/java/javase/15/docs/api/index.html) + +**If you have issues with the IDE, try [reimporting the build](https://gitlab.epfl.ch/lamp/cs210/-/blob/master/labs/example-lab.md#ide-features-like-type-on-hover-or-go-to-definition-do-not-work), if you still have problems, use `compile` in sbt instead.** + +# Be functional! + +This course is about **functional** programming, therefore you're not allowed to use the following +constructs in this assignment: +- `var` +- `while` +- `return` +- Any class in the `scala.collection.mutable` package + +# Exercise 16: For-comprehensions + +You are given two classes (`Library` and `Book` which are defined below) and the method `f`, which from a given list of libraries generates a list of Scala book titles with their edition and location. Books might be in more than one location. Two editions of a book are considered distinct. + + +```scala +case class Library(books: List[Book], location: String) +case class Book(title: String, publisher: String, editions: List[Int]) + +def f(libraries: List[Library]): List[(String, Int, String)] = { + for { + library <- libraries + book <- library.books + if book.title.contains("Scala") + if !book.title.contains("La Scala") // List books about `Scala` but not books about `La Scala` opera + edition <- book.editions + } yield (book.title, edition, library.location) +} +``` + +Your task is to rewrite the method `f` to use `map`, `flatMap` and `withFilter` instead of the for-comprehension. The resulting method should of course have the same result as the for-comprehension above. diff --git a/previous-exams/2020-final-exam/q17.md b/previous-exams/2020-final-exam/q17.md new file mode 100644 index 0000000000000000000000000000000000000000..6c7b954133f97da6450d6e28c16ac368dd62f241 --- /dev/null +++ b/previous-exams/2020-final-exam/q17.md @@ -0,0 +1,48 @@ +# Setup + +You can use the following commands to make a fresh clone of your repository: + +``` +git clone -b q17 git@gitlab.epfl.ch:lamp/students-repositories-fall-2021/cs210-GASPAR.git cs210-q17 +cd cs210-q17 +``` + +You can always refer to: + * [the example guide](https://gitlab.epfl.ch/lamp/cs210/blob/master/labs/example-lab.md) on the development workflow. + * [this guide](https://gitlab.epfl.ch/lamp/cs210/blob/master/labs/grading-and-submission.md) for details on the submission system. + * [The documentation of the Scala standard library](https://www.scala-lang.org/files/archive/api/2.13.3) + * [The documentation of the Java standard + library](https://docs.oracle.com/en/java/javase/15/docs/api/index.html) + +**If you have issues with the IDE, try [reimporting the build](https://gitlab.epfl.ch/lamp/cs210/-/blob/master/labs/example-lab.md#ide-features-like-type-on-hover-or-go-to-definition-do-not-work), if you still have problems, use `compile` in sbt instead.** + +# Be functional! + +This course is about **functional** programming, therefore you're not allowed to use the following +constructs in this assignment: +- `var` +- `while` +- `return` +- Any class in the `scala.collection.mutable` package + +# Exercise 17: For-comprehensions + +You are given two classes (`Library` and `Book` which are defined below) and the method `f`, which from a given list of libraries and a list of publishers generates a list with the book count of each publisher in each library. Book count results are a triple containing the publisher, the library location, and the number of books. If a library does not contain books from a publisher then the count is not included in the result. + + +```scala +case class Library(books: List[Book], location: String) +case class Book(title: String, authors: List[String], publisher: String, year: Int) + +def f(libraries: List[Library], publishers: List[String]): List[(String, String, Int)] = { + for { + publisher <- publishers + library <- libraries + if numBooksPublishedBy(library.books, publisher) != 0 // Don't list if there are no books in the library + } yield (publisher, library.location, numBooksPublishedBy(library.books, publisher)) +} + +def numBooksPublishedBy(books: List[Book], publisher: String): Int = ... +``` + +Your task is to rewrite the method `f` to use `map`, `flatMap` and `withFilter` instead of the for-comprehension. The resulting method should of course have the same result as the for-comprehension above. diff --git a/previous-exams/2020-final-exam/q18.md b/previous-exams/2020-final-exam/q18.md new file mode 100644 index 0000000000000000000000000000000000000000..4e9747648221ed5fc52e406fea78905a6cd8f636 --- /dev/null +++ b/previous-exams/2020-final-exam/q18.md @@ -0,0 +1,46 @@ +# Setup + +You can use the following commands to make a fresh clone of your repository: + +``` +git clone -b q18 git@gitlab.epfl.ch:lamp/students-repositories-fall-2021/cs210-GASPAR.git cs210-q18 +cd cs210-q18 +``` + +You can always refer to: + * [the example guide](https://gitlab.epfl.ch/lamp/cs210/blob/master/labs/example-lab.md) on the development workflow. + * [this guide](https://gitlab.epfl.ch/lamp/cs210/blob/master/labs/grading-and-submission.md) for details on the submission system. + * [The documentation of the Scala standard library](https://www.scala-lang.org/files/archive/api/2.13.3) + * [The documentation of the Java standard + library](https://docs.oracle.com/en/java/javase/15/docs/api/index.html) + +**If you have issues with the IDE, try [reimporting the build](https://gitlab.epfl.ch/lamp/cs210/-/blob/master/labs/example-lab.md#ide-features-like-type-on-hover-or-go-to-definition-do-not-work), if you still have problems, use `compile` in sbt instead.** + +# Be functional! + +This course is about **functional** programming, therefore you're not allowed to use the following +constructs in this assignment: +- `var` +- `while` +- `return` +- Any class in the `scala.collection.mutable` package + +# Exercise 18: For-comprehensions + +You are given the class `Book` (which is defined below) and the method `f`, which from a given list of books, a predicate on years and a predicate on authors will return tuples with the author and name of the books for which the predicates hold. + + +```scala +case class Book(title: String, authors: List[String], year: Int) + +def f(books: List[Book], yearPred: Int => Boolean, authorPred: String => Boolean): List[(String, String)] = { + for { + book <- books + if yearPred(book.year) + author <- book.authors + if authorPred(author) + } yield (author, book.title) +} +``` + +Your task is to rewrite the method `f` to use `map`, `flatMap` and `withFilter` instead of the for-comprehension. The resulting method should of course have the same result as the for-comprehension above. diff --git a/previous-exams/2020-final-exam/q19.md b/previous-exams/2020-final-exam/q19.md new file mode 100644 index 0000000000000000000000000000000000000000..6dc1f3873369b3f6c9202acfe62facf4c4032b78 --- /dev/null +++ b/previous-exams/2020-final-exam/q19.md @@ -0,0 +1,51 @@ +# Setup + +You can use the following commands to make a fresh clone of your repository: + +``` +git clone -b q19 git@gitlab.epfl.ch:lamp/students-repositories-fall-2021/cs210-GASPAR.git cs210-q19 +cd cs210-q19 +``` + +You can always refer to: + * [the example guide](https://gitlab.epfl.ch/lamp/cs210/blob/master/labs/example-lab.md) on the development workflow. + * [this guide](https://gitlab.epfl.ch/lamp/cs210/blob/master/labs/grading-and-submission.md) for details on the submission system. + * [The documentation of the Scala standard library](https://www.scala-lang.org/files/archive/api/2.13.3) + * [The documentation of the Java standard + library](https://docs.oracle.com/en/java/javase/15/docs/api/index.html) + +**If you have issues with the IDE, try [reimporting the build](https://gitlab.epfl.ch/lamp/cs210/-/blob/master/labs/example-lab.md#ide-features-like-type-on-hover-or-go-to-definition-do-not-work), if you still have problems, use `compile` in sbt instead.** + +# Be functional! + +This course is about **functional** programming, therefore you're not allowed to use the following +constructs in this assignment: +- `var` +- `while` +- `return` +- Any class in the `scala.collection.mutable` package + +# Exercise + +You are given four classes (`Client`, `Purchase`, `Store` and `DataBase` which are defined below) and the method `f`. The method `f` lists all the purchases location and amount performed by a client with name `clientName`. + +```scala +case class Client(id: Int, name: String) +case class Purchase(clientId: Int, storeId: Int, amount: Double) +case class Store(id: Int, location: String) + +case class DataBase(clients: List[Client], purchases: List[Purchase], stores: List[Store]) + +def purchasesOf(db: DataBase, clientName: String): List[(String, Double)] = { + for { + client <- db.clients + if client.name == clientName + purchase <- db.purchases + if purchase.clientId == client.id + store <- db.stores + if store.id == purchase.storeId + } yield (store.location, purchase.amount) +} +``` + +Your task is to rewrite the method `f` to use `map`, `flatMap` and `withFilter` instead of the for-comprehension. The resulting method should of course have the same result as the for-comprehension above. diff --git a/previous-exams/2020-final-exam/q20.md b/previous-exams/2020-final-exam/q20.md new file mode 100644 index 0000000000000000000000000000000000000000..583c7d3cf9c5dad8fe2467a16493de946ef200e6 --- /dev/null +++ b/previous-exams/2020-final-exam/q20.md @@ -0,0 +1,49 @@ +# Setup + +You can use the following commands to make a fresh clone of your repository: + +``` +git clone -b q20 git@gitlab.epfl.ch:lamp/students-repositories-fall-2021/cs210-GASPAR.git cs210-q20 +cd cs210-q20 +``` + +You can always refer to: + * [the example guide](https://gitlab.epfl.ch/lamp/cs210/blob/master/labs/example-lab.md) on the development workflow. + * [this guide](https://gitlab.epfl.ch/lamp/cs210/blob/master/labs/grading-and-submission.md) for details on the submission system. + * [The documentation of the Scala standard library](https://www.scala-lang.org/files/archive/api/2.13.3) + * [The documentation of the Java standard + library](https://docs.oracle.com/en/java/javase/15/docs/api/index.html) + +**If you have issues with the IDE, try [reimporting the build](https://gitlab.epfl.ch/lamp/cs210/-/blob/master/labs/example-lab.md#ide-features-like-type-on-hover-or-go-to-definition-do-not-work), if you still have problems, use `compile` in sbt instead.** + +# Be functional! + +This course is about **functional** programming, therefore you're not allowed to use the following +constructs in this assignment: +- `var` +- `while` +- `return` +- Any class in the `scala.collection.mutable` package + +# Exercise + +You are given three classes (`Client`, `Purchase` and `DataBase` which are defined below) and the method `f`. The method `f` lists all the purchases client's ages and amount. Purchases for clients with no age set in the database are ignored. + + +```scala +case class Client(id: Int, name: String, age: Option[Int]) +case class Purchase(clientId: Int, amount: Double) + +case class DataBase(clients: List[Client], purchases: List[Purchase]) + +def purchasesClientAgeAndAmounts(db: DataBase): List[(Int, Double)] = { + for { + client <- db.clients + age <- client.age.toList + purchase <- db.purchases + if purchase.clientId == client.id + } yield (age, purchase.amount) +} +``` + +Your task is to rewrite the method `f` to use `map`, `flatMap` and `withFilter` instead of the for-comprehension. The resulting method should of course have the same result as the for-comprehension above. diff --git a/previous-exams/2020-final-exam/q21.md b/previous-exams/2020-final-exam/q21.md new file mode 100644 index 0000000000000000000000000000000000000000..9c3749ef7d7b93f80f5d585f0cbdeeb9ead920c4 --- /dev/null +++ b/previous-exams/2020-final-exam/q21.md @@ -0,0 +1,38 @@ +# Setup + +You can use the following commands to make a fresh clone of your repository: + +``` +git clone -b q21 git@gitlab.epfl.ch:lamp/students-repositories-fall-2021/cs210-GASPAR.git cs210-q21 +cd cs210-q21 +``` + +You can always refer to: + * [the example guide](https://gitlab.epfl.ch/lamp/cs210/blob/master/labs/example-lab.md) on the development workflow. + * [this guide](https://gitlab.epfl.ch/lamp/cs210/blob/master/labs/grading-and-submission.md) for details on the submission system. + * [The documentation of the Scala standard library](https://www.scala-lang.org/files/archive/api/2.13.3) + * [The documentation of the Java standard + library](https://docs.oracle.com/en/java/javase/15/docs/api/index.html) + +**If you have issues with the IDE, try [reimporting the build](https://gitlab.epfl.ch/lamp/cs210/-/blob/master/labs/example-lab.md#ide-features-like-type-on-hover-or-go-to-definition-do-not-work), if you still have problems, use `compile` in sbt instead.** + +# Be functional! + +This course is about **functional** programming, therefore you're not allowed to use the following +constructs in this assignment: +- `var` +- `while` +- `return` +- Any class in the `scala.collection.mutable` package + +# Exercise + +Define the sequence of all the squares written backwards: + +``` +1, 4, 9, 61, 52, 63, 94, 46, 18, 1, 121, 441, 961, 691, 522, 652, 982, 423, 163, 4, 144, 484, 925, 675, 526, ... +``` + +This sequence is infinite and must be implemented as a lazy list of integers. + +For the purpose of this exercise, you should ignore the limitations of 32-bit integers. diff --git a/previous-exams/2020-final-exam/q22.md b/previous-exams/2020-final-exam/q22.md new file mode 100644 index 0000000000000000000000000000000000000000..93403c0b7063257005f1f166360384d3648cd602 --- /dev/null +++ b/previous-exams/2020-final-exam/q22.md @@ -0,0 +1,46 @@ +# Setup + +You can use the following commands to make a fresh clone of your repository: + +``` +git clone -b q22 git@gitlab.epfl.ch:lamp/students-repositories-fall-2021/cs210-GASPAR.git cs210-q22 +cd cs210-q22 +``` + +You can always refer to: + * [the example guide](https://gitlab.epfl.ch/lamp/cs210/blob/master/labs/example-lab.md) on the development workflow. + * [this guide](https://gitlab.epfl.ch/lamp/cs210/blob/master/labs/grading-and-submission.md) for details on the submission system. + * [The documentation of the Scala standard library](https://www.scala-lang.org/files/archive/api/2.13.3) + * [The documentation of the Java standard + library](https://docs.oracle.com/en/java/javase/15/docs/api/index.html) + +**If you have issues with the IDE, try [reimporting the build](https://gitlab.epfl.ch/lamp/cs210/-/blob/master/labs/example-lab.md#ide-features-like-type-on-hover-or-go-to-definition-do-not-work), if you still have problems, use `compile` in sbt instead.** + +# Be functional! + +This course is about **functional** programming, therefore you're not allowed to use the following +constructs in this assignment: +- `var` +- `while` +- `return` +- Any class in the `scala.collection.mutable` package + +# Exercise + +Define the sequence of all numbers that read the same upside down (180° rotation) on a seven-segment display: + +``` +0, 1, 2, 5, 8, 11, 22, 55, 69, 88, 96, 101, 111, 121, 151, 181, 202, 212, 222, 252, 282, 505, 515, 525, 555, ... +``` + +For reference, digits on a seven-segment display are rendered as follows: + +``` + _ _ _ _ _ _ _ _ +| | | _| _| |_| |_ |_ | |_| |_| +|_| | |_ _| | _| |_| | |_| _| +``` + +This sequence is infinite and must be implemented as a lazy list of integers. + +For the purpose of this exercise, you should ignore the limitations of 32-bit integers. diff --git a/previous-exams/2020-final-exam/q23.md b/previous-exams/2020-final-exam/q23.md new file mode 100644 index 0000000000000000000000000000000000000000..1be3b4fdd4d3ecee5e1bcfb6d059fda441471097 --- /dev/null +++ b/previous-exams/2020-final-exam/q23.md @@ -0,0 +1,38 @@ +# Setup + +You can use the following commands to make a fresh clone of your repository: + +``` +git clone -b q23 git@gitlab.epfl.ch:lamp/students-repositories-fall-2021/cs210-GASPAR.git cs210-q23 +cd cs210-q23 +``` + +You can always refer to: + * [the example guide](https://gitlab.epfl.ch/lamp/cs210/blob/master/labs/example-lab.md) on the development workflow. + * [this guide](https://gitlab.epfl.ch/lamp/cs210/blob/master/labs/grading-and-submission.md) for details on the submission system. + * [The documentation of the Scala standard library](https://www.scala-lang.org/files/archive/api/2.13.3) + * [The documentation of the Java standard + library](https://docs.oracle.com/en/java/javase/15/docs/api/index.html) + +**If you have issues with the IDE, try [reimporting the build](https://gitlab.epfl.ch/lamp/cs210/-/blob/master/labs/example-lab.md#ide-features-like-type-on-hover-or-go-to-definition-do-not-work), if you still have problems, use `compile` in sbt instead.** + +# Be functional! + +This course is about **functional** programming, therefore you're not allowed to use the following +constructs in this assignment: +- `var` +- `while` +- `return` +- Any class in the `scala.collection.mutable` package + +# Exercise + +Define the sequence of all numbers divisible by 7 that read the same backward as forward: + +``` +7, 77, 161, 252, 343, 434, 525, 595, 616, 686, 707, 777, 868, 959, 1001, 1771, 2002, 2772, 3003, 3773, 4004, 4774, 5005, 5775, 6006, ... +``` + +This sequence is infinite and must be implemented as a lazy list of integers. + +For the purpose of this exercise, you should ignore the limitations of 32-bit integers. diff --git a/previous-exams/2020-final-exam/q24.md b/previous-exams/2020-final-exam/q24.md new file mode 100644 index 0000000000000000000000000000000000000000..c1a7a9e5b4c0faf25cbaacc7ec76dfef0318b0dd --- /dev/null +++ b/previous-exams/2020-final-exam/q24.md @@ -0,0 +1,48 @@ +# Setup + +You can use the following commands to make a fresh clone of your repository: + +``` +git clone -b q24 git@gitlab.epfl.ch:lamp/students-repositories-fall-2021/cs210-GASPAR.git cs210-q24 +cd cs210-q24 +``` + +You can always refer to: + * [the example guide](https://gitlab.epfl.ch/lamp/cs210/blob/master/labs/example-lab.md) on the development workflow. + * [this guide](https://gitlab.epfl.ch/lamp/cs210/blob/master/labs/grading-and-submission.md) for details on the submission system. + * [The documentation of the Scala standard library](https://www.scala-lang.org/files/archive/api/2.13.3) + * [The documentation of the Java standard + library](https://docs.oracle.com/en/java/javase/15/docs/api/index.html) + +**If you have issues with the IDE, try [reimporting the build](https://gitlab.epfl.ch/lamp/cs210/-/blob/master/labs/example-lab.md#ide-features-like-type-on-hover-or-go-to-definition-do-not-work), if you still have problems, use `compile` in sbt instead.** + +# Be functional! + +This course is about **functional** programming, therefore you're not allowed to use the following +constructs in this assignment: +- `var` +- `while` +- `return` +- Any class in the `scala.collection.mutable` package + +# Exercise + +Pell numbers are defined by the following equations: + +``` +a(0) = 0 +a(1) = 1 +a(n) = 2 * a(n - 1) + a(n - 2) (for n > 1) +``` + +Here are the first 25 Pell numbers: + +``` +0, 1, 2, 5, 12, 29, 70, 169, 408, 985, 2378, 5741, 13860, 33461, 80782, 195025, 470832, 1136689, 2744210, 6625109, 15994428, 38613965, 93222358, 225058681, 543339720 +``` + +Implement this sequence as an infinite lazy list of integers. + +There are many ways to correctly solve this problem, however some implementations run in an exponential time, so be careful, an inefficient implementation might result in a timeout during the grading process. + +For the purpose of this exercise, you should ignore the limitations of 32-bit integers. diff --git a/previous-exams/2020-final-exam/q25.md b/previous-exams/2020-final-exam/q25.md new file mode 100644 index 0000000000000000000000000000000000000000..0d330d68f1d5479d13bbeda576d194abe08cece7 --- /dev/null +++ b/previous-exams/2020-final-exam/q25.md @@ -0,0 +1,66 @@ +# Setup + +You can use the following commands to make a fresh clone of your repository: + +``` +git clone -b q25 git@gitlab.epfl.ch:lamp/students-repositories-fall-2021/cs210-GASPAR.git cs210-q25 +cd cs210-q25 +``` + +You can always refer to: + * [the example guide](https://gitlab.epfl.ch/lamp/cs210/blob/master/labs/example-lab.md) on the development workflow. + * [this guide](https://gitlab.epfl.ch/lamp/cs210/blob/master/labs/grading-and-submission.md) for details on the submission system. + * [The documentation of the Scala standard library](https://www.scala-lang.org/files/archive/api/2.13.3) + * [The documentation of the Java standard + library](https://docs.oracle.com/en/java/javase/15/docs/api/index.html) + +**If you have issues with the IDE, try [reimporting the build](https://gitlab.epfl.ch/lamp/cs210/-/blob/master/labs/example-lab.md#ide-features-like-type-on-hover-or-go-to-definition-do-not-work), if you still have problems, use `compile` in sbt instead.** + +# Be functional! + +This course is about **functional** programming, therefore you're not allowed to use the following +constructs in this assignment: +- `var` +- `while` +- `return` +- Any class in the `scala.collection.mutable` package + +# Exercise + +Consider the infinite integer sequences `a(n)` defined by the following equations (for a given initial positive integer `k`): + +``` +a(0) = k +a(n+1) = a(n) / 2 if a(n) is even (for n >= 0) +a(n+1) = 3 * a(n) + 1 if a(n) is odd (for n >= 0) +``` + +Here are the first 16 numbers of that sequence for `k = 10`: + +``` +10, 5, 16, 8, 4, 2, 1, 4, 2, 1, 4, 2, 1, 4, 2, 1, ... +``` + +For `k = 11`: + +``` +11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1, 4, ... +``` + +And for `k = 12`: + +``` +12, 6, 3, 10, 5, 16, 8, 4, 2, 1, 4, 2, 1, 4, 2, 1, ... +``` + +As you can see, these sequences all converge to `1` and then loop with the same pattern (`1, 4, 2, 1,`). + +There is a famous mathematical conjecture stating that for any initial positive integer `k` the resulting sequence always converges to `1`. + +In this exercise, your task is to write a lazy list implementation of these integer sequences. Your sequences should stop when the first `1` is reached, that is: + +- `(k = 10) 10, 5, 16, 8, 4, 2, 1` +- `(k = 11) 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1` +- `(k = 12) 12, 6, 3, 10, 5, 16, 8, 4, 2, 1` + +For the purpose of this exercise, you should ignore the limitations of 32-bit integers. diff --git a/previous-exams/2020-final-exam/q6.md b/previous-exams/2020-final-exam/q6.md new file mode 100644 index 0000000000000000000000000000000000000000..9adf44af71c0b04ff341f130493bfe854bf4f6a0 --- /dev/null +++ b/previous-exams/2020-final-exam/q6.md @@ -0,0 +1,82 @@ +# Setup + +You can use the following commands to make a fresh clone of your repository: + +``` +git clone -b q6 git@gitlab.epfl.ch:lamp/students-repositories-fall-2021/cs210-GASPAR.git cs210-q6 +cd cs210-q6 +``` + +You can always refer to: + * [the example guide](https://gitlab.epfl.ch/lamp/cs210/blob/master/labs/example-lab.md) on the development workflow. + * [this guide](https://gitlab.epfl.ch/lamp/cs210/blob/master/labs/grading-and-submission.md) for details on the submission system. + * [The documentation of the Scala standard library](https://www.scala-lang.org/files/archive/api/2.13.3) + * [The documentation of the Java standard + library](https://docs.oracle.com/en/java/javase/15/docs/api/index.html) + +**If you have issues with the IDE, try [reimporting the build](https://gitlab.epfl.ch/lamp/cs210/-/blob/master/labs/example-lab.md#ide-features-like-type-on-hover-or-go-to-definition-do-not-work), if you still have problems, use `compile` in sbt instead.** + +# Be functional! + +This course is about **functional** programming, therefore you're not allowed to use the following +constructs in this assignment: +- `var` +- `while` +- `return` +- Any class in the `scala.collection.mutable` package + +# Exercise + +You are given the method `fImperative`, which from a given list of characters, representing a word, generates a list with one letter removed, such that the resulting word is lexicographically largest among all the options. + +For example, for the given list of characters: `('d', 'b', 'c')` there are three possible results after removing one letter: `(b', 'c')`, `('d', 'c')` and `('d', 'b')`. Word `('d', 'c')` is lexicographically largest among the options, so this is the result of our method for this input example. + +```scala + def fImperative(chars: List[Char]): List[Char] = { + var i = 0 + var n = chars.size + var res: List[Char] = List() + + while (i < n - 1 && chars(i) >= chars(i + 1)) { + res = chars(i) :: res + i = i + 1 + } + + i = i + 1 + + while (i < n) { + res = chars(i) :: res + i = i + 1 + } + + res.reverse + } +``` + +This solution uses var-s and while loops so it is not in the spirit of functional programming. Your task is to write a new implementation of this method in a purely functional way, using pattern matching and a limited set of existing list methods given in the appendix. Your solution should produce the same result as the given imperative method, but without imperative constructs such as var and while. You may define and implement additional methods. Correct solutions will be given a full score even if they are not tail-recursive. + +In this exercise you are only allowed to use existing list methods which are listed in the appendix. + +`def f(chars: List[Char]): List[Char] = ???` + +# Appendix + +Here are the methods that you are allowed to use on `List[A]`: + +`xs.head: A`: returns the first element of the list. Throws an exception if the list is empty. + +`xs.tail: List[A]`: returns the list `xs` without its first element. Throws an exception if the list is empty. + +`xs.isEmpty: Boolean`: returns `true` if the list does not contain any elements, `false` otherwise. + +`x :: (xs: List[A]): List[A]`: prepends the element `x` to the left of `xs`, returning a `List[A]`. + +`xs.reverse: List[A]`: reverses the elements of the list `xs`. + + + + + + + + diff --git a/previous-exams/2020-final-exam/q7.md b/previous-exams/2020-final-exam/q7.md new file mode 100644 index 0000000000000000000000000000000000000000..a37cbaad7df1ddae2d864a75cc485dd644ac04f2 --- /dev/null +++ b/previous-exams/2020-final-exam/q7.md @@ -0,0 +1,72 @@ +# Setup + +You can use the following commands to make a fresh clone of your repository: + +``` +git clone -b q7 git@gitlab.epfl.ch:lamp/students-repositories-fall-2021/cs210-GASPAR.git cs210-q7 +cd cs210-q7 +``` + +You can always refer to: + * [the example guide](https://gitlab.epfl.ch/lamp/cs210/blob/master/labs/example-lab.md) on the development workflow. + * [this guide](https://gitlab.epfl.ch/lamp/cs210/blob/master/labs/grading-and-submission.md) for details on the submission system. + * [The documentation of the Scala standard library](https://www.scala-lang.org/files/archive/api/2.13.3) + * [The documentation of the Java standard + library](https://docs.oracle.com/en/java/javase/15/docs/api/index.html) + +**If you have issues with the IDE, try [reimporting the build](https://gitlab.epfl.ch/lamp/cs210/-/blob/master/labs/example-lab.md#ide-features-like-type-on-hover-or-go-to-definition-do-not-work), if you still have problems, use `compile` in sbt instead.** + +# Be functional! + +This course is about **functional** programming, therefore you're not allowed to use the following +constructs in this assignment: +- `var` +- `while` +- `return` +- Any class in the `scala.collection.mutable` package + +# Exercise + +You are given the method `fImperative`, which from a given list of integers generates a list of tuples, such that each tuple contains number and value of equal consecutive elements of the input list. + +For example, for the given input list: `(2,2,2,7,7,2)` the output list should be: `((3,2),(2,7),(1,2))`. + + +```scala + def fImperative(nums: List[Int]): List[(Int, Int)] = { + var cnt = 1 + var i = 0 + var res: List[(Int, Int)] = List() + + while (i < nums.length - 1) { + if (nums(i) == nums(i + 1)) cnt = cnt + 1 + else { + res = (cnt, nums(i)) :: res + cnt = 1 + } + i = i + 1 + } + if(nums.isEmpty) Nil + else ((cnt, nums(nums.length - 1)) :: res).reverse + } +``` + +This solution uses var-s and while loops so it is not in the spirit of functional programming. Your task is to write a new implementation of this method in a purely functional way, using pattern matching and a limited set of existing list methods given in the appendix. Your solution should produce the same result as the given imperative method, but without imperative constructs such as var and while. You may define and implement additional methods. Correct solutions will be given a full score even if they are not tail-recursive. + +In this exercise you are only allowed to use existing list methods which are listed in the appendix. + +`def f(l: List[Int]): List[(Int, Int)] = ???` + +# Appendix + +Here are the methods that you are allowed to use on `List[A]`: + +`xs.head: A`: returns the first element of the list. Throws an exception if the list is empty. + +`xs.tail: List[A]`: returns the list `xs` without its first element. Throws an exception if the list is empty. + +`xs.isEmpty: Boolean`: returns `true` if the list does not contain any elements, `false` otherwise. + +`x :: (xs: List[A]): List[A]`: prepends the element `x` to the left of `xs`, returning a `List[A]`. + +`xs.reverse: List[A]`: reverses the elements of the list `xs`. diff --git a/previous-exams/2020-final-exam/q8.md b/previous-exams/2020-final-exam/q8.md new file mode 100644 index 0000000000000000000000000000000000000000..40d77f16ca8587f21fc16edcb5404224aff7c5f7 --- /dev/null +++ b/previous-exams/2020-final-exam/q8.md @@ -0,0 +1,76 @@ +# Setup + +You can use the following commands to make a fresh clone of your repository: + +``` +git clone -b q8 git@gitlab.epfl.ch:lamp/students-repositories-fall-2021/cs210-GASPAR.git cs210-q8 +cd cs210-q8 +``` + +You can always refer to: + * [the example guide](https://gitlab.epfl.ch/lamp/cs210/blob/master/labs/example-lab.md) on the development workflow. + * [this guide](https://gitlab.epfl.ch/lamp/cs210/blob/master/labs/grading-and-submission.md) for details on the submission system. + * [The documentation of the Scala standard library](https://www.scala-lang.org/files/archive/api/2.13.3) + * [The documentation of the Java standard + library](https://docs.oracle.com/en/java/javase/15/docs/api/index.html) + +**If you have issues with the IDE, try [reimporting the build](https://gitlab.epfl.ch/lamp/cs210/-/blob/master/labs/example-lab.md#ide-features-like-type-on-hover-or-go-to-definition-do-not-work), if you still have problems, use `compile` in sbt instead.** + +# Be functional! + +This course is about **functional** programming, therefore you're not allowed to use the following +constructs in this assignment: +- `var` +- `while` +- `return` +- Any class in the `scala.collection.mutable` package + +# Exercise + +You are given the method `fImperative`, which from a given list of integers, which represents heights of people standing in a bank queue, generates a list of the same size, such that the value of each element is the number of people visible to the corresponding person in the input list. A person is considered visible to another person if they are in front of them in the queue, and there is no taller (or equally tall) person in between. You can assume that all the elements of the input list are natural numbers. + +For example, for the given input list: `(182, 160, 180, 178)` the output list should be `(0, 1, 2, 2)`. + +```scala + def fImperative(nums: List[Int]): List[Int] = { + var i = 0 + var j = 0 + var res: List[Int] = List() + + while (i < nums.size) { + var max = -1 + var cnt = 0 + j = i - 1 + while (j >= 0) { + if(nums(j) > max) { + cnt = cnt + 1 + max = nums(j) + } + j = j - 1 + } + res = cnt :: res + i = i + 1 + } + res.reverse + } +``` + +This solution uses var-s and while loops so it is not in the spirit of functional programming. Your task is to write a new implementation of this method in a purely functional way, using pattern matching and a limited set of existing list methods given in the appendix. Your solution should produce the same result as the given imperative method, but without imperative constructs such as var and while. You may define and implement additional methods. Correct solutions will be given a full score even if they are not tail-recursive. + +In this exercise you are only allowed to use existing list methods which are listed in the appendix. + +`def f(nums: List[Int]): List[Int] = ???` + +# Appendix + +Here are the methods that you are allowed to use on `List[A]`: + +`xs.head: A`: returns the first element of the list. Throws an exception if the list is empty. + +`xs.tail: List[A]`: returns the list `xs` without its first element. Throws an exception if the list is empty. + +`xs.isEmpty: Boolean`: returns `true` if the list does not contain any elements, `false` otherwise. + +`x :: (xs: List[A]): List[A]`: prepends the element `x` to the left of `xs`, returning a `List[A]`. + +`xs.reverse: List[A]`: reverses the elements of the list `xs`. diff --git a/previous-exams/2020-final-exam/q9.md b/previous-exams/2020-final-exam/q9.md new file mode 100644 index 0000000000000000000000000000000000000000..a953dee248f11c4bb5af288615e106d31114a205 --- /dev/null +++ b/previous-exams/2020-final-exam/q9.md @@ -0,0 +1,72 @@ +# Setup + +You can use the following commands to make a fresh clone of your repository: + +``` +git clone -b q9 git@gitlab.epfl.ch:lamp/students-repositories-fall-2021/cs210-GASPAR.git cs210-q9 +cd cs210-q9 +``` + +You can always refer to: + * [the example guide](https://gitlab.epfl.ch/lamp/cs210/blob/master/labs/example-lab.md) on the development workflow. + * [this guide](https://gitlab.epfl.ch/lamp/cs210/blob/master/labs/grading-and-submission.md) for details on the submission system. + * [The documentation of the Scala standard library](https://www.scala-lang.org/files/archive/api/2.13.3) + * [The documentation of the Java standard + library](https://docs.oracle.com/en/java/javase/15/docs/api/index.html) + +**If you have issues with the IDE, try [reimporting the build](https://gitlab.epfl.ch/lamp/cs210/-/blob/master/labs/example-lab.md#ide-features-like-type-on-hover-or-go-to-definition-do-not-work), if you still have problems, use `compile` in sbt instead.** + +# Be functional! + +This course is about **functional** programming, therefore you're not allowed to use the following +constructs in this assignment: +- `var` +- `while` +- `return` +- Any class in the `scala.collection.mutable` package + +# Exercise + +You are given the method `fImperative`, which computes its result from a given list of distinct characters `l1`, which represents a secret word, and another list `l2`, which represents a guess word. The result of the method is a list of characters from the secret word that are present in the guess word (in order of appearance from the secret word and without duplicate letters). + +For example, for the given secret word: `('o','t','h','e','r')` and the guess word: `('t','a','r','t','s')` the output list should be: `('t','r')`. + +```scala + def fImperative(l1: List[Char], l2: List[Char]): List[Char] = { + var i = 0 + var res: List[Char] = List() + + while (i < l1.size) { + var j = 0 + while (j < l2.size) { + if(l2(j) == l1(i)) { + res = l2(j) :: res + j = l2.size + } + j = j + 1 + } + i = i + 1 + } + res.reverse + } +``` + +This solution uses var-s and while loops so it is not in the spirit of functional programming. Your task is to write a new implementation of this method in a purely functional way, using pattern matching and a limited set of existing list methods given in the appendix. Your solution should produce the same result as the given imperative method, but without imperative constructs such as var and while. You may define and implement additional methods. Correct solutions will be given a full score even if they are not tail-recursive. + +In this exercise you are only allowed to use existing list methods which are listed in the appendix. + +`def f(l1: List[Char], l2: List[Char]): List[Char] = ???` + +# Appendix + +Here are the methods that you are allowed to use on `List[A]`: + +`xs.head: A`: returns the first element of the list. Throws an exception if the list is empty. + +`xs.tail: List[A]`: returns the list `xs` without its first element. Throws an exception if the list is empty. + +`xs.isEmpty: Boolean`: returns `true` if the list does not contain any elements, `false` otherwise. + +`x :: (xs: List[A]): List[A]`: prepends the element `x` to the left of `xs`, returning a `List[A]`. + +`xs.reverse: List[A]`: reverses the elements of the list `xs`.