From 5f29d2d61a17fddaa26235e53410fc26fb0e73b1 Mon Sep 17 00:00:00 2001 From: TanCodes Date: Sat, 9 Oct 2021 20:28:09 +0530 Subject: [PATCH] Python Dsa added #1 array --- Python/Python_DSA/#1 Array.ipynb | 572 ++++++++++++++++++ .../#1 Array-checkpoint.ipynb | 572 ++++++++++++++++++ 2 files changed, 1144 insertions(+) create mode 100644 Python/Python_DSA/#1 Array.ipynb create mode 100644 Python/Python_DSA/.ipynb_checkpoints/#1 Array-checkpoint.ipynb diff --git a/Python/Python_DSA/#1 Array.ipynb b/Python/Python_DSA/#1 Array.ipynb new file mode 100644 index 0000000..4c37a30 --- /dev/null +++ b/Python/Python_DSA/#1 Array.ipynb @@ -0,0 +1,572 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#
🔰 Arrays in Python" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### An array is a collection of items stored at contiguous memory locations.\n", + "\n", + "- Python arrays are a data structure like lists. \n", + "- They contain a number of objects that can be of different data types.\n", + "- Python has a number of built-in data structures, such as arrays.\n", + "- Index starts with 0.\n", + "- We can access each element via its index.\n", + "- The length of the array defines the capacity to store the elements." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "![](https://ik.imagekit.io/tfme5aczhhf/contributiojn/mceu_61653743011611745415316_1__OE7LeT5_K.jpg)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Some of the basic operations supported by an array are as follows:\n", + "\n", + "- [x] Traverse - It prints all the elements one by one.
\n", + "- [x] Insertion - It adds an element at the given index.
\n", + "- [x] Deletion - It deletes an element at the given index.
\n", + "- [x] Search - It searches an element using the given index or by the value.
\n", + "- [x] Update - It updates an element at the given index.
" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "##### Create basic array" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['1st', '2nd', '3rd']" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Simple way to create an array.\n", + "\n", + "array = [\"1st\", \"2nd\", \"3rd\"]\n", + "#prints: ['1st', '2nd', '3rd']\n", + "array" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "array('d', [1.1, 3.5, 4.5])\n" + ] + } + ], + "source": [ + "# Also can be created using array module\n", + "\n", + "import array as arr\n", + "a = arr.array('d', [1.1, 3.5, 4.5])\n", + "print(a)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Working with Arrays" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['data1', 'data2', 3, 4, 5]" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# created one small array\n", + "# -------------------------------------------------------------------\n", + "arr = [\"data1\",\"data2\", 3, 4, 5]\n", + "arr" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "3" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#acessing the array - We are passing the index of an array. Index starts with 0\n", + "# -------------------------------------------------------------------\n", + "arr[2]" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "5" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# len() gives the length of our array i.e 6 which len starts with 1.\n", + "# -------------------------------------------------------------------\n", + "len(arr)" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['data0', 'data2', 3, 4, 5]" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# replacing (updating) the data of an array\n", + "# -> We updated the index 0 data with new data i.e \"data0\"\n", + "# -------------------------------------------------------------------\n", + "arr[0] = \"data0\"\n", + "arr" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['data0', 'data2', 3, 4, 5, 'last_data']" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#APPEND - adds the element to the last postion of the array\n", + "# -------------------------------------------------------------------\n", + "arr.append(\"last_data\")\n", + "arr" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['data0', 3, 4, 5, 'last_data']" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# removes the data from an array which is \"data2\"\n", + "# -------------------------------------------------------------------\n", + "arr.remove(\"data2\")\n", + "arr" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "4" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# pop means to remove the data from an array at given index\n", + "# -------------------------------------------------------------------\n", + "arr.pop(2)" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['data0', 3, 5, 'last_data']" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# so this is now our array after popping the data from index 2\n", + "# -------------------------------------------------------------------\n", + "arr" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "data0\n", + "3\n", + "5\n", + "last_data\n" + ] + } + ], + "source": [ + "# Looping through the array\n", + "# -------------------------------------------------------------------\n", + "for items in arr:\n", + " print(items)" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['last_data', 5, 3, 'data0']" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# it reverses the array\n", + "# -------------------------------------------------------------------\n", + "arr.reverse()\n", + "arr" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [], + "source": [ + "# clear, removes all the array data and makes it empty.\n", + "# -------------------------------------------------------------------\n", + "arr.clear()" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[]" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "arr #so our array is now empty after using clear()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Q) Find the second largest number in an array" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [], + "source": [ + "a = [1,3,4,5,0,2]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### could array be empyt?\n", + "yes . 2nd largest(a) should return None." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### could array have only one element?\n", + "yes. 2nd largest(a) should return None." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### what if we had say [2,2,1]\n", + "yes . 2nd largest(a) should return none . then 2nd largest(a) should return 2" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [], + "source": [ + "def second_largest(given_list):\n", + " largest = None\n", + " second_largest = None\n", + " for current_number in given_list:\n", + " if largest == None:\n", + " largest = current_number\n", + " elif current_number > largest:\n", + " second_largest = largest\n", + " largest = current_number\n", + " elif second_largest == None:\n", + " second_largest = current_number\n", + " elif current_number > second_largest:\n", + " second_largest = current_number\n", + " return second_largest" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "##### Test cases" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Max is 4\n" + ] + } + ], + "source": [ + "print(\"Max is\", second_largest([1,3,4,5,0,2]))" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Max is -2\n" + ] + } + ], + "source": [ + "print(\"Max is\",second_largest([-2,-1]))" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Max is 2\n" + ] + } + ], + "source": [ + "print(\"Max is\",second_largest([2,2,1]))" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Max is None\n" + ] + } + ], + "source": [ + "print(\"Max is\",second_largest([2]))" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Max is None\n" + ] + } + ], + "source": [ + "print(\"Max is\",second_largest([]))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## `Find me around the web 🌎:`\n", + "\n", + "\n", + "* Portfolio \n", + "* Instagram \n", + "* LinkedIn \n", + "* Twitter \n", + "* Behance \n", + "* YouTube \n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "\n", + "###
HAPPY CODING WITH PYTHON \n", + "![](https://img.shields.io/badge/Python-FFD43B?style=for-the-badge&logo=python&logoColor=darkgreen)\n", + "![](https://forthebadge.com/images/badges/built-with-love.svg)\n", + "

\n", + " \n", + "

" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "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.0" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} diff --git a/Python/Python_DSA/.ipynb_checkpoints/#1 Array-checkpoint.ipynb b/Python/Python_DSA/.ipynb_checkpoints/#1 Array-checkpoint.ipynb new file mode 100644 index 0000000..4c37a30 --- /dev/null +++ b/Python/Python_DSA/.ipynb_checkpoints/#1 Array-checkpoint.ipynb @@ -0,0 +1,572 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#
🔰 Arrays in Python" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### An array is a collection of items stored at contiguous memory locations.\n", + "\n", + "- Python arrays are a data structure like lists. \n", + "- They contain a number of objects that can be of different data types.\n", + "- Python has a number of built-in data structures, such as arrays.\n", + "- Index starts with 0.\n", + "- We can access each element via its index.\n", + "- The length of the array defines the capacity to store the elements." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "![](https://ik.imagekit.io/tfme5aczhhf/contributiojn/mceu_61653743011611745415316_1__OE7LeT5_K.jpg)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Some of the basic operations supported by an array are as follows:\n", + "\n", + "- [x] Traverse - It prints all the elements one by one.
\n", + "- [x] Insertion - It adds an element at the given index.
\n", + "- [x] Deletion - It deletes an element at the given index.
\n", + "- [x] Search - It searches an element using the given index or by the value.
\n", + "- [x] Update - It updates an element at the given index.
" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "##### Create basic array" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['1st', '2nd', '3rd']" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Simple way to create an array.\n", + "\n", + "array = [\"1st\", \"2nd\", \"3rd\"]\n", + "#prints: ['1st', '2nd', '3rd']\n", + "array" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "array('d', [1.1, 3.5, 4.5])\n" + ] + } + ], + "source": [ + "# Also can be created using array module\n", + "\n", + "import array as arr\n", + "a = arr.array('d', [1.1, 3.5, 4.5])\n", + "print(a)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Working with Arrays" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['data1', 'data2', 3, 4, 5]" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# created one small array\n", + "# -------------------------------------------------------------------\n", + "arr = [\"data1\",\"data2\", 3, 4, 5]\n", + "arr" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "3" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#acessing the array - We are passing the index of an array. Index starts with 0\n", + "# -------------------------------------------------------------------\n", + "arr[2]" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "5" + ] + }, + "execution_count": 5, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# len() gives the length of our array i.e 6 which len starts with 1.\n", + "# -------------------------------------------------------------------\n", + "len(arr)" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['data0', 'data2', 3, 4, 5]" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# replacing (updating) the data of an array\n", + "# -> We updated the index 0 data with new data i.e \"data0\"\n", + "# -------------------------------------------------------------------\n", + "arr[0] = \"data0\"\n", + "arr" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['data0', 'data2', 3, 4, 5, 'last_data']" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#APPEND - adds the element to the last postion of the array\n", + "# -------------------------------------------------------------------\n", + "arr.append(\"last_data\")\n", + "arr" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['data0', 3, 4, 5, 'last_data']" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# removes the data from an array which is \"data2\"\n", + "# -------------------------------------------------------------------\n", + "arr.remove(\"data2\")\n", + "arr" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "4" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# pop means to remove the data from an array at given index\n", + "# -------------------------------------------------------------------\n", + "arr.pop(2)" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['data0', 3, 5, 'last_data']" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# so this is now our array after popping the data from index 2\n", + "# -------------------------------------------------------------------\n", + "arr" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "data0\n", + "3\n", + "5\n", + "last_data\n" + ] + } + ], + "source": [ + "# Looping through the array\n", + "# -------------------------------------------------------------------\n", + "for items in arr:\n", + " print(items)" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "['last_data', 5, 3, 'data0']" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# it reverses the array\n", + "# -------------------------------------------------------------------\n", + "arr.reverse()\n", + "arr" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [], + "source": [ + "# clear, removes all the array data and makes it empty.\n", + "# -------------------------------------------------------------------\n", + "arr.clear()" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[]" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "arr #so our array is now empty after using clear()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Q) Find the second largest number in an array" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [], + "source": [ + "a = [1,3,4,5,0,2]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### could array be empyt?\n", + "yes . 2nd largest(a) should return None." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### could array have only one element?\n", + "yes. 2nd largest(a) should return None." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### what if we had say [2,2,1]\n", + "yes . 2nd largest(a) should return none . then 2nd largest(a) should return 2" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [], + "source": [ + "def second_largest(given_list):\n", + " largest = None\n", + " second_largest = None\n", + " for current_number in given_list:\n", + " if largest == None:\n", + " largest = current_number\n", + " elif current_number > largest:\n", + " second_largest = largest\n", + " largest = current_number\n", + " elif second_largest == None:\n", + " second_largest = current_number\n", + " elif current_number > second_largest:\n", + " second_largest = current_number\n", + " return second_largest" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "##### Test cases" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Max is 4\n" + ] + } + ], + "source": [ + "print(\"Max is\", second_largest([1,3,4,5,0,2]))" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Max is -2\n" + ] + } + ], + "source": [ + "print(\"Max is\",second_largest([-2,-1]))" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Max is 2\n" + ] + } + ], + "source": [ + "print(\"Max is\",second_largest([2,2,1]))" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Max is None\n" + ] + } + ], + "source": [ + "print(\"Max is\",second_largest([2]))" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Max is None\n" + ] + } + ], + "source": [ + "print(\"Max is\",second_largest([]))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## `Find me around the web 🌎:`\n", + "\n", + "\n", + "* Portfolio \n", + "* Instagram \n", + "* LinkedIn \n", + "* Twitter \n", + "* Behance \n", + "* YouTube \n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "\n", + "###
HAPPY CODING WITH PYTHON \n", + "![](https://img.shields.io/badge/Python-FFD43B?style=for-the-badge&logo=python&logoColor=darkgreen)\n", + "![](https://forthebadge.com/images/badges/built-with-love.svg)\n", + "

\n", + " \n", + "

" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "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.0" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +}