Add 13th solution

This commit is contained in:
Patrick 2023-01-31 11:41:32 +01:00
parent 7cb1158a5a
commit 6af262dafb
Signed by: Paddi
GPG Key ID: CEF3546E095DF113
1 changed files with 195 additions and 0 deletions

195
solutions/13/13.ipynb Normal file
View File

@ -0,0 +1,195 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "2338e70c-941e-47d1-b6c4-8f2cd1bd5685",
"metadata": {},
"outputs": [],
"source": [
"#include <iostream>\n",
"#include <algorithm>\n",
"#include <cmath>"
]
},
{
"cell_type": "markdown",
"id": "783d966b-26f0-4074-a14e-73aaae3f4cce",
"metadata": {},
"source": [
"# Werte Tauschen"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "af180c70-ab20-49bb-ae74-ed34fa3e9bfe",
"metadata": {},
"outputs": [],
"source": [
"template<typename T>\n",
"void swap(T &a, T &b) {\n",
" T tmp = a;\n",
" a = b;\n",
" b = a;\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "fc50e18b-daf6-4e4f-8959-58395e3f1489",
"metadata": {},
"outputs": [],
"source": [
"int a=1; int b=2;\n",
"swap(a,b);\n",
"assert(a==2);\n",
"assert(b==1);\n",
"std::string c=\"hallo\"; std::string d= \"welt\";\n",
"swap(c,d);\n",
"assert(c==\"welt\");\n",
"assert(d==\"hallo\");"
]
},
{
"cell_type": "markdown",
"id": "71edb726-91b7-4d2e-bf88-48c454781b21",
"metadata": {},
"source": [
"# Werte sortieren"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "8e188f82-b014-4949-a7bd-7d81dcecb846",
"metadata": {},
"outputs": [],
"source": [
"template <typename T>\n",
"void sort_by_distance_to(std::vector<T> &vec, T distance_to) {\n",
" std::sort(vec.begin(), vec.end(), [&] (const T &a, const T &b) -> bool {return std::abs(a - distance_to) < std::abs(b - distance_to);});\n",
" return;\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "548d66b4-f943-4048-b78c-780fc945dc3a",
"metadata": {},
"outputs": [],
"source": [
"std::vector<int> test_vec {1,2,3,4,5,6}"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "e4941422-785b-4895-8c2c-6b22276f93a8",
"metadata": {},
"outputs": [],
"source": [
"sort_by_distance_to(test_vec, 5);"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "fa6ab9b0-0d96-4bcb-ab77-4ad7a617fd74",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{ 5, 4, 6, 3, 2, 1 }"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"test_vec"
]
},
{
"cell_type": "markdown",
"id": "53e9fb64-b639-4f32-96f6-96dccc32d468",
"metadata": {
"tags": []
},
"source": [
"# Online Hilfe cppreference.com\n",
"1. \n",
"```c++\n",
"template< class ForwardIt, class T >\n",
"ForwardIt remove( ForwardIt first, ForwardIt last, const T& value );\n",
"```\n",
"2. `remove` removes all values matching the `value` parameter from the iterator given/bounded by `first` and `last` and returns the new end as a `ForwardIt`. To actually shrink the vector, [erase](https://en.cppreference.com/w/cpp/container/vector/erase) is used. This is called \"Eraseremove idiom\""
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "45dea8c5-5ce2-4833-99f5-89d2be15c237",
"metadata": {},
"outputs": [],
"source": [
"void remove_twos(std::vector<int> &vec) {\n",
" auto it = std::remove(vec.begin(), vec.end(), 2);\n",
" vec.erase(it, vec.end());\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "9fcc99b4-0112-462e-8630-f8e7b6e95c2e",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{ 7, 3, 1, -5 }"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"std::vector<int> numbers = {2,7,3,2,1,-5};\n",
"remove_twos(numbers);\n",
"numbers"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "44e725f3-50cc-4c8e-8e2c-f95f06e7eb4c",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "C++17",
"language": "C++17",
"name": "xcpp17"
},
"language_info": {
"codemirror_mode": "text/x-c++src",
"file_extension": ".cpp",
"mimetype": "text/x-c++src",
"name": "c++",
"version": "17"
}
},
"nbformat": 4,
"nbformat_minor": 5
}