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", + " | passengerid | \n", + "survived | \n", + "pclass | \n", + "name | \n", + "sex | \n", + "age | \n", + "sibsp | \n", + "parch | \n", + "ticket | \n", + "fare | \n", + "cabin | \n", + "embarked | \n", + "
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | \n", + "1 | \n", + "0 | \n", + "3 | \n", + "Braund, Mr. Owen Harris | \n", + "male | \n", + "22.0 | \n", + "1 | \n", + "0 | \n", + "A/5 21171 | \n", + "7.2500 | \n", + "NaN | \n", + "S | \n", + "
| 1 | \n", + "2 | \n", + "1 | \n", + "1 | \n", + "Cumings, Mrs. John Bradley (Florence Briggs Th... | \n", + "female | \n", + "38.0 | \n", + "1 | \n", + "0 | \n", + "PC 17599 | \n", + "71.2833 | \n", + "C85 | \n", + "C | \n", + "
| 2 | \n", + "3 | \n", + "1 | \n", + "3 | \n", + "Heikkinen, Miss. Laina | \n", + "female | \n", + "26.0 | \n", + "0 | \n", + "0 | \n", + "STON/O2. 3101282 | \n", + "7.9250 | \n", + "NaN | \n", + "S | \n", + "
| 3 | \n", + "4 | \n", + "1 | \n", + "1 | \n", + "Futrelle, Mrs. Jacques Heath (Lily May Peel) | \n", + "female | \n", + "35.0 | \n", + "1 | \n", + "0 | \n", + "113803 | \n", + "53.1000 | \n", + "C123 | \n", + "S | \n", + "
| 4 | \n", + "5 | \n", + "0 | \n", + "3 | \n", + "Allen, Mr. William Henry | \n", + "male | \n", + "35.0 | \n", + "0 | \n", + "0 | \n", + "373450 | \n", + "8.0500 | \n", + "NaN | \n", + "S | \n", + "
| ... | \n", + "... | \n", + "... | \n", + "... | \n", + "... | \n", + "... | \n", + "... | \n", + "... | \n", + "... | \n", + "... | \n", + "... | \n", + "... | \n", + "... | \n", + "
| 886 | \n", + "887 | \n", + "0 | \n", + "2 | \n", + "Montvila, Rev. Juozas | \n", + "male | \n", + "27.0 | \n", + "0 | \n", + "0 | \n", + "211536 | \n", + "13.0000 | \n", + "NaN | \n", + "S | \n", + "
| 887 | \n", + "888 | \n", + "1 | \n", + "1 | \n", + "Graham, Miss. Margaret Edith | \n", + "female | \n", + "19.0 | \n", + "0 | \n", + "0 | \n", + "112053 | \n", + "30.0000 | \n", + "B42 | \n", + "S | \n", + "
| 888 | \n", + "889 | \n", + "0 | \n", + "3 | \n", + "Johnston, Miss. Catherine Helen \"Carrie\" | \n", + "female | \n", + "NaN | \n", + "1 | \n", + "2 | \n", + "W./C. 6607 | \n", + "23.4500 | \n", + "NaN | \n", + "S | \n", + "
| 889 | \n", + "890 | \n", + "1 | \n", + "1 | \n", + "Behr, Mr. Karl Howell | \n", + "male | \n", + "26.0 | \n", + "0 | \n", + "0 | \n", + "111369 | \n", + "30.0000 | \n", + "C148 | \n", + "C | \n", + "
| 890 | \n", + "891 | \n", + "0 | \n", + "3 | \n", + "Dooley, Mr. Patrick | \n", + "male | \n", + "32.0 | \n", + "0 | \n", + "0 | \n", + "370376 | \n", + "7.7500 | \n", + "NaN | \n", + "Q | \n", + "
891 rows × 12 columns
\n", + "| \n", + " | passengerid | \n", + "survived | \n", + "pclass | \n", + "name | \n", + "sex | \n", + "age | \n", + "sibsp | \n", + "parch | \n", + "ticket | \n", + "fare | \n", + "cabin | \n", + "embarked | \n", + "
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | \n", + "1 | \n", + "0 | \n", + "3 | \n", + "Mr. Owen Harris Braund | \n", + "male | \n", + "22.0 | \n", + "1 | \n", + "0 | \n", + "A/5 21171 | \n", + "7.2500 | \n", + "NaN | \n", + "S | \n", + "
| 1 | \n", + "2 | \n", + "1 | \n", + "1 | \n", + "Mrs. John Bradley (Florence Briggs Thayer) Cum... | \n", + "female | \n", + "38.0 | \n", + "1 | \n", + "0 | \n", + "PC 17599 | \n", + "71.2833 | \n", + "C85 | \n", + "C | \n", + "
| 2 | \n", + "3 | \n", + "1 | \n", + "3 | \n", + "Miss. Laina Heikkinen | \n", + "female | \n", + "26.0 | \n", + "0 | \n", + "0 | \n", + "STON/O2. 3101282 | \n", + "7.9250 | \n", + "NaN | \n", + "S | \n", + "
| 3 | \n", + "4 | \n", + "1 | \n", + "1 | \n", + "Mrs. Jacques Heath (Lily May Peel) Futrelle | \n", + "female | \n", + "35.0 | \n", + "1 | \n", + "0 | \n", + "113803 | \n", + "53.1000 | \n", + "C123 | \n", + "S | \n", + "
| 4 | \n", + "5 | \n", + "0 | \n", + "3 | \n", + "Mr. William Henry Allen | \n", + "male | \n", + "35.0 | \n", + "0 | \n", + "0 | \n", + "373450 | \n", + "8.0500 | \n", + "NaN | \n", + "S | \n", + "
| ... | \n", + "... | \n", + "... | \n", + "... | \n", + "... | \n", + "... | \n", + "... | \n", + "... | \n", + "... | \n", + "... | \n", + "... | \n", + "... | \n", + "... | \n", + "
| 886 | \n", + "887 | \n", + "0 | \n", + "2 | \n", + "Rev. Juozas Montvila | \n", + "male | \n", + "27.0 | \n", + "0 | \n", + "0 | \n", + "211536 | \n", + "13.0000 | \n", + "NaN | \n", + "S | \n", + "
| 887 | \n", + "888 | \n", + "1 | \n", + "1 | \n", + "Miss. Margaret Edith Graham | \n", + "female | \n", + "19.0 | \n", + "0 | \n", + "0 | \n", + "112053 | \n", + "30.0000 | \n", + "B42 | \n", + "S | \n", + "
| 888 | \n", + "889 | \n", + "0 | \n", + "3 | \n", + "Miss. Catherine Helen \"Carrie\" Johnston | \n", + "female | \n", + "NaN | \n", + "1 | \n", + "2 | \n", + "W./C. 6607 | \n", + "23.4500 | \n", + "NaN | \n", + "S | \n", + "
| 889 | \n", + "890 | \n", + "1 | \n", + "1 | \n", + "Mr. Karl Howell Behr | \n", + "male | \n", + "26.0 | \n", + "0 | \n", + "0 | \n", + "111369 | \n", + "30.0000 | \n", + "C148 | \n", + "C | \n", + "
| 890 | \n", + "891 | \n", + "0 | \n", + "3 | \n", + "Mr. Patrick Dooley | \n", + "male | \n", + "32.0 | \n", + "0 | \n", + "0 | \n", + "370376 | \n", + "7.7500 | \n", + "NaN | \n", + "Q | \n", + "
891 rows × 12 columns
\n", + "