From 428d857359b5d8b639ca41b03ca8fc8b3d143a50 Mon Sep 17 00:00:00 2001 From: Oskar Date: Sat, 12 Oct 2024 15:06:05 +0200 Subject: more --- 9p18.cpp | 25 +++++++++++++++++++++++++ 9p19.cpp | 25 +++++++++++++++++++++++++ 9p20.cpp | 37 +++++++++++++++++++++++++++++++++++++ 9p21.cpp | 27 +++++++++++++++++++++++++++ 9p22.cpp | 23 +++++++++++++++++++++++ 5 files changed, 137 insertions(+) create mode 100644 9p18.cpp create mode 100644 9p19.cpp create mode 100644 9p20.cpp create mode 100644 9p21.cpp create mode 100644 9p22.cpp diff --git a/9p18.cpp b/9p18.cpp new file mode 100644 index 0000000..e7df6fa --- /dev/null +++ b/9p18.cpp @@ -0,0 +1,25 @@ +#include +#include + +/* + * + * 9.18 + * + * + */ + +int main () { + + std::string input; + std::deque ds; + while(std::cin >> input) { + ds.push_back(input); + } + + for(auto cbeg = ds.cbegin() ; cbeg != ds.cend() ; ++cbeg) { + std::cout << *cbeg << " "; + } + + std::cout << std::endl; + return 0; +} diff --git a/9p19.cpp b/9p19.cpp new file mode 100644 index 0000000..6c47476 --- /dev/null +++ b/9p19.cpp @@ -0,0 +1,25 @@ +#include +#include + +/* + * + * 9.19 + * + * Changes needed: Include list, Change deque in to list. + */ + +int main () { + + std::string input; + std::list ds; + while(std::cin >> input) { + ds.push_back(input); + } + + for(auto cbeg = ds.cbegin() ; cbeg != ds.cend() ; ++cbeg) { + std::cout << *cbeg << " "; + } + + std::cout << std::endl; + return 0; +} diff --git a/9p20.cpp b/9p20.cpp new file mode 100644 index 0000000..84cb78b --- /dev/null +++ b/9p20.cpp @@ -0,0 +1,37 @@ +#include +#include +#include + +/* + * + * 9.20 + * + * + */ + +int main () { + + std::deque odd; + std::deque even; + std::list numbers = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20}; + for(auto &a : numbers) { + if((a % 2) == 0) { + even.push_back(a); + } else { + odd.push_back(a); + } + } + + std::cout << "Even: \n"; + for(auto &a : even) { + std::cout << a << " "; + } + + std::cout << "\nOdd: \n"; + for(auto &a : odd) { + std::cout << a << " "; + } + + std::cout << std::endl; + return 0; +} diff --git a/9p21.cpp b/9p21.cpp new file mode 100644 index 0000000..be101b9 --- /dev/null +++ b/9p21.cpp @@ -0,0 +1,27 @@ +#include +#include +#include + +/* + * + * 9.21 + * + * It's the same thing. Vector does not have push_front so this would be necessary. + * It's an expensive operation to do this so it woul be best to use a deque + */ + +int main () { + + std::string word; + std::vector lst; + auto iter = lst.begin(); + while (std::cin >> word) { + iter = lst.insert(iter, word); // same as calling push_front + } + + for(auto &a : lst) { + std::cout << a << std::endl; + } + + return 0; +} diff --git a/9p22.cpp b/9p22.cpp new file mode 100644 index 0000000..90a7f92 --- /dev/null +++ b/9p22.cpp @@ -0,0 +1,23 @@ +#include +#include + +/* + * + * 9.22 + * + * I don't know what the author intends for the program to do. + * It may be obvious to the author but if he does tell me then + * it could be anything. + */ + +int main () { + + int some_val = 0; + std::vector iv = {1,2,3,4,5,6,7,8}; + std::vector::iterator iter = iv.begin(), + mid = iv.begin() + iv.size()/2; + while (iter != mid) + if (*iter == some_val) + iv.insert(iter, 2 * some_val); + return 0; +} -- cgit v1.2.3