diff --git a/__pycache__/function.cpython-313.pyc b/__pycache__/function.cpython-313.pyc new file mode 100644 index 0000000..1d562a2 Binary files /dev/null and b/__pycache__/function.cpython-313.pyc differ diff --git a/function.py b/function.py new file mode 100644 index 0000000..a437637 --- /dev/null +++ b/function.py @@ -0,0 +1,81 @@ +from enum import unique + + +def get_unique_list(lst): + """ + Takes a list as an argument and returns a new list with unique elements from the first list. + + Parameters: + lst (list): The input list. + + Returns: + list: A new list with unique elements from the input list. + """ + list = [] + for i in lst: + if i not in list: + list.append(i) + print(list) + + +def count_case(string): + + uppercase = 0 + lowercase = 0 + for i in string: + if i.isupper(): + uppercase += 1 + elif i.islower(): + lowercase += 1 + print(f"Uppercase count: {uppercase}\nLowercase count: {lowercase}") + """ + Returns the number of uppercase and lowercase letters in the given string. + + Parameters: + string (str): The string to count uppercase and lowercase letters in. + + Returns: + A tuple containing the count of uppercase and lowercase letters in the string. + """ + # your code goes here + +from itertools import count +from os import remove +import string + +from numpy import append + + +def remove_punctuation(sentence): + """ + Removes all punctuation marks (commas, periods, exclamation marks, question marks) from a sentence. + + Parameters: + sentence (str): A string representing a sentence. + + Returns: + str: The sentence without any punctuation marks. + """ + new_sentence = [] + for i in sentence: + if i not in string.punctuation: + new_sentence.append(i) + return new_sentence + + +def word_count(sentence): + """ + Counts the number of words in a given sentence. To do this properly, first it removes punctuation from the sentence. + Note: A word is defined as a sequence of characters separated by spaces. We can assume that there will be no leading or trailing spaces in the input sentence. + + Parameters: + sentence (str): A string representing a sentence. + + Returns: + int: The number of words in the sentence. + """ + no_punct = str.maketrans(" ", " ", string.punctuation) + sentence = sentence.translate(no_punct).replace(" ", " ") + + count_words = count(len(sentence.split())) + return count_words diff --git a/lab-python-functions.ipynb b/lab-python-functions.ipynb index 7e1dcbd..7324385 100644 --- a/lab-python-functions.ipynb +++ b/lab-python-functions.ipynb @@ -31,8 +31,19 @@ "execution_count": null, "id": "df908bed-acc6-4b67-b33a-f3b1c564a49f", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 2, 3, 4, 5, 6, 7, 8]\n" + ] + } + ], "source": [ + "from enum import unique\n", + "\n", + "\n", "def get_unique_list(lst):\n", " \"\"\"\n", " Takes a list as an argument and returns a new list with unique elements from the first list.\n", @@ -43,7 +54,15 @@ " Returns:\n", " list: A new list with unique elements from the input list.\n", " \"\"\"\n", - " # your code goes here\n" + " list = []\n", + " for i in lst:\n", + " if i not in list:\n", + " list.append(i)\n", + " print(list)\n", + "\n", + "\n", + "list1 = [1, 2, 3, 4, 1, 2, 4, 5, 6, 7, 8]\n", + "get_unique_list(list1)" ] }, { @@ -63,9 +82,27 @@ "execution_count": null, "id": "7d5c8e34-a116-4428-ab9d-e0e15e338fff", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Uppercase count: 2\n", + "Lowercase count: 8\n" + ] + } + ], "source": [ "def count_case(string):\n", + "\n", + " uppercase = 0\n", + " lowercase = 0\n", + " for i in string:\n", + " if i.isupper():\n", + " uppercase += 1\n", + " elif i.islower():\n", + " lowercase += 1\n", + " print(f\"Uppercase count: {uppercase}\\nLowercase count: {lowercase}\")\n", " \"\"\"\n", " Returns the number of uppercase and lowercase letters in the given string.\n", "\n", @@ -75,7 +112,11 @@ " Returns:\n", " A tuple containing the count of uppercase and lowercase letters in the string.\n", " \"\"\"\n", - " # your code goes here" + " # your code goes here\n", + "\n", + "\n", + "in_put = \"Hello World\"\n", + "count_case(in_put)" ] }, { @@ -95,10 +136,26 @@ "execution_count": null, "id": "c15b91d4-cfd6-423b-9f36-76012b8792b8", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "count(7)" + ] + }, + "execution_count": 61, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ + "from itertools import count\n", + "from os import remove\n", "import string\n", "\n", + "from numpy import append\n", + "\n", + "\n", "def remove_punctuation(sentence):\n", " \"\"\"\n", " Removes all punctuation marks (commas, periods, exclamation marks, question marks) from a sentence.\n", @@ -109,20 +166,33 @@ " Returns:\n", " str: The sentence without any punctuation marks.\n", " \"\"\"\n", - " # your code goes here\n", + " new_sentence = []\n", + " for i in sentence:\n", + " if i not in string.punctuation:\n", + " new_sentence.append(i)\n", + " return new_sentence\n", + "\n", "\n", "def word_count(sentence):\n", " \"\"\"\n", " Counts the number of words in a given sentence. To do this properly, first it removes punctuation from the sentence.\n", " Note: A word is defined as a sequence of characters separated by spaces. We can assume that there will be no leading or trailing spaces in the input sentence.\n", - " \n", + "\n", " Parameters:\n", " sentence (str): A string representing a sentence.\n", "\n", " Returns:\n", " int: The number of words in the sentence.\n", " \"\"\"\n", - " # your code goes here" + " no_punct = str.maketrans(\" \", \" \", string.punctuation)\n", + " sentence = sentence.translate(no_punct).replace(\" \", \" \")\n", + "\n", + " count_words = count(len(sentence.split()))\n", + " return count_words\n", + "\n", + "\n", + "word = \"Note : this is an example !!! Good day : )\"\n", + "word_count(word)" ] }, { @@ -171,9 +241,54 @@ "execution_count": null, "id": "57f9afc7-8626-443c-9c3e-eb78ef503193", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "'Error'" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# your code goes here" + "def add(a, b):\n", + " return a + b\n", + "\n", + "\n", + "def sub(a, b):\n", + " return a - b\n", + "\n", + "\n", + "def mult(a, b):\n", + " return a * b\n", + "\n", + "\n", + "def divi(a, b):\n", + " if b == 0:\n", + " return \"This operation can not be done\"\n", + " else:\n", + " return a / b\n", + "\n", + "\n", + "def opperation(a, b, opper):\n", + " if opper == \"+\":\n", + " return add(a, b)\n", + " elif opper == \"-\":\n", + " return sub(a, b)\n", + " elif opper == \"*\":\n", + " return mult(a, b)\n", + " elif opper == \"/\":\n", + " return divi(a, b)\n", + " else:\n", + " return \"Error\"\n", + "\n", + "\n", + "x = 5\n", + "z = 2\n", + "opperation(x, z, \"\")" ] }, { @@ -195,9 +310,52 @@ "execution_count": null, "id": "ff3e816c-13ab-447d-a6f2-bb47a8fad2e2", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "11" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# your code goes here" + "def add(a, b, args):\n", + " return a + b + args\n", + "\n", + "\n", + "def sub(a, b, args):\n", + " return a - b - args\n", + "\n", + "\n", + "def mult(a, b, args):\n", + " return a * b * args\n", + "\n", + "\n", + "def divi(a, b, args):\n", + " if b == 0:\n", + " return \"This operation can not be done\"\n", + " else:\n", + " return a / b / args\n", + "\n", + "\n", + "def opperation(a, b, opper, args):\n", + " if opper == \"+\":\n", + " return add(a, b, args)\n", + " elif opper == \"-\":\n", + " return sub(a, b, args)\n", + " elif opper == \"*\":\n", + " return mult(a, b, args)\n", + " elif opper == \"/\":\n", + " return divi(a, b, args)\n", + " else:\n", + " return \"Error\"\n", + "\n", + "\n", + "opperation(4, 3, \"+\", 4)" ] }, { @@ -276,13 +434,31 @@ "execution_count": null, "id": "5832ecfe-c652-418d-8fbc-bac4b1166b40", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Module loaded successfully!\n" + ] + } + ], "source": [ - "# IPython extension to reload modules before executing user code.\n", - "%load_ext autoreload\n", - "%autoreload 2 \n", + "import os\n", + "import sys\n", "\n", - "# your code goes here" + "# Add the current directory to sys.path so Python can find your modules\n", + "# This is especially helpful on Windows to avoid DLL or pathing issues\n", + "sys.path.append(os.getcwd())\n", + "\n", + "# Import your custom module\n", + "# Replace 'my_functions' with your actual .py filename\n", + "try:\n", + " import function as mf\n", + "\n", + " print(\"Module loaded successfully!\")\n", + "except ImportError:\n", + " print(\"Check if your .py file is in the same folder as this notebook.\")" ] }, { @@ -318,15 +494,60 @@ "execution_count": null, "id": "a1d55cea-96c3-4853-8220-17c0904a8816", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "233\n" + ] + }, + { + "data": { + "text/plain": [ + "[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233]" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# your code goes here" + "def fibo(x):\n", + "\n", + " if x <= 1:\n", + " return x\n", + " else:\n", + " return fibo(x - 1) + fibo(x - 2)\n", + "\n", + "\n", + "def safe_fibo(num):\n", + " sequance = []\n", + " a, b = 0, 1\n", + " for _ in range(num + 1):\n", + " sequance.append(a)\n", + " a, b = b, a + b\n", + " return sequance\n", + "\n", + "\n", + "random = 13\n", + "print(fibo(random))\n", + "safe_fibo(random)" ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "a7d919f1", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -340,7 +561,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.9" } }, "nbformat": 4,