diff --git a/.ipynb_checkpoints/main-checkpoint.ipynb b/.ipynb_checkpoints/main-checkpoint.ipynb new file mode 100644 index 0000000..8ad48f8 --- /dev/null +++ b/.ipynb_checkpoints/main-checkpoint.ipynb @@ -0,0 +1,618 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Functions" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "On this lab we will put to practice some of the concepts we have learned on this past few days.\n", + "\n", + "`NOTE: On this lab you should try to write all the functions yourself using only the most basic of python syntax and without functions such as len, count, sum, max, min, in, etc. Give it a try. 🧑ðŸŧ‍ðŸ’ŧðŸ‘ĐðŸŧ‍ðŸ’ŧ`\n", + "\n", + "The cell after each exercise contains a few tests to check if your function works as expected." + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "from mod.testing import *\n", + "import unittest" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 1. Write a function that returns the greater of two numbers" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "def greater(a,b):\n", + " if a > b:\n", + " return a\n", + " else:\n", + " return b" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "....................................................................................................\n", + "----------------------------------------------------------------------\n", + "Ran 100 tests in 0.052s\n", + "\n", + "OK\n" + ] + } + ], + "source": [ + "# This will test your function \n", + "test_greater(greater)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 2. Now write a function that returns the largest element on a list" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [], + "source": [ + "def greatest(arr):\n", + " arr.sort()\n", + " return arr[-1]\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "....................................................................................................\n", + "----------------------------------------------------------------------\n", + "Ran 100 tests in 0.054s\n", + "\n", + "OK\n" + ] + } + ], + "source": [ + "# This will test your function \n", + "test_greatest(greatest)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 3. Write a function that sums all the elements on a list" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [], + "source": [ + "def sum_all(arr):\n", + " sum_ = 0\n", + " for i in arr:\n", + " sum_ += i\n", + " return sum_" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "....................................................................................................\n", + "----------------------------------------------------------------------\n", + "Ran 100 tests in 0.048s\n", + "\n", + "OK\n" + ] + } + ], + "source": [ + "# This will test your function \n", + "test_sum(sum_all)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 4. Write another function that multiplies all the elements on a list" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [], + "source": [ + "def mult_all(arr):\n", + " mult_ = 1\n", + " for i in arr:\n", + " mult_ *= i\n", + " return mult_\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "....................................................................................................\n", + "----------------------------------------------------------------------\n", + "Ran 100 tests in 0.042s\n", + "\n", + "OK\n" + ] + } + ], + "source": [ + "# This will test your function \n", + "test_mult(mult_all)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 5. Now combine those two ideas and write a function that receives a list and either \"+\" or \"*\" and outputs acordingly" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [], + "source": [ + "def oper_all(arr, oper):\n", + " sum_ = 0\n", + " mult_ = 1\n", + " if oper == '+':\n", + " for i in arr: \n", + " sum_ += i \n", + " return sum_\n", + " elif oper == '*':\n", + " for i in arr: \n", + " mult_ *= i\n", + " return mult_\n", + " else:\n", + " pass\n", + " \n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "....................................................................................................\n", + "----------------------------------------------------------------------\n", + "Ran 100 tests in 0.047s\n", + "\n", + "OK\n" + ] + } + ], + "source": [ + "# This will test your function \n", + "test_operations(oper_all)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 6. Write a function that returns the factorial of a number." + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [], + "source": [ + "def factorial(n):\n", + " fact = 1\n", + " for i in range(1, n+1):\n", + " fact *= i\n", + " return fact\n", + " \n", + " \n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "....................................................................................................\n", + "----------------------------------------------------------------------\n", + "Ran 100 tests in 0.046s\n", + "\n", + "OK\n" + ] + } + ], + "source": [ + "# This will test your function \n", + "test_factorial(factorial)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 7. Write a function that takes a list and returns a list of the unique values.\n", + "\n", + "`NOTE: You cannot use set. ðŸĪ”`" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [], + "source": [ + "def unique(arr):\n", + " unique = []\n", + " for item in arr:\n", + " if item not in unique:\n", + " unique.append(item)\n", + " return unique " + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "....................................................................................................\n", + "----------------------------------------------------------------------\n", + "Ran 100 tests in 0.103s\n", + "\n", + "OK\n" + ] + } + ], + "source": [ + "# This will test your function \n", + "test_unique(unique)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 8. Write a function that returns the mode of a list, i.e.: the element that appears the most times.\n", + "`NOTE: You should not use count... 🧐`" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [], + "source": [ + "def mode_counter(arr):\n", + " counter = 0\n", + " counter_max = 0\n", + " mode = 0\n", + " for i in arr:\n", + " #print('i = ', i)\n", + " for element in arr:\n", + " #print('element = ', element)\n", + " if i == element:\n", + " counter += 1\n", + " #print('counter = ', counter)\n", + " if counter > counter_max:\n", + " counter_max = counter\n", + " mode = i\n", + " #print('counter_max = ', counter_max)\n", + " #print('mode = ', mode)\n", + " counter = 0\n", + " return mode\n", + " \n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "....................................................................................................\n", + "----------------------------------------------------------------------\n", + "Ran 100 tests in 0.159s\n", + "\n", + "OK\n" + ] + } + ], + "source": [ + "# This will test your function \n", + "test_mode(mode_counter)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 9. Write a function that calculates the standard deviation of a list.\n", + "`NOTE: Do not use any libraries or already built functions. 😉`" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from statistics import stdev\n", + "def st_dev(arr):\n", + " pass" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# This will test your function \n", + "test_stdev(st_dev)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 10. Write a function to check if a string is a pangram, i.e.: if it contains all the letters of the alphabet at least once. Mind that the strings may contain characters that are not letters." + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "metadata": {}, + "outputs": [], + "source": [ + "def pangram(string):\n", + " alphabet = \"abcdefghijklmnopqrstuvwxyz\"\n", + " for character in alphabet:\n", + " if character not in string.lower():\n", + " return False\n", + " \n", + " return True\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 34, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "..............................\n", + "----------------------------------------------------------------------\n", + "Ran 30 tests in 0.017s\n", + "\n", + "OK\n" + ] + } + ], + "source": [ + "# This will test your function \n", + "test_pangram(pangram)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 11. Write a function that receives a string of comma separated words and returns a string of comma separated words sorted alphabetically.\n", + "\n", + "`NOTE: You may use sorted but not split and definitely no join! ðŸĪŠ`" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "metadata": {}, + "outputs": [], + "source": [ + "def sort_alpha(string):\n", + " string+=\",\"\n", + " newlist=[]\n", + " word=\"\"\n", + " newword=\"\"\n", + " for letter in string.lower():\n", + " if letter ==\",\":\n", + " \n", + " newlist.append(word)\n", + " word=\"\"\n", + " else:\n", + " word+=letter\n", + " \n", + " newlist.sort()\n", + " \n", + " for elem in newlist:\n", + " newword+=\",\"+elem\n", + " \n", + " newword2=newword[1:]\n", + " return newword2" + ] + }, + { + "cell_type": "code", + "execution_count": 37, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "....................................................................................................\n", + "----------------------------------------------------------------------\n", + "Ran 100 tests in 0.061s\n", + "\n", + "OK\n" + ] + } + ], + "source": [ + "# This will test your function \n", + "test_alpha(sort_alpha)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 12. Write a function to check if a given password is strong (at least 8 characters, at least one lower case, at least one upper case, at least one number and at least one special character). It should output True if strong and False if not.\n", + "`Valid special characters: # @ ! $ % & ( ) ^ * [ ] { }`" + ] + }, + { + "cell_type": "code", + "execution_count": 38, + "metadata": {}, + "outputs": [], + "source": [ + "def check_pass(string):\n", + " count_lower = 0\n", + " count_upper = 0\n", + " count_special = 0\n", + " count_number = 0\n", + " \n", + " lower = \"abcdefghijklmnopqrstuvwxyz\"\n", + " upper = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'\n", + " special = \"#@!$%&()^*[]{}\"\n", + " number = '1234567890'\n", + " \n", + " for i in string:\n", + " if i in lower:\n", + " count_lower += 1\n", + " elif i in upper:\n", + " count_upper += 1\n", + " elif i in special:\n", + " count_special += 1\n", + " elif i in number:\n", + " count_number += 1\n", + " else:\n", + " return False\n", + " \n", + " return count_lower >= 1 and count_upper >= 1 and count_special >= 1 and count_number >= 1 and len(string) >= 8" + ] + }, + { + "cell_type": "code", + "execution_count": 39, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "....................................................................................................\n", + "----------------------------------------------------------------------\n", + "Ran 100 tests in 0.067s\n", + "\n", + "OK\n" + ] + } + ], + "source": [ + "# This will test your function \n", + "test_pass(check_pass)" + ] + } + ], + "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.12" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} diff --git a/main.ipynb b/main.ipynb index 73f285a..8ad48f8 100644 --- a/main.ipynb +++ b/main.ipynb @@ -20,7 +20,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "metadata": {}, "outputs": [], "source": [ @@ -37,19 +37,34 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "def greater(a,b):\n", - " pass" + " if a > b:\n", + " return a\n", + " else:\n", + " return b" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "....................................................................................................\n", + "----------------------------------------------------------------------\n", + "Ran 100 tests in 0.052s\n", + "\n", + "OK\n" + ] + } + ], "source": [ "# This will test your function \n", "test_greater(greater)" @@ -64,19 +79,33 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "def greatest(arr):\n", - " pass" + " arr.sort()\n", + " return arr[-1]\n", + " " ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "....................................................................................................\n", + "----------------------------------------------------------------------\n", + "Ran 100 tests in 0.054s\n", + "\n", + "OK\n" + ] + } + ], "source": [ "# This will test your function \n", "test_greatest(greatest)" @@ -91,19 +120,34 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "def sum_all(arr):\n", - " pass" + " sum_ = 0\n", + " for i in arr:\n", + " sum_ += i\n", + " return sum_" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "....................................................................................................\n", + "----------------------------------------------------------------------\n", + "Ran 100 tests in 0.048s\n", + "\n", + "OK\n" + ] + } + ], "source": [ "# This will test your function \n", "test_sum(sum_all)" @@ -118,19 +162,35 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "def mult_all(arr):\n", - " pass" + " mult_ = 1\n", + " for i in arr:\n", + " mult_ *= i\n", + " return mult_\n", + " " ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "....................................................................................................\n", + "----------------------------------------------------------------------\n", + "Ran 100 tests in 0.042s\n", + "\n", + "OK\n" + ] + } + ], "source": [ "# This will test your function \n", "test_mult(mult_all)" @@ -145,19 +205,44 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "def oper_all(arr, oper):\n", - " pass" + " sum_ = 0\n", + " mult_ = 1\n", + " if oper == '+':\n", + " for i in arr: \n", + " sum_ += i \n", + " return sum_\n", + " elif oper == '*':\n", + " for i in arr: \n", + " mult_ *= i\n", + " return mult_\n", + " else:\n", + " pass\n", + " \n", + " " ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "....................................................................................................\n", + "----------------------------------------------------------------------\n", + "Ran 100 tests in 0.047s\n", + "\n", + "OK\n" + ] + } + ], "source": [ "# This will test your function \n", "test_operations(oper_all)" @@ -172,19 +257,39 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "def factorial(n):\n", - " pass" + " fact = 1\n", + " for i in range(1, n+1):\n", + " fact *= i\n", + " return fact\n", + " \n", + " \n", + " " ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 13, + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "....................................................................................................\n", + "----------------------------------------------------------------------\n", + "Ran 100 tests in 0.046s\n", + "\n", + "OK\n" + ] + } + ], "source": [ "# This will test your function \n", "test_factorial(factorial)" @@ -201,19 +306,35 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 14, "metadata": {}, "outputs": [], "source": [ "def unique(arr):\n", - " pass" + " unique = []\n", + " for item in arr:\n", + " if item not in unique:\n", + " unique.append(item)\n", + " return unique " ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "....................................................................................................\n", + "----------------------------------------------------------------------\n", + "Ran 100 tests in 0.103s\n", + "\n", + "OK\n" + ] + } + ], "source": [ "# This will test your function \n", "test_unique(unique)" @@ -229,19 +350,49 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "def mode_counter(arr):\n", - " pass" + " counter = 0\n", + " counter_max = 0\n", + " mode = 0\n", + " for i in arr:\n", + " #print('i = ', i)\n", + " for element in arr:\n", + " #print('element = ', element)\n", + " if i == element:\n", + " counter += 1\n", + " #print('counter = ', counter)\n", + " if counter > counter_max:\n", + " counter_max = counter\n", + " mode = i\n", + " #print('counter_max = ', counter_max)\n", + " #print('mode = ', mode)\n", + " counter = 0\n", + " return mode\n", + " \n", + " " ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "....................................................................................................\n", + "----------------------------------------------------------------------\n", + "Ran 100 tests in 0.159s\n", + "\n", + "OK\n" + ] + } + ], "source": [ "# This will test your function \n", "test_mode(mode_counter)" @@ -285,19 +436,37 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 33, "metadata": {}, "outputs": [], "source": [ "def pangram(string):\n", - " pass" + " alphabet = \"abcdefghijklmnopqrstuvwxyz\"\n", + " for character in alphabet:\n", + " if character not in string.lower():\n", + " return False\n", + " \n", + " return True\n", + " " ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 34, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "..............................\n", + "----------------------------------------------------------------------\n", + "Ran 30 tests in 0.017s\n", + "\n", + "OK\n" + ] + } + ], "source": [ "# This will test your function \n", "test_pangram(pangram)" @@ -314,19 +483,49 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 36, "metadata": {}, "outputs": [], "source": [ "def sort_alpha(string):\n", - " pass" + " string+=\",\"\n", + " newlist=[]\n", + " word=\"\"\n", + " newword=\"\"\n", + " for letter in string.lower():\n", + " if letter ==\",\":\n", + " \n", + " newlist.append(word)\n", + " word=\"\"\n", + " else:\n", + " word+=letter\n", + " \n", + " newlist.sort()\n", + " \n", + " for elem in newlist:\n", + " newword+=\",\"+elem\n", + " \n", + " newword2=newword[1:]\n", + " return newword2" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 37, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "....................................................................................................\n", + "----------------------------------------------------------------------\n", + "Ran 100 tests in 0.061s\n", + "\n", + "OK\n" + ] + } + ], "source": [ "# This will test your function \n", "test_alpha(sort_alpha)" @@ -342,19 +541,53 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 38, "metadata": {}, "outputs": [], "source": [ "def check_pass(string):\n", - " pass" + " count_lower = 0\n", + " count_upper = 0\n", + " count_special = 0\n", + " count_number = 0\n", + " \n", + " lower = \"abcdefghijklmnopqrstuvwxyz\"\n", + " upper = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'\n", + " special = \"#@!$%&()^*[]{}\"\n", + " number = '1234567890'\n", + " \n", + " for i in string:\n", + " if i in lower:\n", + " count_lower += 1\n", + " elif i in upper:\n", + " count_upper += 1\n", + " elif i in special:\n", + " count_special += 1\n", + " elif i in number:\n", + " count_number += 1\n", + " else:\n", + " return False\n", + " \n", + " return count_lower >= 1 and count_upper >= 1 and count_special >= 1 and count_number >= 1 and len(string) >= 8" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], + "execution_count": 39, + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "....................................................................................................\n", + "----------------------------------------------------------------------\n", + "Ran 100 tests in 0.067s\n", + "\n", + "OK\n" + ] + } + ], "source": [ "# This will test your function \n", "test_pass(check_pass)" @@ -363,7 +596,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -377,7 +610,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.8.6" + "version": "3.9.12" } }, "nbformat": 4,