diff --git a/lab-python-functions.ipynb b/lab-python-functions.ipynb index 7e1dcbd..ef159a7 100644 --- a/lab-python-functions.ipynb +++ b/lab-python-functions.ipynb @@ -28,10 +28,28 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "id": "df908bed-acc6-4b67-b33a-f3b1c564a49f", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter a list of numbers separated by a space: 2 3 4 5 6 7 8 3 4 22 3 4 5\n" + ] + }, + { + "data": { + "text/plain": [ + "['2', '3', '4', '5', '6', '7', '8', '22']" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "def get_unique_list(lst):\n", " \"\"\"\n", @@ -43,7 +61,13 @@ " Returns:\n", " list: A new list with unique elements from the input list.\n", " \"\"\"\n", - " # your code goes here\n" + "# your code goes here\n", + " lst = input(\"Enter a list of numbers separated by a space: \").split()\n", + " new_list = []\n", + " for i in lst:\n", + " if i not in new_list:\n", + " new_list.append(i)\n", + " return new_list" ] }, { @@ -60,10 +84,36 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 16, "id": "7d5c8e34-a116-4428-ab9d-e0e15e338fff", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdin", + "output_type": "stream", + "text": [ + "Enter a string with capital letters, lower letters, nbrs ..: Hello World\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Uppercase count: 2, Lowercase count: 9\n" + ] + }, + { + "ename": "NameError", + "evalue": "name 'ount_case' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[31m---------------------------------------------------------------------------\u001b[39m", + "\u001b[31mNameError\u001b[39m Traceback (most recent call last)", + "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[16]\u001b[39m\u001b[32m, line 33\u001b[39m\n\u001b[32m 31\u001b[39m string = \u001b[38;5;28minput\u001b[39m(\u001b[33m\"\u001b[39m\u001b[33mEnter a string with capital letters, lower letters, nbrs ..: \u001b[39m\u001b[33m\"\u001b[39m)\n\u001b[32m 32\u001b[39m \u001b[38;5;28mprint\u001b[39m(\u001b[33mf\u001b[39m\u001b[33m\"\u001b[39m\u001b[33mUppercase count: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mcount_case(string)[\u001b[32m0\u001b[39m]\u001b[38;5;132;01m}\u001b[39;00m\u001b[33m, Lowercase count: \u001b[39m\u001b[38;5;132;01m{\u001b[39;00mcount_case(string)[\u001b[32m1\u001b[39m]\u001b[38;5;132;01m}\u001b[39;00m\u001b[33m\"\u001b[39m)\n\u001b[32m---> \u001b[39m\u001b[32m33\u001b[39m \u001b[43mount_case\u001b[49m(string)\n", + "\u001b[31mNameError\u001b[39m: name 'ount_case' is not defined" + ] + } + ], "source": [ "def count_case(string):\n", " \"\"\"\n", @@ -75,7 +125,29 @@ " 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", + " #string = \"My name is Zidene Aourdache. I graduated from INELEC in 2002\"\n", + " my_tuple = ()\n", + " #Initiate counters\n", + " upper_letter = 0\n", + " lower_letter = 0\n", + " num = 0\n", + " other = 0\n", + " \n", + " for i in string:\n", + " if i.isupper():\n", + " upper_letter += 1\n", + " elif i.lower():\n", + " lower_letter += 1\n", + " elif i.isnumber():\n", + " num += 1\n", + " else:\n", + " other += 1\n", + " return upper_letter, lower_letter\n", + " \n", + "string = input(\"Enter a string with capital letters, lower letters, nbrs ..: \")\n", + "print(f\"Uppercase count: {count_case(string)[0]}, Lowercase count: {count_case(string)[1]}\")\n", + " " ] }, { @@ -92,10 +164,21 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 34, "id": "c15b91d4-cfd6-423b-9f36-76012b8792b8", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "6" + ] + }, + "execution_count": 34, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ "import string\n", "\n", @@ -110,6 +193,15 @@ " str: The sentence without any punctuation marks.\n", " \"\"\"\n", " # your code goes here\n", + " new_sentence =\"\"\n", + " for char in sentence:\n", + " if char not in string.punctuation:\n", + " new_sentence += char\n", + " \n", + " return new_sentence\n", + " \n", + " \n", + " \n", "\n", "def word_count(sentence):\n", " \"\"\"\n", @@ -122,30 +214,13 @@ " Returns:\n", " int: The number of words in the sentence.\n", " \"\"\"\n", - " # your code goes here" - ] - }, - { - "cell_type": "markdown", - "id": "52814e4d-7631-4c80-80b6-2b206f7e7419", - "metadata": {}, - "source": [ - "*For example, calling*\n", - "```python \n", - "word_count(\"Note : this is an example !!! Good day : )\")\n", - "``` \n", - "\n", - "*would give you as expected output: 7*" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "f7cfd32a-f559-47ff-81c1-2576bd4fe3bf", - "metadata": {}, - "outputs": [], - "source": [ - "# your code goes here" + "# your code goes here\n", + " clean_sentence = remove_punctuation(sentence)\n", + " clean_sentence = clean_sentence.strip()\n", + " words_number = len(clean_sentence.split())\n", + " return words_number\n", + "sentence = \"this is an example !!! Good day\"\n", + "word_count(sentence)" ] }, { @@ -168,12 +243,57 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 52, "id": "57f9afc7-8626-443c-9c3e-eb78ef503193", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "-120" + ] + }, + "execution_count": 52, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "# your code goes here" + "# your code goes here\n", + "def addition(a,b):\n", + " result = a + b\n", + " return result\n", + "\n", + "def subtraction(a,b):\n", + " result = a - b\n", + " return result\n", + " \n", + "def multiplication(a,b):\n", + " result = a * b\n", + " return result\n", + "\n", + "def division(a,b):\n", + " result = a / b\n", + " return result\n", + "\n", + "def calculate(operator, operand1, operand2):\n", + " \n", + " if operator == \"+\":\n", + " result = addition(operand1, operand2)\n", + " elif operator == \"-\":\n", + " result = subtraction(operand1, operand2)\n", + " elif operator == \"*\":\n", + " result = multiplication(operand1, operand2)\n", + " elif operator == \"/\" and operand2 != 0:\n", + " result = division(operand1, operand2)\n", + " else:\n", + " print(\"Error, cannot divide by 0!!\")\n", + "\n", + " return round(result, 2)\n", + " \n", + "\n", + "calculate(\"-\", 110, 230)\n", + " \n" ] }, { @@ -192,12 +312,58 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 62, "id": "ff3e816c-13ab-447d-a6f2-bb47a8fad2e2", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Calculation is not allowed!!\n", + "Operation not allowed!\n" + ] + } + ], "source": [ - "# your code goes here" + "# your coddef addition(a,b):\n", + "def addition(*args):\n", + " result = 0\n", + " for i in args:\n", + " result +=i\n", + " return result\n", + "\n", + "def subtraction(*args):\n", + " result = 0\n", + " for i in args:\n", + " result -=i\n", + " return result\n", + " \n", + "def multiplication(*args):\n", + " result = 1\n", + " for i in args:\n", + " result *=i\n", + " return result\n", + "\n", + "def division(a,b):\n", + " result = a / b\n", + " return result\n", + "\n", + "def calculate(operator, *args):\n", + " \n", + " if operator == \"+\":\n", + " result = addition(*args)\n", + " elif operator == \"-\":\n", + " result = subtraction(*args)\n", + " elif operator == \"*\":\n", + " result = multiplication(*args)\n", + " \n", + " else:\n", + " print(\"Calculation is not allowed!!\")\n", + " return \"Operation not allowed!\"\n", + " return round(result, 2)\n", + "\n", + "print(calculate(\"/\", 3, 5, 4, 5))" ] }, { @@ -210,7 +376,7 @@ }, { "cell_type": "markdown", - "id": "56780ce8-6610-4fb5-a4ef-22cd46af83d2", + "id": "44bee860-27c4-4225-a02d-459554fd528a", "metadata": {}, "source": [ "Moving the functions created in Challenge 1 to a Python file.\n", @@ -255,6 +421,30 @@ "\n" ] }, + { + "cell_type": "code", + "execution_count": 63, + "id": "6cf2c4cd-040e-4c5b-9364-2ae0f0bdb2f5", + "metadata": {}, + "outputs": [ + { + "ename": "NameError", + "evalue": "name 'word_count' is not defined", + "output_type": "error", + "traceback": [ + "\u001b[31m---------------------------------------------------------------------------\u001b[39m", + "\u001b[31mNameError\u001b[39m Traceback (most recent call last)", + "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[63]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m1\u001b[39m \u001b[38;5;28;01mfrom\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[34;01mfunctions\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mimport\u001b[39;00m get_unique_list_f\n\u001b[32m 2\u001b[39m get_unique_list_f(lst)\n", + "\u001b[36mFile \u001b[39m\u001b[32m~\\Desktop\\Trainings\\IRONHACK\\Week1\\Day2\\lab-python-functions-extra\\functions.py:89\u001b[39m\n\u001b[32m 87\u001b[39m \u001b[38;5;28;01mreturn\u001b[39;00m words_number\n\u001b[32m 88\u001b[39m sentence = \u001b[33m\"\u001b[39m\u001b[33mthis is an example !!! Good day\u001b[39m\u001b[33m\"\u001b[39m\n\u001b[32m---> \u001b[39m\u001b[32m89\u001b[39m \u001b[43mword_count\u001b[49m(sentence)\n", + "\u001b[31mNameError\u001b[39m: name 'word_count' is not defined" + ] + } + ], + "source": [ + "from functions import get_unique_list_f\n", + "get_unique_list_f(lst)\n" + ] + }, { "cell_type": "markdown", "id": "14f222c5-e7bb-4626-b45c-bd735001f768", @@ -326,9 +516,9 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python [conda env:base] *", "language": "python", - "name": "python3" + "name": "conda-base-py" }, "language_info": { "codemirror_mode": { @@ -340,7 +530,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.9" } }, "nbformat": 4,