From 3b7495aa48ce2c43f43229a9ae58badd30440dbb Mon Sep 17 00:00:00 2001 From: Feliciu Svabu Date: Fri, 27 Feb 2026 15:04:00 +0000 Subject: [PATCH] Extra Lab completed --- __pycache__/function.cpython-313.pyc | Bin 0 -> 2936 bytes function.py | 81 +++++++++ lab-python-functions.ipynb | 263 ++++++++++++++++++++++++--- 3 files changed, 323 insertions(+), 21 deletions(-) create mode 100644 __pycache__/function.cpython-313.pyc create mode 100644 function.py diff --git a/__pycache__/function.cpython-313.pyc b/__pycache__/function.cpython-313.pyc new file mode 100644 index 0000000000000000000000000000000000000000..1d562a22362c23836165a0befa94862edc73225c GIT binary patch literal 2936 zcmbVO-A@!(6u&e3!S3uQh(N2x3mRoDU>e01s3yj?0xKK4>qgCPLWkW8Or4$C-q{r) zG1-?U(59sin$V<4ZPJ%MH1W|tq7M*MaENJ>)(74Y`~!NcK>gv)11%JrGVU0Jz@ zWL<-jqYqLyNZlz9_hj85@ia+D${Y8Nx9}FwBB_?RZ`{xQV(g16T!A@%JTM;QL6}p( zN+=b8KAZ~jNHz?%A`M$JzF24*w~R@)w`^!%mRYWP1aS~I^mv86&!Kxr&JsgPlS5?m z2qA`it9t}=^D-e5;DEH0Y+eca*wS)ivx#Gb1PCRWnp0p+LpmeH(a)BA}Y2+zqr7|~A!(elc)|^(FrOx3g(;2E-befw5TAF2aTH{~|%?&8Z zb(O0HR$|;5R&7v1q#FT@_*f}*+?QL+g`O;WJcch6uD-U zVlLYS+uCllYD=1ho}iT4SJ-g&`h02DG_qsdG)C3jo$M#~N?e`1rcPybb*jH8r2W%n zBUjQ)!|G=!&epyMit~14hLt8AKTP5VJOI`)f>s}N6|&P#g5k=?TcPNpv>6C~dvP-u zU5+osSETO}uRSE(`o>E<%Hqkd;(MS^Vu5W8=r+WcY~w+<(H0M37sf7%a~|xx*tKBi zgKi&!P=t&39zzESAQ=)ovOv)YpvaYWy$v^mppC89c@Y(?r=^tK58E5k#Bum?CDHDI zc5t0Q=LN)Jb*(&cv5G=Gxo9a@S_(tLrR6X==WaqIiFpznuP-jW(3*x=@|hL%T`MmB?oS6Y`6Uyp5u!ppapZZBuP z$<*gw`$@R{jRJvQ4IW$3Rx+!-BTvSjjI1hGpSnkPWS_^!gRsFuV3g^)NpG55&jUMn z#7U4QI4hWUSt2HJ^x(c=Z1@A&CJ7q-OK4}p-HEY z0{dBwH8F=nRB8dCFeAqf@JDT$_Ci#FLoQP%x6*??1_2AQu_ET%^r=A5)Z3eqGg2&f_#~(@0gFQ$X9XnnU>{@kq3dHO~LOt!D zgVl$mDe`Yg+7wVi2C*Yr64xGRgVj}7ev+6c2U??$B$4zEw3M8Z2CIlONCp{9v`nNn z0;{dHapPShlNu$|-iT#Dol9K=f>f>`7g(a!80EqgAY89ywa^YI3A4lya<~r#OL;b}87waq9*jBZ9cHzM6@ zk?u#m>yh_1BInj3=N^Cj97MJh&%+3-BSxiozd>@ZhF-;^E9u{gGMYCwcQLH$1!`v-+=AGAA|Qf))L#R z8F0gr?biSvCDYU`#~HF|arjju4&!*hKyhAh4M(mJ7q#7Z^IwTLlH&{CM+Gxq*4ZTv z<(A-|gYd+TOOm9Qq;uCBmZG~o#DAvZc|juId!CYxU3Z)0+vy`pXT|%1D2o@KlD1dm i$V+l~i-ca0E?m>K+Y^yGcTN)D`HCBr&OakjvF$(G2iDdA literal 0 HcmV?d00001 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,