From 2792a87c95c098d77996370148dc7f15917e0ca1 Mon Sep 17 00:00:00 2001 From: JirR02 Date: Mon, 21 Apr 2025 18:40:47 +0200 Subject: [PATCH] Exercise 8 Added Exercise 8 to repository --- Informatik_I/Exercise_8/Task_1/README.org | 65 ++++++++++++ Informatik_I/Exercise_8/Task_2/README.org | 119 ++++++++++++++++++++++ Informatik_I/Exercise_8/Task_3/README.org | 81 +++++++++++++++ Informatik_I/Exercise_8/Task_4/README.org | 118 +++++++++++++++++++++ 4 files changed, 383 insertions(+) create mode 100644 Informatik_I/Exercise_8/Task_1/README.org create mode 100644 Informatik_I/Exercise_8/Task_2/README.org create mode 100644 Informatik_I/Exercise_8/Task_3/README.org create mode 100644 Informatik_I/Exercise_8/Task_4/README.org diff --git a/Informatik_I/Exercise_8/Task_1/README.org b/Informatik_I/Exercise_8/Task_1/README.org new file mode 100644 index 0000000..8147f27 --- /dev/null +++ b/Informatik_I/Exercise_8/Task_1/README.org @@ -0,0 +1,65 @@ +#+title: Task 1: Reverse Digits + +#+author: JirR02 +* Task +:PROPERTIES: +:CUSTOM_ID: task +:END: +The goal of this task is to implement a program to read a number and +print its reverse. + +You should complete the implementation of the reverse function in +=reverse.cpp=. This function gets an =int= as input parameter, and it +should print the digits of the number in reverse. + +The =reverse= function must be implemented recursively (without any +loop). To enforce this, we disallow the use of =for= or =while=. + +* Input +:PROPERTIES: +:CUSTOM_ID: input +:END: +The input is a single non-negative =int= number. + +An input example: + +#+begin_src shell + 321231 +#+end_src + +* Output +:PROPERTIES: +:CUSTOM_ID: output +:END: +For the above input, the output should be: + +#+begin_src shell + 132123 +#+end_src + +Note: You are not allowed to use the string library to solve this task! + +* Solution +:PROPERTIES: +:CUSTOM_ID: solution +:END: +#+begin_src cpp +#include "reverse.h" +#include + +// PRE: n >= 0 +// POST: print the digits of the input in reverse order to std::cout +void reverse(int n) { + + int res = n % 10; + std::cout << res; + + int new_int = n / 10; + + if (new_int == 0) { + return; + } + + reverse(new_int); +} +#+end_src diff --git a/Informatik_I/Exercise_8/Task_2/README.org b/Informatik_I/Exercise_8/Task_2/README.org new file mode 100644 index 0000000..02a4bbc --- /dev/null +++ b/Informatik_I/Exercise_8/Task_2/README.org @@ -0,0 +1,119 @@ +#+title: Task 2: Set Product + +#+author: JirR02 +* Task +:PROPERTIES: +:CUSTOM_ID: task +:END: +In this exercise, you are going to implement a function which computes +the /n-fold Cartesian product/ for sets. The n-fold Cartesian product is +a way to combine multiple sets of items. Imagine you have multiple sets, +denoted as \(A_1, A_2, ..., A_n\). The n-fold Cartesian product is like +taking one item from each set and combining them into a new set. + +For example, let's say you have two sets: + +\[ +A_1 = \{a,b,c\} \text{ and } A_2 = \{ x, y, z \} +\] + +The Cartesian product of these two sets would be a new set that contains +all possible combinations of one item from \(A_1\) and one item from +\(A_2\): + +\[ +A_1 \times A_2 = \{(a,x),(a,y),(a,z),(b,x),(b,y),(b,z),(c,x),(c,y),(c,z)\} +\] + +In general, for any two sets \(A\) and \(B\), the Cartesian product +\(A times B\) is defined as: + +\[ +A \times B \{(a,b) | a \in A \text{ and } b \in B\} +\] + +This can be generalized to the n-fold Cartesian product. For any \(n\) +sets \(A_1, A_2, ... , A_n\), the n-fold Cartesian product is defined +as: + +\[ +A_1 \times A_2 \times ... \times A_n = \{(a_1, a_2, ..., a_n) | a_i \in A_i \text{ for all } i = 1, 2, ... , n\} +\] + +Note: This task naturally lends itself to a *recursive implementation*. + +** The Set class +:PROPERTIES: +:CUSTOM_ID: the-set-class +:END: +A set class is implemented in =set.h= and =set.ipp=. This class +implements a number of useful operations on sets. *For an overview of +its functionalities, please refer to this week's Power Set code +example*. Note that you do not need to understand any of the code in +=set.h= and =set.ipp= in order to use it! + +* Input & Output +:PROPERTIES: +:CUSTOM_ID: input-output +:END: +When the program starts, you are first prompted to enter the number of +sets. Then, you are prompted to enter each set. Sets are entered on a +single line using the format += =. + +Once all sets are entered, the set product is computed using the +=set_product= function. Finally, the resulting set is printed to the +console. + +*** Example +:PROPERTIES: +:CUSTOM_ID: example +:END: +#+begin_src shell +Number of sets: 2 +Set 0: 3 a b c +Set 1: 2 x y +Product Set: +{ax,ay,bx,by,cx,cy} +#+end_src + +*Note*: Each input set is a set of char elements. At least 1 set must be +inputted. + +* Solution +:PROPERTIES: +:CUSTOM_ID: solution +:END: +#+begin_src cpp +#include +#include +#include + +#include "set.h" +#include "solution.h" + +StringSet set_product(const std::vector &sets) { + StringSet res; + StringSet ram; + if (sets.size() == 1) { + for (int i = 0; i < sets[0].size(); ++i) { + std::string j(1, sets[0][i]); + res.insert(j); + } + return res; + } + + CharSet first_subset = sets[0]; + std::vector remaining_set(sets.begin() + 1, sets.end()); + + StringSet res_subset = set_product(remaining_set); + + for (char char_first_subset : first_subset.elements()) { + for (std::string str_res_subset : res_subset.elements()) { + res.insert(std::string(1, char_first_subset) + str_res_subset); + } + } + + return res; +} +#+end_src diff --git a/Informatik_I/Exercise_8/Task_3/README.org b/Informatik_I/Exercise_8/Task_3/README.org new file mode 100644 index 0000000..d6fec98 --- /dev/null +++ b/Informatik_I/Exercise_8/Task_3/README.org @@ -0,0 +1,81 @@ +#+title: Task 3: All permutations + +#+author: JirR02 +* Task +:PROPERTIES: +:CUSTOM_ID: task +:END: +In this exercise, you are going to implement a function which computes +the set of all permutations of a given string. + +Note: This task naturally lends itself to a *recursive implementation*. + +** Example +:PROPERTIES: +:CUSTOM_ID: example +:END: +The string "abc" has the following set of permutations: + +{ abc, acb, bac, bca, cab, cba } + +** The Set class +:PROPERTIES: +:CUSTOM_ID: the-set-class +:END: +A set class is implemented in =set.h= and =set.ipp=. This class +implements a number of useful operations on sets. *For an overview of +its functionalities, please refer to the Power Set code example*. Note +that you do not need to understand any of the code in =set.h= and +=set.ipp= in order to use it! + +* Input & Output +:PROPERTIES: +:CUSTOM_ID: input-output +:END: +When the program starts, you are first prompted to enter a string. + +Then, the program computes the set of all permutations using the +function *all_permutations*. Finally, the resulting set is printed to +the console. + +*** Example +:PROPERTIES: +:CUSTOM_ID: example-1 +:END: +#+begin_src shell +String: abc +Permutations: +{abc,acb,bac,bca,cab,cba} +#+end_src + +Hint: You may find the substr method useful. The method is documented +here. + +* Solution +:PROPERTIES: +:CUSTOM_ID: solution +:END: +#+begin_src cpp +#include "pair_sum.h" +#include + +// PRE: for any two indices i and j, v[i] + v[j] ≤ INT_MAX +// POST: returns the number of indices (i,j), i