diff --git a/Aufgabe02.py b/Aufgabe02.py new file mode 100644 index 0000000000000000000000000000000000000000..38d28f4e26e80432f6f310b164fa5da3333fc99e --- /dev/null +++ b/Aufgabe02.py @@ -0,0 +1,31 @@ +def find_min_max(A): + curr_min = A[0] + curr_max = A[0] + for i in A[1:]: + if i < curr_min: + curr_min = i + elif i > curr_max: + curr_max = i + return curr_min, curr_max + +def find_min_max_recursive(A, l=0, r=None): + if r is None: + r = len(A) + + if r - l == 1: + return A[l], A[l] + + min1, max1 = find_min_max_recursive(A, l, (l+r)//2) + min2, max2 = find_min_max_recursive(A, (l+r)//2, r) + + if min1 < min2: + minA = min1 + else: + minA = min2 + + if max1 > max2: + maxA = max1 + else: + maxA = max2 + + return minA, maxA diff --git a/Aufgabe02Testing.ipynb b/Aufgabe02Testing.ipynb new file mode 100644 index 0000000000000000000000000000000000000000..356f1ccd79d147fee29c623a4da52ab07458716c --- /dev/null +++ b/Aufgabe02Testing.ipynb @@ -0,0 +1,68 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "import random\n", + "\n", + "from Aufgabe02 import find_min_max, find_min_max_recursive" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[28, 99, 48, 91, 55, 36, 94, 36, 66, 69, 85, 94, 64, 83, 57, 31, 80, 51, 65, 39]\n", + "28 99\n", + "(28, 99)\n", + "(28, 99)\n" + ] + } + ], + "source": [ + "num_list = range(10, 100)\n", + "rand_list = random.choices(num_list, k=20)\n", + "\n", + "print(rand_list)\n", + "print(min(rand_list), max(rand_list))\n", + "print(find_min_max(rand_list))\n", + "print(find_min_max_recursive(rand_list))" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3.9.6 64-bit", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.6" + }, + "orig_nbformat": 4, + "vscode": { + "interpreter": { + "hash": "11938c6bc6919ae2720b4d5011047913343b08a43b18698fd82dedb0d4417594" + } + } + }, + "nbformat": 4, + "nbformat_minor": 2 +}