diff --git a/main_completed.ipynb b/main_completed.ipynb new file mode 100644 index 0000000..1f9f097 --- /dev/null +++ b/main_completed.ipynb @@ -0,0 +1,665 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "C1a8C1b4arKa" + }, + "source": [ + "# Functions" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "ryCMGxWkarKb" + }, + "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": 6, + "metadata": { + "id": "uZZA76g2arKb" + }, + "outputs": [], + "source": [ + "import unittest\n", + "from testing import *" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "RnOXKgbGarKb" + }, + "source": [ + "## 1. Write a function that returns the greater of two numbers" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": { + "id": "v7vWV60iarKb" + }, + "outputs": [], + "source": [ + "def greater(a,b):\n", + " if a > b:\n", + " return a\n", + " else:\n", + " return b" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "Q58d875qarKb", + "outputId": "b30cf0d5-1349-4b7b-c72f-291ae2c16c2b" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stderr", + "text": [ + "....................................................................................................\n", + "----------------------------------------------------------------------\n", + "Ran 100 tests in 0.104s\n", + "\n", + "OK\n" + ] + } + ], + "source": [ + "# This will test your function\n", + "test_greater(greater)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "D2AUblmyarKb" + }, + "outputs": [], + "source": [ + "#your code here" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "yZtmeoqyarKb" + }, + "source": [ + "## 2. Now write a function that returns the largest element on a list" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "metadata": { + "id": "n_6sPEOtarKb" + }, + "outputs": [], + "source": [ + "def greatest(lst):\n", + " largest = lst[0]\n", + " for x in lst:\n", + " if x > largest:\n", + " largest = x\n", + " return largest" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "metadata": { + "id": "N_7c2aVLarKb", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "49f1718c-251c-4adf-a210-2f2410710b73" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stderr", + "text": [ + "....................................................................................................\n", + "----------------------------------------------------------------------\n", + "Ran 100 tests in 0.082s\n", + "\n", + "OK\n" + ] + } + ], + "source": [ + "# This will test your function\n", + "test_greatest(greatest)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Cf4DO9nyarKc" + }, + "source": [ + "## 3. Write a function that sums all the elements on a list" + ] + }, + { + "cell_type": "code", + "execution_count": 35, + "metadata": { + "id": "nYJ2NhXIarKc" + }, + "outputs": [], + "source": [ + "def sum_all(lst):\n", + " running_sum = 0\n", + " for x in lst:\n", + " running_sum = running_sum + x\n", + "\n", + " return running_sum" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "metadata": { + "scrolled": true, + "id": "zAIjAsW-arKc", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "d601d85b-b25d-465e-feae-7105efdffaff" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stderr", + "text": [ + "....................................................................................................\n", + "----------------------------------------------------------------------\n", + "Ran 100 tests in 0.085s\n", + "\n", + "OK\n" + ] + } + ], + "source": [ + "# This will test your function\n", + "test_sum(sum_all)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "nuWtU4lyarKc" + }, + "source": [ + "## 4. Write another function that multiplies all the elements on a list" + ] + }, + { + "cell_type": "code", + "execution_count": 43, + "metadata": { + "id": "VSI_48WuarKc" + }, + "outputs": [], + "source": [ + "def mult_all(lst):\n", + " running_sum1 = 1\n", + " for x in lst:\n", + " running_sum1 = x * running_sum1\n", + "\n", + " return running_sum1" + ] + }, + { + "cell_type": "code", + "execution_count": 44, + "metadata": { + "scrolled": true, + "id": "MO5CnIM2arKc", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "collapsed": true, + "outputId": "380f6eeb-fafe-41d7-9e8d-8f3eb0e2d295" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stderr", + "text": [ + "....................................................................................................\n", + "----------------------------------------------------------------------\n", + "Ran 100 tests in 0.093s\n", + "\n", + "OK\n" + ] + } + ], + "source": [ + "# This will test your function\n", + "test_mult(mult_all)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "kpdukrf1arKc" + }, + "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": 59, + "metadata": { + "id": "f_t3PWC7arKc" + }, + "outputs": [], + "source": [ + "def oper_all(arr, oper = \"*\"):\n", + " if oper == \"*\":\n", + " multisum2 = 1\n", + " for x in arr:\n", + " multisum2 = multisum2 * x\n", + " return multisum2\n", + " else:\n", + " sumsum2 = 0\n", + " for x in arr:\n", + " sumsum2 = sumsum2 + x\n", + " return sumsum2\n" + ] + }, + { + "cell_type": "code", + "execution_count": 60, + "metadata": { + "id": "Y5men7BLarKc", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "d58a51c8-97d4-4f10-a902-2a55b053c3c3" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stderr", + "text": [ + "....................................................................................................\n", + "----------------------------------------------------------------------\n", + "Ran 100 tests in 0.069s\n", + "\n", + "OK\n" + ] + } + ], + "source": [ + "# This will test your function\n", + "test_operations(oper_all)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "AXd8NH65arKc" + }, + "source": [ + "## 6. Write a function that returns the factorial of a number." + ] + }, + { + "cell_type": "code", + "execution_count": 61, + "metadata": { + "id": "4zkhR-9marKc" + }, + "outputs": [], + "source": [ + "def factorial(n):\n", + " result = 1\n", + " for x in range(1, n+1):\n", + " result = result * x\n", + " return result" + ] + }, + { + "cell_type": "code", + "execution_count": 62, + "metadata": { + "id": "-bHXO9UDarKc" + }, + "outputs": [], + "source": [ + "#factorial formula\n", + "#n! = n * ( n - 1 ) *...*1\n", + "\n", + "# This code defines a function called \"factorial\" which takes an input \"n\". The function uses a for loop to iterate through the range of numbers\n", + "# from 1 to n+1. For each number in that range, it multiplies the current value of x by the number in the range. At the end of the loop,\n", + "# the function returns the final value of x, which will be the factorial of the input number \"n\".\n", + "\n", + "# The Factorial of a positive integer n is the product of all positive integers less than or equal to n.\n", + "# For example, the factorial of 6 (written \"6!\") is 6 * 5 * 4 * 3 * 2 * 1 = 720.\n", + "\n", + "# So this function takes an input of any positive integer, and returns the factorial of that number." + ] + }, + { + "cell_type": "code", + "execution_count": 63, + "metadata": { + "id": "XkND1mh-arKc", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "93cf7539-25d0-44c2-af5c-3ce0a0f63e9e" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stderr", + "text": [ + "....................................................................................................\n", + "----------------------------------------------------------------------\n", + "Ran 100 tests in 0.099s\n", + "\n", + "OK\n" + ] + } + ], + "source": [ + "# This will test your function\n", + "test_factorial(factorial)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "W5Zp8806arKc" + }, + "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": null, + "metadata": { + "id": "lZulnG4BarKc" + }, + "outputs": [], + "source": [ + "def unique(lst_un):\n", + "#your code here" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "Jj8sluNxarKc" + }, + "outputs": [], + "source": [ + "# This will test your function\n", + "test_unique(unique)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "7EVT1bYdarKc" + }, + "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": null, + "metadata": { + "id": "8iycj7diarKc" + }, + "outputs": [], + "source": [ + "def mode_counter(arr):\n", + "#your code here" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "HYkUu-kTarKd" + }, + "outputs": [], + "source": [ + "# This will test your function\n", + "test_mode(mode_counter)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "7rspZMQKarKd" + }, + "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": { + "id": "6uykkZ29arKd" + }, + "outputs": [], + "source": [ + "def st_dev(list_sd):\n", + "#your code here" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "Sr-0JCU4arKd" + }, + "outputs": [], + "source": [ + "# This will test your function\n", + "test_stdev(st_dev)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "RzdF7cGzarKd" + }, + "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": null, + "metadata": { + "id": "16ex9XfXarKd" + }, + "outputs": [], + "source": [ + "def pangram(string):\n", + "#your code here" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "g_HB7bYFarKd" + }, + "outputs": [], + "source": [ + "# This will test your function\n", + "test_pangram(pangram)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Em0kG518arKd" + }, + "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": null, + "metadata": { + "id": "c3tsxz4LarKd" + }, + "outputs": [], + "source": [ + "def sort_alpha(string):\n", + "#your code here" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "NnOHkfpHarKd" + }, + "outputs": [], + "source": [ + "# This will test your function\n", + "test_alpha(sort_alpha)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "LYjn8Lg3arKd" + }, + "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." + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "id": "R8Kb7qfTarKd" + }, + "outputs": [], + "source": [ + "def check_pass(password): # took me almost 2 hours to get testing running\n", + " # Länge prüfen\n", + " if len(password) < 8:\n", + " return False\n", + "\n", + " lower = False # had help from ChatGPT in this case, didn't get it\n", + " upper = False\n", + " digit = False\n", + " special = False\n", + "\n", + " specials = \"!@#$%^&*()-_=+[]{};:,.?/<>\"\n", + "\n", + " for char in password:\n", + " if char >= \"a\" and char <= \"z\":\n", + " lower = True\n", + " elif char >= \"A\" and char <= \"Z\":\n", + " upper = True\n", + " elif char >= \"0\" and char <= \"9\":\n", + " digit = True\n", + " elif char in specials:\n", + " special = True\n", + "\n", + " return lower and upper and digit and special\n", + "" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": { + "id": "WWWFBjyKarKd", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "36c90600-c557-4559-a197-73fa591d3d8d" + }, + "outputs": [ + { + "output_type": "stream", + "name": "stderr", + "text": [ + "....................................................................................................\n", + "----------------------------------------------------------------------\n", + "Ran 100 tests in 0.072s\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.13" + }, + "vscode": { + "interpreter": { + "hash": "aee8b7b246df8f9039afb4144a1f6fd8d2ca17a180786b69acc140d282b71a49" + } + }, + "colab": { + "provenance": [] + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} \ No newline at end of file