diff --git a/refactoring.ipynb b/refactoring.ipynb index e2c178a..184c1f5 100644 --- a/refactoring.ipynb +++ b/refactoring.ipynb @@ -100,6 +100,15 @@ "$$" ] }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": {}, + "outputs": [], + "source": [ + "%autoreload 2" + ] + }, { "cell_type": "markdown", "metadata": { @@ -113,13 +122,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": { "slideshow": { "slide_type": "fragment" } }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "7" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "a = (1, 1)\n", "b = (4, 5)\n", @@ -159,7 +179,12 @@ }, "outputs": [], "source": [ - "# YOUR CODE HERE" + "def manhattan(a, b):\n", + " d_x = b[0] - a[0]\n", + " d_y = b[1] - a[1]\n", + "\n", + " distance = d_x + d_y\n", + " return distance" ] }, { @@ -171,9 +196,20 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "7" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "a = (1, 1)\n", "b = (4, 5)\n", @@ -204,13 +240,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 26, "metadata": { "slideshow": { "slide_type": "fragment" } }, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "7" + ] + }, + "execution_count": 26, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "from utils.distances import manhattan\n", "\n", @@ -226,7 +273,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 27, "metadata": {}, "outputs": [], "source": [ @@ -244,9 +291,38 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 28, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "\u001b[1m============================= test session starts ==============================\u001b[0m\n", + "platform linux -- Python 3.12.9, pytest-8.3.4, pluggy-1.5.0 -- /home/bariscan/.pyenv/versions/3.12.9/envs/workintech/bin/python\n", + "cachedir: .pytest_cache\n", + "rootdir: /home/bariscan/code/Workintech/data-distances/tests\n", + "plugins: typeguard-4.4.2, dash-4.0.0, anyio-4.8.0\n", + "\u001b[1mcollecting ... \u001b[0mcollected 2 items\n", + "\n", + "test_manhattan_from_notebook.py::TestManhattanFromNotebook::test_manhattan \u001b[32mPASSED\u001b[0m\u001b[32m [ 50%]\u001b[0m\n", + "test_manhattan_from_notebook.py::TestManhattanFromNotebook::test_manhattan_reverse \u001b[32mPASSED\u001b[0m\u001b[32m [100%]\u001b[0m\n", + "\n", + "\u001b[32m============================== \u001b[32m\u001b[1m2 passed\u001b[0m\u001b[32m in 0.01s\u001b[0m\u001b[32m ===============================\u001b[0m\n", + "\n", + "\n", + "💯 You can commit your code:\n", + "\n", + "\u001b[1;32mgit\u001b[39m add tests/manhattan_from_notebook.pickle\n", + "\n", + "\u001b[32mgit\u001b[39m commit -m \u001b[33m'Completed manhattan_from_notebook step'\u001b[39m\n", + "\n", + "\u001b[32mgit\u001b[39m push origin master\n", + "\n" + ] + } + ], "source": [ "print(result.check())" ] @@ -266,9 +342,18 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 29, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Distance between a and b: 7\n", + "Distance between b and a: 7\n" + ] + } + ], "source": [ "print(\"Distance between a and b:\", manhattan(a,b))\n", "print(\"Distance between b and a:\", manhattan(b,a))" @@ -778,9 +863,21 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "workintech", "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.12.9" } }, "nbformat": 4, diff --git a/tests/manhattan_from_notebook.pickle b/tests/manhattan_from_notebook.pickle new file mode 100644 index 0000000..09eb139 Binary files /dev/null and b/tests/manhattan_from_notebook.pickle differ diff --git a/tests/test_euclidean.py b/tests/test_euclidean.py new file mode 100644 index 0000000..02d81ce --- /dev/null +++ b/tests/test_euclidean.py @@ -0,0 +1,11 @@ +from utils.distances import euclidean + +def test_euclidean(): + # Temel senaryolar + assert euclidean((0, 0), (0, 0)) == 0 + assert euclidean((0, 0), (3, 4)) == 5 # 3-4-5 üçgeni + assert euclidean((0, 0), (1, 0)) == 1 + + # Negatif değer senaryoları + assert euclidean((0, 0), (-3, -4)) == 5 + assert euclidean((3, 4), (0, 0)) == 5 diff --git a/tests/test_manhattan.py b/tests/test_manhattan.py index 45cabc8..99d3c16 100644 --- a/tests/test_manhattan.py +++ b/tests/test_manhattan.py @@ -4,9 +4,5 @@ def test_manhattan(): assert manhattan((0, 0), (0, 0)) == 0 assert manhattan((0, 0), (1, 1)) == 2 assert manhattan((0, 0), (1, 0)) == 1 - assert manhattan((0, 0), (0, 1)) == 1 assert manhattan((0, 0), (-1, 0)) == 1 - assert manhattan((0, 0), (0, -1)) == 1 assert manhattan((0, 0), (-1, -1)) == 2 - assert manhattan((0, 0), (1, -1)) == 2 - assert manhattan((0, 0), (-1, 1)) == 2 diff --git a/utils/distances.py b/utils/distances.py index 86dee54..ed7fd2c 100644 --- a/utils/distances.py +++ b/utils/distances.py @@ -1 +1,15 @@ -pass # YOUR CODE HERE +def minkowski(a, b, p): + d_x = b[0] - a[0] + d_y = b[1] - a[1] + + # Genel Minkowski formülü: (|dx|^p + |dy|^p)^(1/p) + distance = (abs(d_x)**p + abs(d_y)**p)**(1/p) + return distance + +def manhattan(a, b): + # p=1 durumu + return minkowski(a, b, 1) + +def euclidean(a, b): + # p=2 durumu + return minkowski(a, b, 2)