diff --git a/homeworks/AT_intermediate_exercise.ipynb b/homeworks/AT_intermediate_exercise.ipynb new file mode 100644 index 0000000..5a39c8e --- /dev/null +++ b/homeworks/AT_intermediate_exercise.ipynb @@ -0,0 +1,325 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## ❌ DO NOT EDIT - MAKE A COPY\n", + "# Q1: Alphabet Slices\n", + "* Store the first ten letters of the alphabet in a list.\n", + "* Use a slice to print out the first three letters of the alphabet.\n", + "* Use a slice to print out any three letters from the middle of your list." + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "* Store the first ten letters of the alphabet in a list: ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']\n", + "* Use a slice to print out the first three letters of the alphabet: ['a', 'b', 'c']\n", + "* Use a slice to print out any three letters from the middle of your list: ['e', 'f', 'g']\n" + ] + } + ], + "source": [ + "# Solve Alphabet slices here. \n", + "## Extra Credit: Do this without 'hard coding' the alpahbet.\n", + "import string\n", + "\n", + "my_first_10_letters = list(string.ascii_lowercase[0:10])\n", + "print(f'* Store the first ten letters of the alphabet in a list: {my_first_10_letters}')\n", + "\n", + "print(f'* Use a slice to print out the first three letters of the alphabet: {my_first_10_letters[0:3]}')\n", + "midpoint = (len(my_first_10_letters) // 2) - 1\n", + "print(f'* Use a slice to print out any three letters from the middle of your list: {my_first_10_letters[midpoint:midpoint+3]}')\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Q2: Covert all the rapper names to title case and save them into a new different list. \n", + "Example: **lil wayne** becomes **Lil Wayne**" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['Lil Wayne', 'Nicki Minaj', 'Drake']\n" + ] + } + ], + "source": [ + "# Solve rapper names here\n", + "rappers = ['lil wayne', 'nicki minaj', 'drake']\n", + "\n", + "capitalized_rappers = list(map(str.title, rappers))\n", + "print(capitalized_rappers)\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Q3: Write a function that takes a number and returns:\n", + "* True if the input number is even.\n", + "* False if the input number is odd." + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n" + ] + } + ], + "source": [ + "# Solve problem here\n", + "\n", + "def even_odd(lst):\n", + " return lst%2 == 0\n", + "\n", + "print(even_odd(16))\n", + " " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Q4: Find the sum and the average of this list of numbers.\n", + "\n", + "Try doing this using a loop. Then try doing this without using a loop. " + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "list_sum_looped: 353,\n", + "list_sum: 353,\n", + "list_avg_looped: 35.3,\n", + "list_avg: 35.3\n" + ] + } + ], + "source": [ + "# Solve problem here:\n", + "import statistics\n", + "\n", + "my_list = [1, 5, 10, 55, 88, 44, 42, 50, 20, 38]\n", + "\n", + "list_sum_looped = sum(num for num in my_list)\n", + "list_sum = sum(my_list)\n", + "\n", + "list_avg_looped = list_sum_looped / len(my_list)\n", + "list_avg = statistics.mean(my_list)\n", + "\n", + "\n", + "# Keep this as your last line in this cell.\n", + "print(f\"list_sum_looped: {list_sum_looped},\\nlist_sum: {list_sum},\\nlist_avg_looped: {list_avg_looped},\\nlist_avg: {list_avg}\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Q5: \n", + "## Write a function that takes a list and returns a new list that has all the duplicates removed.\n", + "\n", + "Example input and expected output:\n", + "- input = `[\"Michele\", \"Robin\", \"Sara\", \"Michele\"]`\n", + "- expected output = `['Michele', 'Robin', 'Sara']`\n" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'Michele', 'Robin', 'Sara'}\n" + ] + } + ], + "source": [ + "# Solve problem here:\n", + "\n", + "names = [\"Michele\", \"Robin\", \"Sara\", \"Michele\"]\n", + "print(set(names))\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Q6: Write a function that takes a list of numbers \n", + "(for example, `a = [5, 10, 15, 20, 25]`) and returns a new list of only the first and last elements of the given list.\n", + "\n", + "Example input and expected output:\n", + "- input = `[5, 10, 15, 20, 25]`\n", + "- expected output = `[5, 25]`" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[5, 25]\n" + ] + } + ], + "source": [ + "# Solve problem here:\n", + "input_list = [5, 10, 99, 20, 25]\n", + "def first_last(input_list):\n", + " return [input_list[0], input_list[-1]]\n", + "\n", + "print(first_last(input_list))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Q7: \n", + "## Implement a function that takes as input three variables, and returns the largest of the three. \n", + "### Try doing this without using the `max()` function!\n", + "\n", + "_**Note:** all three input numbers will always be different, no need to account for a tie._\n", + "\n", + "Example input and expected output:\n", + "- input: `your_function(1, 5, 10)`\n", + "- expected output: `10`" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": {}, + "outputs": [], + "source": [ + "# Solve Problem here:\n", + "\n", + "def my_max(a, b, c):\n", + " # Fill in your code below and return max value of a, b, c\n", + " if a > b and a > c:\n", + " return a\n", + " elif b > c:\n", + " return b\n", + " else:\n", + " return c\n", + " " + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "10\n" + ] + } + ], + "source": [ + "# Test to see if your function works properly.\n", + "print(my_max(1, 5, 10))\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Q8: Write a function that takes a number as input and returns the following:\n", + "* If the input is divisible by three, return `'fizz'`\n", + "* If the input is divisible by five, return `'buzz'`\n", + "* If the input is divisible by three and by five, return `'fizzbuzz'`\n", + "* If the input is not divisible by three or five, return `None`." + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "fizzbuzz\n" + ] + } + ], + "source": [ + "# Solve Problem fizzbuzz here:\n", + "\n", + "def fizz_buzz(num):\n", + " if num%3 == 0 and num%5==0:\n", + " return 'fizzbuzz'\n", + " elif num%3 == 0:\n", + " return 'fizz'\n", + " elif num%5 == 0:\n", + " return 'buzz'\n", + " else:\n", + " return 'None'\n", + " \n", + "print(fizz_buzz(15))" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": ".venv", + "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.13.4" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +}