From 77ff6485f8710de8b298e944a56028b9e73dbf99 Mon Sep 17 00:00:00 2001 From: Ishrat <164588557+ishratarshad@users.noreply.github.com> Date: Mon, 4 Aug 2025 08:38:09 -0400 Subject: [PATCH] completed week 2 hw --- homeworks/IA_week2_hw.ipynb | 360 ++++++++++++++++++++++++++++++++++++ 1 file changed, 360 insertions(+) create mode 100644 homeworks/IA_week2_hw.ipynb diff --git a/homeworks/IA_week2_hw.ipynb b/homeworks/IA_week2_hw.ipynb new file mode 100644 index 0000000..db09a95 --- /dev/null +++ b/homeworks/IA_week2_hw.ipynb @@ -0,0 +1,360 @@ +{ + "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": null, + "metadata": {}, + "outputs": [], + "source": [ + "import string\n", + "\n", + "alphabet = list(string.ascii_lowercase[:10]) # Extra credit: no hardcoding\n", + "print(\"First 3 letters:\", alphabet[:3]) # First 3 letters\n", + "print(\"Middle 3 letters:\", alphabet[3:6]) # Any 3 from the middle" + ] + }, + { + "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": 1, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Rappers with title case: ['Lil Wayne', 'Nicki Minaj', 'Drake']\n" + ] + } + ], + "source": [ + "# Solve rapper names here\n", + "rappers = ['lil wayne', 'nicki minaj', 'drake']\n", + "titled_rappers = [name.title() for name in rappers]\n", + "print(\"Rappers with title case:\", titled_rappers) " + ] + }, + { + "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": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "True\n", + "False\n" + ] + } + ], + "source": [ + "def even_odd(num):\n", + " return num % 2 == 0\n", + "\n", + "print(even_odd(4)) \n", + "print(even_odd(5)) " + ] + }, + { + "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": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "With loop:\n", + "353 35.3\n", + "wihtout loop:\n", + "353 35.3\n" + ] + } + ], + "source": [ + "# Solve problem here:\n", + "\n", + "my_list = [1, 5, 10, 55, 88, 44, 42, 50, 20, 38]\n", + "list_sum = 0\n", + "\n", + "for n in my_list:\n", + " list_sum += n\n", + "list_average = list_sum / len(my_list)\n", + "\n", + "print(\"With loop:\")\n", + "# Keep this as your last line in this cell.\n", + "print(list_sum, list_average)\n", + "\n", + "\n", + "#without loop:\n", + "list_s = sum(my_list)\n", + "list_a = list_s / len(my_list)\n", + "\n", + "print(\"wihtout loop:\")\n", + "print(list_s, list_a) # Output: sum and average without loop" + ] + }, + { + "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": 9, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['Michele', 'Robin', 'Sara', 'Michele']\n", + "list after removing duplicates: ['Michele', 'Robin', 'Sara']\n" + ] + } + ], + "source": [ + "\n", + "names = [\"Michele\", \"Robin\", \"Sara\", \"Michele\"]\n", + "print(names)\n", + "\n", + "def removeDuplicates(a):\n", + " s = set()\n", + " res = []\n", + " for name in a:\n", + " if name not in s:\n", + " s.add(name)\n", + " res.append(name)\n", + " return res\n", + "print(\"list after removing duplicates:\", removeDuplicates(names))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "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": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "input list: [5, 10, 99, 20, 25]\n", + "new list with first and last elements: [5, 25]\n" + ] + } + ], + "source": [ + "# Solve problem here:\n", + "input_list = [5, 10, 99, 20, 25]\n", + "print(\"input list:\", input_list)\n", + "new_list = []\n", + "new_list.append(input_list[0]) \n", + "new_list.append(input_list[-1]) \n", + "print(\"new list with first and last elements:\", new_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": 11, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "10\n" + ] + } + ], + "source": [ + "def my_max(a, b, c):\n", + " if a > b and a > c:\n", + " return a\n", + " elif b > a and b > c:\n", + " return b\n", + " else:\n", + " return c\n", + "\n", + "print(my_max(1, 5, 10)) " + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "10" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Test to see if your function works properly.\n", + "my_max(1, 5, 10)" + ] + }, + { + "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": null, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1\n", + "2\n", + "3: fizz\n", + "4\n", + "5: buzz\n", + "6: fizz\n", + "7\n", + "8\n", + "9: fizz\n", + "10: buzz\n", + "11\n", + "12: fizz\n", + "13\n", + "14\n", + "15: fizzbuzz\n" + ] + } + ], + "source": [ + "def fizzbuzz(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", + "#test the fizzbuzz function\n", + "for i in range(1, 16):\n", + " result = fizzbuzz(i)\n", + " if result:\n", + " print(f\"{i}: {result}\")\n", + " else:\n", + " print(i)" + ] + } + ], + "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.12.1" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +}