{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "8dc79c58-7ff8-42ff-8ebd-469446306abb", "metadata": {}, "outputs": [], "source": [ "### This should be the only import you need. \n", "\n", "## If you need to import anything else, first ASK ME!\n", "\n", "from math import sqrt\n" ] }, { "cell_type": "markdown", "id": "9a1d8025-4545-45e0-907d-334779117b04", "metadata": {}, "source": [ "Name: ____Alex Dekhtyar__\n", "\n", "Cal Poly email: ______________________________\n", "\n", "\n", "Major: ______________________________________\n", "\n", "Are you enrolled or on the waitlist? ________________________" ] }, { "cell_type": "markdown", "id": "f69c8f9b-fefa-4fcb-bd4c-7a9ea4726bf4", "metadata": {}, "source": [ "**DATA 301 Lab 1**\n", "\n", "**Instructions**: Read questions. In the Python cells provided, write Python code that provides requested computations. Each problem has \n", "one or more tests. Run tests, validate your code. When you are done, find the notebook in your home directory/your hard drive, and submit it using the following handin command:\n", "\n", "\n", " handin dekhtyar 301-lab01 Lab01-Data301.ipynb\n", "\n", "Please note, you have to be logged into a CSL unix server (unix1.csc.calpoly.edu, unix2, unix3, etc.) in order to use the handin command.\n", "\n", "**If you do not have a CSL account**: Attach the Lab01-Data301.ipynb file to a PM on the course Slack workspace.\n", "\n" ] }, { "cell_type": "markdown", "id": "325b558d-1325-4910-a799-b32404e34bf5", "metadata": {}, "source": [ "**Problem 1**\n", "\n", "The Python code in the cell below instantiates two lists of integer numbers\n" ] }, { "cell_type": "code", "execution_count": 2, "id": "29d96160-b543-4fe3-b126-71aab9354c40", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "([1, 2, 3, 4, 5], [10, 30, 40, 50, 70])" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "## LIST 1\n", "l = [1,2,3,4,5]\n", "\n", "## LIST 2\n", "\n", "q = [10, 30, 40, 50, 70]\n", "\n", "l,q\n" ] }, { "cell_type": "markdown", "id": "eaddf1f9-b045-4a2e-9da4-641e85306769", "metadata": {}, "source": [ "In the cell below write a python **list comprehension statement** that creates a new list, whose values are the pairwise products of the elements from the two lists above, minus the pairwise sums. That is, an element of the new list *p* is computed using the following expression:\n", "\n", "> p_i = l_i * q_i - (l_i+q_i)" ] }, { "cell_type": "code", "execution_count": 3, "id": "65b3e346-731a-4b31-96fc-0f9c84e308de", "metadata": {}, "outputs": [], "source": [ "## compute the list p here using a list comprehension syntax\n", "\n" ] }, { "cell_type": "code", "execution_count": 6, "id": "fc7ee72b-2d39-4a4b-8c84-9d10c9387089", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n" ] } ], "source": [ "## Test\n", "p1Answer = [-1, 28, 77, 146, 275]\n", "print(p== p1Answer)\n", "\n" ] }, { "cell_type": "markdown", "id": "e2b41931-3a6b-4dc8-922d-cd1f40121538", "metadata": {}, "source": [ "**Problem 2**\n", "\n", "Consider the following dictionary." ] }, { "cell_type": "code", "execution_count": 8, "id": "29517a10-c620-4a27-bc04-7ee1d4a3582d", "metadata": {}, "outputs": [], "source": [ "## this records the number of Wordle tries for each CSSE faculty member playing wordle for Wordle 281\n", "d = { 'alex': 4,\n", " 'znjp': 3,\n", " 'zoe': 5,\n", " 'akeen': 5,\n", " 'foaad':4,\n", " 'clements':4,\n", " 'stephen':4,\n", " 'ayaan':4}" ] }, { "cell_type": "markdown", "id": "132e3e2e-07e1-4bd6-a1fd-cbfc503dc12e", "metadata": {}, "source": [ "Place code into the cell below that results creation of a list _dd_ that consists of keys from the dictionary _d_ whose values are even.\n" ] }, { "cell_type": "code", "execution_count": 13, "id": "e4d8e040-13dd-4a90-bfcc-066f4647f6e4", "metadata": {}, "outputs": [], "source": [ "## Your code goes here\n", "\n", "## final variable that contains your list should be named dd\n", "\n", "\n", "\n" ] }, { "cell_type": "code", "execution_count": 17, "id": "df831987-b205-4ef4-83aa-04f164f79e9e", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n" ] } ], "source": [ "## Test\n", "p2Answer = ['alex','foaad', 'clements', 'stephen', 'ayaan']\n", "\n", "\n", "## note, that we are only interested in the correct keys here, not in whatever order they are in the list\n", "\n", "print(set(p2Answer)== set(dd))\n" ] }, { "cell_type": "markdown", "id": "caef1dc3-2ed2-4af1-8f82-695d80ea89cd", "metadata": {}, "source": [ "**Problem 3**\n", "\n", "In the cell below, write a function that takes as input a dictionary (we will test this function on the dictionary from Problem 2), and outputs the standard deviation of all the _values_ in the dictionary. You cannot use any Pandas, NumPy or math package functions except for square root for this - compute the standard deviation from scratch. You can use built-in functions that work with dictionaries and lists. Use the formula for the population standard deviation.\n" ] }, { "cell_type": "code", "execution_count": 36, "id": "48d6acf3-50e1-4f05-878e-a5e9b41c3efa", "metadata": {}, "outputs": [], "source": [ "## stDevDict(): returns the population standard deviation of the values of the input array\n", "\n", "def stDevDict(dc):\n", " \n", " ## your code goes here\n", "\n", " return \n", " " ] }, { "cell_type": "code", "execution_count": 41, "id": "bcfe004d-38ea-44c6-ad04-4714482b5147", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n" ] } ], "source": [ "### Test\n", "p3Answer = 0.5994789404140899\n", "epsilon = 0.00001\n", "\n", "st= stDevDict(d)\n", "\n", "print(abs(p3Answer - st) <= epsilon)\n", "\n" ] }, { "cell_type": "markdown", "id": "0cb95fdc-0edf-449b-ba71-cec48cd2e0d0", "metadata": {}, "source": [ "**Problem 4**\n", "\n", "Consider the string variable _text_ shown below." ] }, { "cell_type": "code", "execution_count": 44, "id": "14881179-35d5-4103-9d61-1122e881a11f", "metadata": {}, "outputs": [], "source": [ "text = \"As spring break wrapped up students were returning to Cal Poly's campus ahead of the start of the Spring quarter on Sunday March 27 2022 After two years Cal Poly students are returning to normalcy on campus no longer being required to wear masks, regardless of vaccination status beginning on March 28 2022\"\n", "\n", "## source: https://www.ksby.com/news/local-news/cal-poly-students-return-from-spring-break\n", "\n", "\n" ] }, { "cell_type": "markdown", "id": "d69f3fd3-597b-429e-b5ce-377867fbc295", "metadata": {}, "source": [ "In the next cell write Python code that outputs the list of all words of length five (5). The order in which the words are outputed does not matter, also, your list can contain duplicates of the same word. You can use any standard Python string functions." ] }, { "cell_type": "code", "execution_count": 47, "id": "d64bf97e-8f10-425f-adf7-b95e591c870b", "metadata": {}, "outputs": [], "source": [ "## Place your Problem 4 code here\n", "\n", "## use variable words5 to store the list of words of length 5.\n", "\n" ] }, { "cell_type": "code", "execution_count": 50, "id": "aef11e1e-38bd-4b62-9c08-fae7e5c9be31", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n" ] } ], "source": [ "## TEST\n", "\n", "p4Answer = ['break', 'ahead', 'start', 'March', 'After', 'years', 'being', 'March']\n", "\n", "print(set(p4Answer) == set(words5))\n" ] }, { "cell_type": "markdown", "id": "b4fde826-fc4b-4f96-b85b-11093b0b6e61", "metadata": {}, "source": [ "**Problem 5**\n", "\n", "In the cell below write Python code that outputs the list of duplicate words in the string _text_. Each duplicate word needs to be reported once. You can use any standard Python string functions." ] }, { "cell_type": "code", "execution_count": 55, "id": "25147578-aced-463a-bd81-7d09b289979f", "metadata": {}, "outputs": [], "source": [ "## Place your Problem 5 code here\n", "\n", "##use variable duplicates to store the list of duplicate words\n", "\n", " \n" ] }, { "cell_type": "code", "execution_count": 61, "id": "6affb89a-af61-4892-9b38-6cecbb14f5e7", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n" ] } ], "source": [ "### Test\n", "\n", "p5Answer = ['students', 'returning', 'to', 'Cal', 'campus', 'of', 'the', 'on', 'March', '2022']\n", "\n", "p5Answer.sort()\n", "duplicates.sort()\n", "\n", "print(p5Answer == duplicates)\n", "\n", "\n" ] }, { "cell_type": "markdown", "id": "53f38106-d9d2-4e30-a515-a594a0694eec", "metadata": {}, "source": [ "**Problem 6**\n", "\n", "The variable _points_ defined below stores information about a collection of two dimensional points.\n", "\n", "In the cell below, write Python code that returns a pair of points with the shortest Eucledean distance.\n", "The variable should be a tuple that contains two tuples representing the two points. You can use the square root function when computing the Eucledean distance, but otherwise, you need to compute the Eucledean distance from scratch. Feel free to create a function if you feel it might help. (Feel free to create any functions you feel might help). You cannot use any built-in \"min\" or \"max\" functions - build your own.\n" ] }, { "cell_type": "code", "execution_count": 73, "id": "92dbd7e6-a985-4e96-a46e-67298702369a", "metadata": {}, "outputs": [], "source": [ "points = [(2,3), (7,6), (-4, 3), (8,8), (-1, 0), (4, -2), (8,3)]\n", "\n", "### place your Problem 6 code here\n", "## use variable closestPoints for result.\n", "\n", "\n", "\n" ] }, { "cell_type": "code", "execution_count": 79, "id": "4c04abd2-25e1-4e36-beb2-29f977b2514e", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n" ] } ], "source": [ "### TEST\n", "\n", "p6Answer = ((8, 8), (7, 6))\n", "\n", "q = list(closestPoints)\n", "q.sort()\n", "p6a = list(p6Answer)\n", "p6a.sort()\n", "\n", "print(q == p6a)\n" ] }, { "cell_type": "markdown", "id": "b321d497-09f5-4eec-a7b9-fb5d9c91405f", "metadata": {}, "source": [ "**Problem 7**\n", "\n", "Write python code defining the following function _mpf_:\n", "\n", "* _mpf_(0) = 0\n", "* _mpf_(1) = 5\n", "* If the argument is divisible by 3, then _mpf_(x) = _mpf_(x/3)+x/3\n", "* in all other cases, _mpf_(x) is the sum of _mpf_ of x-1 and _mpf_ of x-2\n" ] }, { "cell_type": "code", "execution_count": 91, "id": "0a5f994c-651f-4eb1-93c7-42de25188f0c", "metadata": {}, "outputs": [], "source": [ "def mpf(x): \n", " \n", " ## your code goes here\n", " \n", " return" ] }, { "cell_type": "code", "execution_count": 96, "id": "1a524aba-7896-4f24-b2bd-ba329494d16e", "metadata": { "collapsed": true, "jupyter": { "outputs_hidden": true }, "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 : True\n", "1 : True\n", "5 : True\n", "9 : True\n", "18 : True\n", "22 : True\n", "31 : True\n", "40 : True\n", "41 : True\n", "42 : True\n", "50 : True\n", "100 : True\n", "120 : True\n" ] } ], "source": [ "### TESTS\n", "\n", "p7TestCases = [0,1,5, 9, 18, 22, 31, 40, 41, 42, 50, 100, 120]\n", "\n", "results = [mpf(x) for x in p7TestCases]\n", "\n", "p7Answers = [0, 5, 17.0, 9.0, 13.0, 180.0, 375.0, 676.0, 753.0, 93.0, 1247.0, 7966.0, 716.0]\n", "\n", "for i in range(len(results)):\n", " print (p7TestCases[i], \":\", results[i]==p7Answers[i])\n" ] }, { "cell_type": "markdown", "id": "60d2c927-62cd-4346-8d0b-cf0611af5a45", "metadata": { "tags": [] }, "source": [ "**Problem 8**\n", "\n", "The now-popular game of Wordle allows the player to guess a five-letter word. For each guess, the game provides the player with information about the letters that the players guessed without guessing correctly the position (\"yellow\" letters), letters that the player did not guess (\"black\" letters) and letters that are in the word and the player guessed the position correctly.\n", "\n", "Write a function _wordleEValSimple()_ that takes two parameters:\n", "\n", "* _wordle_: the answer to the Wordle puzzle\n", "* _guess_: the player's guess.\n", "\n", "The function shall return a five-letter string consisting of letters 'b' (for 'black'), 'y' (for 'yellow'), and 'g' (for 'green') representing the response of the Wordle game to the player given the puzzle answer and the player's guess.\n", "\n", "**Note:** For this problem, your function must perform correctly on all five-letter Wordle puzzle words and five-letter guesses that **do not have repeated letters**.\n", "\n", "Your function should return an empty string if either the puzzle word or the guess have the wrong length.\n", "\n" ] }, { "cell_type": "code", "execution_count": 1, "id": "cbe378fb-6a0e-4d7f-bcf0-b41ae67e34ef", "metadata": {}, "outputs": [], "source": [ "def wordleEvalSimple(wordle, guess):\n", " ## your code goes here\n", "\n", " return out\n", " \n" ] }, { "cell_type": "code", "execution_count": 118, "id": "c6e9d113-e26c-4a75-afd8-64d19f8ba1f5", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n", "True\n", "True\n", "True\n", "True\n", "True\n", "True\n" ] } ], "source": [ "### TEST\n", "\n", "print(wordleEvalSimple(\"blast\",\"blast\")== \"ggggg\")\n", "print(wordleEvalSimple(\"blast\",\"score\")== \"ybbbb\")\n", "print(wordleEvalSimple(\"poster\",\"score\")== \"\")\n", "print(wordleEvalSimple(\"score\",\"poster\")==\"\")\n", "print(wordleEvalSimple(\"parse\",\"mound\")== \"bbbbb\")\n", "print(wordleEvalSimple(\"spear\",\"pears\")== \"yyyyy\")\n", "print(wordleEvalSimple(\"place\",\"crane\")== \"ybgbg\")\n" ] }, { "cell_type": "markdown", "id": "df782a4c-0232-43c6-8860-f90b87737e93", "metadata": {}, "source": [ "**Problem 9** Now, write a _wordleEval_ function that takes the same inputs, but outputs correct feedback for **any pair** of words of length 5.\n", "\n", "Specifically:\n", "\n", "* if both the puzzle word and the guess have unique letters, _wrodleEval_ shall behave the same way as _wordleEvalSimple_ from **Problem 8**\n", "\n", "* if the puzzle word has duplicate letters, and the guess has one such letter - privelege green over yellow.\n", "\n", "* if the puzzle word has duplicate letters and the guess has duplicate letters - first determine the number of position matches. Output green for any position matches. Output yellow for position mismatches.\n", "\n", "* If the guess has more occurrences of a letter than the puzzle word, only the number of occurrences of the letter in the guess can be colored green or yellow. Any extra letters in the guess have to be black. Green letters (i.e., position matches) are privileged. Otherwise, color in order of occurrence of letters in the word. \n", "\n", "Check the tests for the exact semantics.\n", "\n" ] }, { "cell_type": "code", "execution_count": 3, "id": "59968663-717b-4739-b595-483716fcd0df", "metadata": {}, "outputs": [], "source": [ "def wordleEval(wordle, guess):\n", " \n", " ## your code goes here\n", " \n", " \n", " return \n", " \n" ] }, { "cell_type": "code", "execution_count": 4, "id": "6a69f700-acbd-40a3-8984-f2b44cbec9d2", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Old Tests\n", "\n" ] }, { "ename": "NameError", "evalue": "name 'out' is not defined", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Old Tests\\n\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 4\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mwordleEvalSimple\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"blast\"\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\"blast\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m==\u001b[0m \u001b[0;34m\"ggggg\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 5\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mwordleEvalSimple\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"blast\"\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\"score\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m==\u001b[0m \u001b[0;34m\"ybbbb\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 6\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mwordleEval\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"poster\"\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\"score\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m==\u001b[0m \u001b[0;34m\"\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;32m\u001b[0m in \u001b[0;36mwordleEvalSimple\u001b[0;34m(wordle, guess)\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;31m## your code goes here\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 4\u001b[0;31m \u001b[0;32mreturn\u001b[0m \u001b[0mout\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 5\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mNameError\u001b[0m: name 'out' is not defined" ] } ], "source": [ "### OLD TESTS\n", "print(\"Old Tests\\n\")\n", "\n", "print(wordleEvalSimple(\"blast\",\"blast\")== \"ggggg\")\n", "print(wordleEvalSimple(\"blast\",\"score\")== \"ybbbb\")\n", "print(wordleEval(\"poster\",\"score\")== \"\")\n", "print(wordleEval(\"score\",\"poster\")==\"\")\n", "print(wordleEval(\"parse\",\"mound\")== \"bbbbb\")\n", "print(wordleEval(\"spear\",\"pears\")== \"yyyyy\")\n", "print(wordleEval(\"place\",\"crane\")== \"ybgbg\")\n", "\n", "print(\"\\nNew Tests\\n\")\n", "\n", "### NEW TESTS\n", "print(wordleEval(\"guess\", \"guess\") == \"ggggg\")\n", "print(wordleEval(\"guess\",\"score\")== \"ybbby\")\n", "print(wordleEval(\"score\",\"guess\")== \"bbyyb\")\n", "print(wordleEval(\"score\",\"scams\")== \"ggbbb\")\n", "print(wordleEval(\"sassy\",\"spars\")== \"gbyby\")\n", "print(wordleEval(\"spars\",\"sassy\")== \"gyybb\")\n", "print(wordleEval(\"spast\",\"sassy\")== \"gybgb\")\n", "\n", "\n", "\n", "\n" ] }, { "cell_type": "markdown", "id": "2fadc7d1-0464-44a4-b679-94649f7b735b", "metadata": {}, "source": [ "**Problem 10**\n", "\n", "Describe the most challenging Python concept you have encountered to date.\n" ] }, { "cell_type": "markdown", "id": "903701f3-ff0c-468c-a46c-7bc56a3392d4", "metadata": {}, "source": [ "**Problem 10 answer**:\n", "\n", "\n", "\n", "\n" ] }, { "cell_type": "markdown", "id": "8ec12388-2bde-4dfa-ab2f-e1a56e060e2b", "metadata": {}, "source": [ "**YOU ARE DONE**\n", "\n", "\n", "If you are using the CSL Jupyer Server, then simply find your _Lab01-Data301.ipynb_ file in your CSL home directory, and submit it using the following handin command:\n", "\n", " handin dekhtyar 301-lab01 Lab01-Data301.ipynb\n", " \n", " \n", "If you are using your own laptop and either a local Jupyter Labs server, or Google Colab, then\n", "\n", "* download the Lab01-Data301.ipynb file to local computer/locate this file on your hard drive\n", "\n", "* attach this file to a PM to me in the course Slack workspace." ] }, { "cell_type": "markdown", "id": "0388d1a8-82ff-4e43-a2af-719212bb6bfc", "metadata": {}, "source": [ "**THANK YOU**" ] }, { "cell_type": "code", "execution_count": null, "id": "efffa635-718e-4724-8057-789c9dc00df3", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.8.8" } }, "nbformat": 4, "nbformat_minor": 5 }