diff --git a/homeworks/AT_intermediate-exercise_week3.ipynb b/homeworks/AT_intermediate-exercise_week3.ipynb new file mode 100644 index 0000000..c08f06a --- /dev/null +++ b/homeworks/AT_intermediate-exercise_week3.ipynb @@ -0,0 +1,1232 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Googling how to do something is NOT cheating. \n", + "# Googling how to do figure something out is ENCOURAGED. " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## #1 Figure out how to print the current date and time. \n", + "The solution should be in this format: 2021-08-05 11:56:30 (YYYY-MM-DD HH:MM:SS)\n", + "\n", + "Hint: You're going to have to import a library" + ] + }, + { + "cell_type": "code", + "execution_count": 50, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The current time is: 2025-08-07 10:19:41\n", + "Today is Thursday, Aug 07, 2025 and the current time is 10:19 AM\n" + ] + } + ], + "source": [ + "from datetime import datetime\n", + "\n", + "currentTime = datetime.now()\n", + "print(f'The current time is: {currentTime.strftime(\"%Y-%m-%d %H:%M:%S\")}')\n", + "\n", + "# Alternate Option\n", + "print(f'Today is {currentTime.strftime(\"%A, %b %d, %Y\")} and the current time is {currentTime.strftime(\"%I:%M %p\")}')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Here’s a list of common `strftime` format codes for future reference:\n", + "\n", + "- `%Y`: 4-digit year (e.g., 2025)\n", + "- `%y`: 2-digit year (e.g., 25)\n", + "- `%m`: 2-digit month (01-12)\n", + "- `%B`: Full month name (e.g., August)\n", + "- `%b`: Abbreviated month name (e.g., Aug)\n", + "- `%d`: Day of the month (01-31)\n", + "- `%-d`: Day of the month (1-31, no leading zero; not on Windows)\n", + "- `%A`: Full weekday name (e.g., Tuesday)\n", + "- `%a`: Abbreviated weekday name (e.g., Tue)\n", + "- `%H`: Hour (00-23, 24-hour clock)\n", + "- `%I`: Hour (01-12, 12-hour clock)\n", + "- `%p`: AM or PM\n", + "- `%M`: Minute (00-59)\n", + "- `%S`: Second (00-59)\n", + "- `%f`: Microsecond (000000-999999)\n", + "- `%z`: UTC offset (e.g., +0000)\n", + "- `%Z`: Time zone name\n", + "- `%j`: Day of the year (001-366)\n", + "- `%U`: Week number of the year (Sunday as first day, 00-53)\n", + "- `%W`: Week number of the year (Monday as first day, 00-53)\n", + "- `%c`: Locale’s date and time (e.g., Tue Aug 5 14:30:00 2025)\n", + "- `%x`: Locale’s date (e.g., 08/05/25)\n", + "- `%X`: Locale’s time (e.g., 14:30:00)\n", + "- `%%`: A literal '%' character" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## #2 Write a function that accepts the radius of a circle and returns the area of a circle. \n", + "The function for the area of a circle is pi * radius squared. " + ] + }, + { + "cell_type": "code", + "execution_count": 51, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The area of a circle with radius 14m is 615.75m²\n" + ] + } + ], + "source": [ + "import math\n", + "\n", + "def calculate_area_of_circle(radius):\n", + " return math.pi*(radius**2)\n", + "\n", + "radius = int(input(f'Enter the radius of the circle (in meters): '))\n", + "print(f'The area of a circle with radius {radius}m is {calculate_area_of_circle(radius):.2f}m²')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## #3 Write a function that takes a list of numbers and returns True if all number in the list are unique and returns False if the list contains duplicate numbers. \n", + "* Hint, maybe think of using a set()\n", + "\n", + "Input: input_list = [2,4,5,5,7,9]\n", + "\n", + "Expected Output: False" + ] + }, + { + "cell_type": "code", + "execution_count": 52, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "❌ NumList: [7, 3, 4, 4, 3] is NOT a unique list\n", + "✅ NumList: [1, 2, 3, 4, 5] is a unique list\n", + "✅ NumList: [0, 2, 1, 7, 9] is a unique list\n", + "❌ NumList: [3, 5, 1, 2, 1] is NOT a unique list\n", + "This List is Empty!\n" + ] + } + ], + "source": [ + "def is_unique_list(numList):\n", + " return len(set(numList)) == len(numList)\n", + "\n", + "numListColumn = [\n", + " [7, 3, 4, 4, 3],\n", + " [1, 2, 3, 4, 5],\n", + " [0, 2, 1, 7, 9],\n", + " [3, 5, 1, 2, 1],\n", + " []\n", + "]\n", + "\n", + "for numList in numListColumn:\n", + " if not numList:\n", + " print(f'This List is Empty!')\n", + " else:\n", + " print(f'✅ NumList: {numList} is a unique list' if is_unique_list(numList) else f'❌ NumList: {numList} is NOT a unique list')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## #4 Write a Python program to count the number of each character of a given text of a text file.\n", + "* Use the text file called 'lil-wayne.txt' in the data folder. \n", + "* Convert all the characters to uppercase first, then count them. \n", + "* Use a dictionary in which the keys of the dictionary are the characters, and the values are the counts of that character. \n", + "\n", + "Expected Result: `{'D': 74, 'W': 63, 'A': 211, 'Y': 44, 'N': 165, 'E': 274, ' ': 522, 'M': 77, 'I': 185, 'C': 60, 'H': 122, 'L': 126, 'R': 152, 'T': 180, 'J': 8, '.': 26, '(': 12, 'B': 59, 'O': 145, 'S': 152, 'P': 39, '2': 24, '7': 7, ',': 37, '1': 28, '9': 16, '8': 6, ')': 12, '[': 14, ']': 14, 'K': 13, 'F': 44, 'G': 53, 'X': 5, 'U': 66, 'V': 20, '3': 2, '4': 4, '5': 5, '6': 3, '0': 36, '\\n': 6, '-': 6, ';': 1, '!': 1, '\"': 8, \"'\": 3, '—': 1}`" + ] + }, + { + "cell_type": "code", + "execution_count": 53, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Counter({'E': 274, 'A': 211, 'I': 185, 'T': 180, 'N': 165, 'R': 152, 'S': 152, 'O': 145, 'L': 126, 'H': 122, 'M': 77, 'D': 74, 'U': 66, 'W': 63, 'C': 60, 'B': 59, 'G': 53, 'Y': 44, 'F': 44, 'P': 39, 'V': 20, 'K': 13, 'J': 8, 'X': 5})\n" + ] + } + ], + "source": [ + "from collections import Counter\n", + "\n", + "with open('../Week-03/intermediate/data/lil-wayne.txt', 'r') as file:\n", + " text = file.read().upper()\n", + " filtered_text = ''.join(char for char in text if char.isalpha())\n", + " letterDict = Counter(filtered_text)\n", + "\n", + "\n", + "print(letterDict)\n", + "\n", + "# Alternatively:\n", + "# letterDict = dict()\n", + "# with open('../Week-03/intermediate/data/lil-wayne.txt', 'r') as file:\n", + "# text = file.read().upper()\n", + "# for char in text:\n", + "# if char.isalpha():\n", + "# if char in letterDict:\n", + "# letterDict[char] += 1:\n", + "# else:\n", + "# letterDict[char] = 1\n", + "\n", + "# print(letterDict)\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# 4.5 Figure out how to sort the dictionary to find which character appears the most." + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The most prevalent character is: E with 274 appearances\n" + ] + } + ], + "source": [ + "most_prevalent = sorted(letterDict.items(), key=lambda item: item[1], reverse=True)[0]\n", + "print(f'The most prevalent character is: {most_prevalent[0]} with {most_prevalent[1]} appearances')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## #5 Using the list below...\n", + "* Remove the fourth number from the list.\n", + "* Take that number you just removed and multiply the last number of the list by that number. \n", + "* Append that number to the end of the list.\n", + "* Print the list\n", + "\n", + "INPUT: [1, 1, 2, 3, 5, 8, 13, 21]\n", + "\n", + "EXPECTED OUTPUT: [1, 1, 2, 5, 8, 13, 21, 63]\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 1, 2, 5, 8, 13, 21, 63]\n" + ] + } + ], + "source": [ + "list1 = [1, 1, 2, 3, 5, 8, 13, 21]\n", + "list1.append(list1[-1]*list1.pop(3))\n", + "print(list1)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## #6 Using the list below, make a new list that contains only the numbers from the original list that are divisible by three.\n", + "\n", + "* Input: original_list = [2, 3, 6, 8, 9, 15, 19, 21]\n", + "* Expected new list: [3, 6, 9, 15, 21]\n" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[3, 6, 9, 15, 21]\n" + ] + } + ], + "source": [ + "original_list = [2, 3, 6, 8, 9, 15, 19, 21]\n", + "factors_of_3 = [num for num in original_list if num%3 == 0]\n", + "print(factors_of_3)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## #7 Reverse the list below" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[21, 19, 15, 9, 8, 6, 3, 2]\n" + ] + } + ], + "source": [ + "original_list = [2, 3, 6, 8, 9, 15, 19, 21]\n", + "reversed_list = original_list[::-1]\n", + "print(reversed_list)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## #8 Using the list below, turn every item of the list into its square. \n", + "* Input original_list = [1, 2, 3, 4, 5, 6, 7]\n", + "* Expected new_list: [1, 4, 9, 16, 25, 36, 49]\n" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 4, 9, 16, 25, 36, 49]\n" + ] + } + ], + "source": [ + "original_list = [1, 2, 3, 4, 5, 6, 7]\n", + "squared_list = [num**2 for num in original_list]\n", + "print(squared_list)\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## #9 Given a two Python list. Iterate both lists simultaneously such that list1 should display item in original order and list2 in reverse order.\n", + "Given input:\n", + "```\n", + "list1 = [10, 20, 30, 40]\n", + "list2 = [100, 200, 300, 400]\n", + "```\n", + "\n", + "Expected Output:\n", + "```\n", + "10 400\n", + "20 300\n", + "30 200\n", + "40 100\n", + "```" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "10 400\n", + "20 300\n", + "30 200\n", + "40 100\n" + ] + } + ], + "source": [ + "list1 = [10, 20, 30, 40]\n", + "list2 = [100, 200, 300, 400]\n", + "\n", + "for item1, item2 in zip(list1, list2[::-1]):\n", + " print(item1, item2)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## #10 Remove empty strings from the list of strings.\n", + "Input: `list_of_strings = ['I', 'love', '', '', 'data', '', 'science', '!']`\n", + "\n", + "Expected Output: `['I', 'love', 'data', 'science', '!']`" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['I', 'love', 'data', 'science', '!']\n" + ] + } + ], + "source": [ + "list_of_strings = ['I', 'love', '', '', 'data', '', 'science', '!']\n", + "print(list(filter(None, list_of_strings))) \n", + "\n", + "# Alternatively:\n", + "# print([s for s in list_of_strings if s])\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## #11 Check if the value 999 exists in the dicitionary below. If so, print true, if not print false.\n", + "\n", + "Input: sample_dict = {'a': 100, 'b': 999, 'c': 300}\n", + "\n", + "Expected Output: True" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n" + ] + } + ], + "source": [ + "sample_dict = {'a': 100, 'b': 999, 'c': 300}\n", + "\n", + "print(999 in sample_dict.values())\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# #12 Fix all the follow broken code snippets below\n", + "(I got these examples from a great website, I will reference them later because they have the answers on them.)" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Greetings, hater of pirates!\n" + ] + } + ], + "source": [ + "greeting = \"Hello, possible pirate! What's the password?\"\n", + "password = 'admin'\n", + "message = greeting+password\n", + "if message in [\"Arrr!\"]:\n", + "\tprint(\"Go away, pirate.\")\n", + "else:\n", + "\tprint(\"Greetings, hater of pirates!\")" + ] + }, + { + "cell_type": "code", + "execution_count": 54, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1142.5\n" + ] + } + ], + "source": [ + "pages = 457\n", + "word_per_page = 250\n", + "number_of_pieces = 100\n", + "\n", + "each_chunk = (457 * 250)/100\n", + "print(each_chunk)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Extra Credit" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Using pandas, read the csv file in the data folder called `titianic.csv` and store it as a variable named `df`." + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
passengeridsurvivedpclassnamesexagesibspparchticketfarecabinembarked
0103Braund, Mr. Owen Harrismale22.010A/5 211717.2500NaNS
1211Cumings, Mrs. John Bradley (Florence Briggs Th...female38.010PC 1759971.2833C85C
2313Heikkinen, Miss. Lainafemale26.000STON/O2. 31012827.9250NaNS
3411Futrelle, Mrs. Jacques Heath (Lily May Peel)female35.01011380353.1000C123S
4503Allen, Mr. William Henrymale35.0003734508.0500NaNS
.......................................
88688702Montvila, Rev. Juozasmale27.00021153613.0000NaNS
88788811Graham, Miss. Margaret Edithfemale19.00011205330.0000B42S
88888903Johnston, Miss. Catherine Helen \"Carrie\"femaleNaN12W./C. 660723.4500NaNS
88989011Behr, Mr. Karl Howellmale26.00011136930.0000C148C
89089103Dooley, Mr. Patrickmale32.0003703767.7500NaNQ
\n", + "

891 rows × 12 columns

\n", + "
" + ], + "text/plain": [ + " passengerid survived pclass \\\n", + "0 1 0 3 \n", + "1 2 1 1 \n", + "2 3 1 3 \n", + "3 4 1 1 \n", + "4 5 0 3 \n", + ".. ... ... ... \n", + "886 887 0 2 \n", + "887 888 1 1 \n", + "888 889 0 3 \n", + "889 890 1 1 \n", + "890 891 0 3 \n", + "\n", + " name sex age sibsp \\\n", + "0 Braund, Mr. Owen Harris male 22.0 1 \n", + "1 Cumings, Mrs. John Bradley (Florence Briggs Th... female 38.0 1 \n", + "2 Heikkinen, Miss. Laina female 26.0 0 \n", + "3 Futrelle, Mrs. Jacques Heath (Lily May Peel) female 35.0 1 \n", + "4 Allen, Mr. William Henry male 35.0 0 \n", + ".. ... ... ... ... \n", + "886 Montvila, Rev. Juozas male 27.0 0 \n", + "887 Graham, Miss. Margaret Edith female 19.0 0 \n", + "888 Johnston, Miss. Catherine Helen \"Carrie\" female NaN 1 \n", + "889 Behr, Mr. Karl Howell male 26.0 0 \n", + "890 Dooley, Mr. Patrick male 32.0 0 \n", + "\n", + " parch ticket fare cabin embarked \n", + "0 0 A/5 21171 7.2500 NaN S \n", + "1 0 PC 17599 71.2833 C85 C \n", + "2 0 STON/O2. 3101282 7.9250 NaN S \n", + "3 0 113803 53.1000 C123 S \n", + "4 0 373450 8.0500 NaN S \n", + ".. ... ... ... ... ... \n", + "886 0 211536 13.0000 NaN S \n", + "887 0 112053 30.0000 B42 S \n", + "888 2 W./C. 6607 23.4500 NaN S \n", + "889 0 111369 30.0000 C148 C \n", + "890 0 370376 7.7500 NaN Q \n", + "\n", + "[891 rows x 12 columns]" + ] + }, + "execution_count": 24, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "import pandas as pd\n", + "df = pd.read_csv('../Week-03/intermediate/data/titanic.csv')\n", + "\n", + "df" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## I know we didn't cover this, but figure out how to find the mean age of the age column in your dataframe. " + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The mean age is: 29.70\n" + ] + } + ], + "source": [ + "mean_age = df[\"age\"].mean()\n", + "print(f'The mean age is: {mean_age:.2f}')" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## What was the oldest and youngest ages on the titanic?" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "The oldest passenger is: Mr. Algernon Henry Wilson Barkworth at 80.0 years old\n", + "The youngest passenger is: Master. Assad Alexander Thomas at 0.42 years old\n" + ] + } + ], + "source": [ + "youngest = df[\"age\"].min()\n", + "youngest_person = df[df[\"age\"] == youngest][\"name\"].tolist()\n", + "oldest = df[\"age\"].max()\n", + "oldest_person = df[df[\"age\"] == oldest][\"name\"].tolist()\n", + "\n", + "# Fun exercise, lets do string editing to make the names more natural\n", + "def naturalize_name(name):\n", + " if ',' in name:\n", + " surname, givenNames = name.split(',', 1) # First we'll split the name on the first instance of the comma\n", + " givenNames = givenNames.strip() # Then I'll remove whitespaces from around the given names\n", + " givenNames = givenNames.split() # Then split the given names into a list containing each name \n", + " # Just a side note, reassigning givenNames to givenNames.split() actually mutates this variable\n", + " # from a string to a list therefore string methods will no longer work\n", + "\n", + " # We'll check if the names contain titles, in this case, they all do, but it helps to be thorough\n", + " if givenNames and givenNames[0].endswith('.'): \n", + " # Separate the title\n", + " title = givenNames[0]\n", + " # Join the first and middle names together into a single string, we'll join each with a space\n", + " firstAndMiddleNames = ' '.join(givenNames[1:])\n", + " # Then return a string with the format we wanted, Title, first name, middle names and surname while removing any whitespace around the variable\n", + " return f\"{title} {firstAndMiddleNames} {surname}\".strip()\n", + " else:\n", + " # If there's no title (no '.' present) then we return a formatted string with the first name, middle names and surname\n", + " return f\"{givenNames} {surname}\".strip()\n", + " \n", + " # Otherwise if theres no ',' (which usually represents a malformed name or an empty string) then we just return the string exactly as we got it\n", + " return name\n", + "\n", + "# Long way of doing this:\n", + "if len(oldest_person) > 1:\n", + " print(f'The oldest passengers are: {\", \".join([naturalize_name(name) for name in oldest_person])} at {oldest} years old') \n", + "elif len(oldest_person) == 1:\n", + " print(f'The oldest passenger is: {naturalize_name(oldest_person[0])} at {oldest} years old')\n", + "if len(youngest_person) > 1:\n", + " print(f'The youngest passengers are: {\", \".join([naturalize_name(name) for name in youngest_person])} at {youngest} years old')\n", + "elif len(youngest_person) == 1:\n", + " print(f'The youngest passenger is: {naturalize_name(youngest_person[0])} at {youngest} years old')\n", + "else:\n", + " print(f'There are no passengers aboard')\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": {}, + "outputs": [ + { + "data": { + "text/html": [ + "
\n", + "\n", + "\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
passengeridsurvivedpclassnamesexagesibspparchticketfarecabinembarked
0103Mr. Owen Harris Braundmale22.010A/5 211717.2500NaNS
1211Mrs. John Bradley (Florence Briggs Thayer) Cum...female38.010PC 1759971.2833C85C
2313Miss. Laina Heikkinenfemale26.000STON/O2. 31012827.9250NaNS
3411Mrs. Jacques Heath (Lily May Peel) Futrellefemale35.01011380353.1000C123S
4503Mr. William Henry Allenmale35.0003734508.0500NaNS
.......................................
88688702Rev. Juozas Montvilamale27.00021153613.0000NaNS
88788811Miss. Margaret Edith Grahamfemale19.00011205330.0000B42S
88888903Miss. Catherine Helen \"Carrie\" JohnstonfemaleNaN12W./C. 660723.4500NaNS
88989011Mr. Karl Howell Behrmale26.00011136930.0000C148C
89089103Mr. Patrick Dooleymale32.0003703767.7500NaNQ
\n", + "

