Exercise 8
Added Exercise 8 to repository
This commit is contained in:
85
Informatik_I/Exercise_8/Task_3/README.org
Normal file
85
Informatik_I/Exercise_8/Task_3/README.org
Normal file
@@ -0,0 +1,85 @@
|
||||
#+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 <climits>
|
||||
|
||||
// PRE: for any two indices i and j, v[i] + v[j] ≤ INT_MAX
|
||||
// POST: returns the number of indices (i,j), i<j of a vector v
|
||||
// that corresponds to the given iterator range such that
|
||||
// v[i] + v[j] == sum.
|
||||
int pairs_with_sum(int sum, iterator begin, iterator end) {
|
||||
int count = 0;
|
||||
int size = end - begin;
|
||||
|
||||
for (int i = 0; i < size; ++i) {
|
||||
for (int j = i + 1; j < size; ++j) {
|
||||
iterator cur_begin = begin + i;
|
||||
iterator cur_end = begin + j;
|
||||
if (*cur_begin + *cur_end == sum)
|
||||
++count;
|
||||
}
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
#+end_src
|
||||
|
||||
--------------
|
||||
|
||||
Made by JirR02 in Switzerland 🇨🇭
|
Reference in New Issue
Block a user