891 rows × 12 columns

\n", + "
" + ], + "text/plain": [ + " passengerid survived pclass \\\n", + "0 1 0 3 \n", + "1 2 1 1 \n", + "2 3 1 3 \n", + "3 4 1 1 \n", + "4 5 0 3 \n", + ".. ... ... ... \n", + "886 887 0 2 \n", + "887 888 1 1 \n", + "888 889 0 3 \n", + "889 890 1 1 \n", + "890 891 0 3 \n", + "\n", + " name sex age sibsp \\\n", + "0 Mr. Owen Harris Braund male 22.0 1 \n", + "1 Mrs. John Bradley (Florence Briggs Thayer) Cum... female 38.0 1 \n", + "2 Miss. Laina Heikkinen female 26.0 0 \n", + "3 Mrs. Jacques Heath (Lily May Peel) Futrelle female 35.0 1 \n", + "4 Mr. William Henry Allen male 35.0 0 \n", + ".. ... ... ... ... \n", + "886 Rev. Juozas Montvila male 27.0 0 \n", + "887 Miss. Margaret Edith Graham female 19.0 0 \n", + "888 Miss. Catherine Helen \"Carrie\" Johnston female NaN 1 \n", + "889 Mr. Karl Howell Behr male 26.0 0 \n", + "890 Mr. Patrick Dooley male 32.0 0 \n", + "\n", + " parch ticket fare cabin embarked \n", + "0 0 A/5 21171 7.2500 NaN S \n", + "1 0 PC 17599 71.2833 C85 C \n", + "2 0 STON/O2. 3101282 7.9250 NaN S \n", + "3 0 113803 53.1000 C123 S \n", + "4 0 373450 8.0500 NaN S \n", + ".. ... ... ... ... ... \n", + "886 0 211536 13.0000 NaN S \n", + "887 0 112053 30.0000 B42 S \n", + "888 2 W./C. 6607 23.4500 NaN S \n", + "889 0 111369 30.0000 C148 C \n", + "890 0 370376 7.7500 NaN Q \n", + "\n", + "[891 rows x 12 columns]" + ] + }, + "execution_count": 25, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Alternative Option:\n", + "# Apply the function to the name column to format the column in my prefered style\n", + "\n", + "df[\"name\"] = df[\"name\"].apply(naturalize_name)\n", + "df" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## How many people embarked from station 'S'?" + ] + }, + { + "cell_type": "code", + "execution_count": 45, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "644 passengers embarked at Station S\n" + ] + } + ], + "source": [ + "print(f'{len(df[df[\"embarked\"] == \"S\"])} passengers embarked at Station S')" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "644 embarked at Station S\n", + "168 embarked at Station C\n", + "77 embarked at Station Q\n" + ] + } + ], + "source": [ + "# Lets go a step further, and find the number of people that embarked from each station\n", + "\n", + "# Group the rows by each unique value in the embarked column, then counts the number of rows per group\n", + "# df.groupby(\"embarked\").size()\n", + "\n", + "# Or Simply count the occurrences of each unique value in the \"embarked\" column\n", + "# df[\"embarked\"].value_counts()\n", + "\n", + "# Now lets format it into a proper output\n", + "for station, count_of_passengers in df[\"embarked\"].value_counts().items():\n", + " print(f'{count_of_passengers} embarked at Station {station}')\n", + "\n", + "# Here's a breakdown:\n", + "# As stated above, .value_counts() counts the occurrences of each unique value in the \"embarked\" column\n", + "# We can then iterate over the Series that was created from .value_counts using .items() \n", + "# as (key/index, value) pairs...a dictionary if you will\n", + "# After that its just a matter of manipulating the dictionary as needed " + ] + } + ], + "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 +} 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 +}