Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pull #17

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open

pull #17

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Copy of Exam.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"nbformat":4,"nbformat_minor":0,"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.0"},"colab":{"name":"Copy of Exam.ipynb","provenance":[{"file_id":"https://github.com/UTA-DataScience/DATA1401.2020.Fall/blob/master/Exams/Mid-term/Exam.ipynb","timestamp":1604083946757}]}},"cells":[{"cell_type":"markdown","metadata":{"id":"UjacMfbO2fn1"},"source":["# Mid-term Exam\n","\n","[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/UTA-DataScience/DATA1401.2020.Fall/blob/master/Exams/Mid-term/Exam.ipynb)\n","\n","Add cells to this notebook as you need for your solutions and your test of your solutions."]},{"cell_type":"code","metadata":{"id":"j8RVx1_15Cr7","executionInfo":{"status":"ok","timestamp":1604084622684,"user_tz":300,"elapsed":749,"user":{"displayName":"celeste esquivel","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiOH_x2vPhIFIpCE9cr1ZkuaiQ2mn9UtMedgwY8=s64","userId":"09302908931913256973"}}},"source":["import numpy as np"],"execution_count":19,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"r6ds7vWp2fn8"},"source":["1a. Write a function `first_alphabetically(lst)` that takes a list `lst` of strings and returns the string that is alphabetically first. For example, calling your function with the list of states:"]},{"cell_type":"code","metadata":{"id":"54t3_miu2foA","executionInfo":{"status":"ok","timestamp":1604084482614,"user_tz":300,"elapsed":645,"user":{"displayName":"celeste esquivel","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiOH_x2vPhIFIpCE9cr1ZkuaiQ2mn9UtMedgwY8=s64","userId":"09302908931913256973"}}},"source":["states=['Mississippi', 'Maryland', 'Delaware', 'Connecticut', 'Virginia', 'Utah', 'Kansas',\n"," 'Wyoming', 'Indiana', 'Louisiana', 'Missouri', 'Illinois', 'Minnesota', 'Vermont', \n"," 'New Mexico', 'North Dakota', 'Wisconsin', 'Tennessee', 'New York', 'Oklahoma', \n"," 'Colorado', 'Pennsylvania', 'West Virginia', 'Alabama', 'Montana', 'Texas', \n"," 'Washington', 'Michigan', 'New Hampshire', 'Arkansas', 'Hawaii', 'Iowa', \n"," 'Idaho', 'Kentucky', 'Ohio', 'Nebraska', 'Alaska', 'Oregon', 'South Dakota', \n"," 'New Jersey', 'Florida', 'Georgia', 'Rhode Island', 'Arizona', 'Maine', \n"," 'South Carolina', 'California', 'Nevada', 'Massachusetts', 'North Carolina']"],"execution_count":13,"outputs":[]},{"cell_type":"code","metadata":{"id":"BGQ4N8C925xD","executionInfo":{"status":"ok","timestamp":1604084484280,"user_tz":300,"elapsed":903,"user":{"displayName":"celeste esquivel","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiOH_x2vPhIFIpCE9cr1ZkuaiQ2mn9UtMedgwY8=s64","userId":"09302908931913256973"}},"outputId":"374147f8-113a-426b-d057-f7c95e20c930","colab":{"base_uri":"https://localhost:8080/","height":36}},"source":["def first_alphabetically(lst):\n"," sorted = list()\n"," min = lst[0]\n"," for i in lst:\n"," if i < min:\n"," min = i\n"," return min\n","first_alphabetically(states)"],"execution_count":14,"outputs":[{"output_type":"execute_result","data":{"application/vnd.google.colaboratory.intrinsic+json":{"type":"string"},"text/plain":["'Alabama'"]},"metadata":{"tags":[]},"execution_count":14}]},{"cell_type":"markdown","metadata":{"id":"i8K-7S_32foT"},"source":["should return the string `\"Alabama\"`. Note that you can compare strings:"]},{"cell_type":"code","metadata":{"id":"ydW1XLg62foX","executionInfo":{"status":"ok","timestamp":1604084486406,"user_tz":300,"elapsed":665,"user":{"displayName":"celeste esquivel","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiOH_x2vPhIFIpCE9cr1ZkuaiQ2mn9UtMedgwY8=s64","userId":"09302908931913256973"}},"outputId":"d4d08bd0-365f-4da0-a13d-9c161a84b6ec","colab":{"base_uri":"https://localhost:8080/"}},"source":["print(\"A\">\"B\")\n","print(\"B\">\"A\")\n","print(\"A\">\"a\")\n","print(\"bca\">\"bbc\")"],"execution_count":15,"outputs":[{"output_type":"stream","text":["False\n","True\n","False\n","True\n"],"name":"stdout"}]},{"cell_type":"markdown","metadata":{"id":"3jMHbNtZ2fop"},"source":["Make sure your implementation isn't case sensitive. **Do not use** python's built-in `min`, `max`, `sort` or any other sort function you find."]},{"cell_type":"markdown","metadata":{"id":"kG_MM0K-2for"},"source":["1b. Write a function `arg_first_alphabetically(lst)`, which does the same thing as in exercise 1 but returns the index of the first string alphabetically."]},{"cell_type":"code","metadata":{"id":"OHB1jLgr3YpP","executionInfo":{"status":"ok","timestamp":1604084489576,"user_tz":300,"elapsed":648,"user":{"displayName":"celeste esquivel","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiOH_x2vPhIFIpCE9cr1ZkuaiQ2mn9UtMedgwY8=s64","userId":"09302908931913256973"}},"outputId":"f70b7a68-7774-4341-cabe-0801b44a6285","colab":{"base_uri":"https://localhost:8080/"}},"source":["def arg_first_alphabetically(lst):\n"," min = first_alphabetically(lst)\n"," for i in range(len(lst)):\n"," if lst[i] == min:\n"," return i\n","arg_first_alphabetically(states)"],"execution_count":16,"outputs":[{"output_type":"execute_result","data":{"text/plain":["23"]},"metadata":{"tags":[]},"execution_count":16}]},{"cell_type":"markdown","metadata":{"id":"tK4BCbbl2fow"},"source":["1c. Use your result in question 2 to implement a function `arg_sort_alphabetically(lst)` that returns a list that is alphabetically sorted. Sorting can be accomplished by successively applying the function in question 1 and removing the first element alphabetically. You can remove an element from a list using `pop()`. Make sure your implementation isn't case sensitive. Do not use python's built-in `min`, `max`, `sort` or any other sort function you find."]},{"cell_type":"code","metadata":{"id":"xxKbC3Ax3qtI","executionInfo":{"status":"ok","timestamp":1604084497430,"user_tz":300,"elapsed":681,"user":{"displayName":"celeste esquivel","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiOH_x2vPhIFIpCE9cr1ZkuaiQ2mn9UtMedgwY8=s64","userId":"09302908931913256973"}},"outputId":"3cabce59-41ad-4bd6-9a90-d58771ec0be2","colab":{"base_uri":"https://localhost:8080/"}},"source":["def arg_sort_alphabetically(lst):\n"," sorted= []\n"," while len(lst)>0:\n"," pos = (arg_first_alphabetically(lst))\n"," sorted.append(lst[pos])\n"," lst.pop(pos)\n"," return (sorted)\n","arg_sort_alphabetically(states)"],"execution_count":17,"outputs":[{"output_type":"execute_result","data":{"text/plain":["['Alabama',\n"," 'Alaska',\n"," 'Arizona',\n"," 'Arkansas',\n"," 'California',\n"," 'Colorado',\n"," 'Connecticut',\n"," 'Delaware',\n"," 'Florida',\n"," 'Georgia',\n"," 'Hawaii',\n"," 'Idaho',\n"," 'Illinois',\n"," 'Indiana',\n"," 'Iowa',\n"," 'Kansas',\n"," 'Kentucky',\n"," 'Louisiana',\n"," 'Maine',\n"," 'Maryland',\n"," 'Massachusetts',\n"," 'Michigan',\n"," 'Minnesota',\n"," 'Mississippi',\n"," 'Missouri',\n"," 'Montana',\n"," 'Nebraska',\n"," 'Nevada',\n"," 'New Hampshire',\n"," 'New Jersey',\n"," 'New Mexico',\n"," 'New York',\n"," 'North Carolina',\n"," 'North Dakota',\n"," 'Ohio',\n"," 'Oklahoma',\n"," 'Oregon',\n"," 'Pennsylvania',\n"," 'Rhode Island',\n"," 'South Carolina',\n"," 'South Dakota',\n"," 'Tennessee',\n"," 'Texas',\n"," 'Utah',\n"," 'Vermont',\n"," 'Virginia',\n"," 'Washington',\n"," 'West Virginia',\n"," 'Wisconsin',\n"," 'Wyoming']"]},"metadata":{"tags":[]},"execution_count":17}]},{"cell_type":"markdown","metadata":{"id":"y-Kapx0u2foz"},"source":["2. Implement a function `outer_product` that takes two one-dimensional lists of numbers and returns the two-dimensional outer product matrix defined as:\n","\n","\\begin{equation*}\n","\\begin{pmatrix} x_1\\\\x_2\\\\ \\vdots \\\\x_m \\end{pmatrix} \\begin{pmatrix} y_1&y_2& \\dots &y_n\\end{pmatrix} =\n","\\begin{pmatrix}\n","x_1y_1 & x_1y_2 & \\dots & x_1y_n\\\\\n","x_2y_1 & x_2y_2 & \\dots & x_2y_n\\\\\n","\\vdots & \\vdots & \\ddots & \\vdots \\\\\n","x_my_1 & x_my_2 & \\dots & x_my_n\n","\\end{pmatrix}\n","\\end{equation*}\n","\n","In other words the elements of matrix C which is the outer product of A and B are $c_{ij} = a_i b_j$."]},{"cell_type":"code","metadata":{"id":"jSn6PG5r45Tc","executionInfo":{"status":"ok","timestamp":1604084656086,"user_tz":300,"elapsed":786,"user":{"displayName":"celeste esquivel","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiOH_x2vPhIFIpCE9cr1ZkuaiQ2mn9UtMedgwY8=s64","userId":"09302908931913256973"}},"outputId":"c78e680a-0f2a-40ab-8d16-999381b3963a","colab":{"base_uri":"https://localhost:8080/"}},"source":["def outer_product(a,b):\n"," outer= np.zeros((len(a),len(b)))\n"," for i in range (len(a)):\n"," for j in range (len(b)):\n"," outer[i][j] = a[i]*b[j]\n"," return outer\n","outer_product([1,2],[3,4])"],"execution_count":21,"outputs":[{"output_type":"execute_result","data":{"text/plain":["array([[3., 4.],\n"," [6., 8.]])"]},"metadata":{"tags":[]},"execution_count":21}]},{"cell_type":"markdown","metadata":{"id":"a8ogcD3H2fo2"},"source":["3a. Implement a function `cumulative_sum(lst)` that takes a list of numbers and returns a list of same size where the element `i` is the sum of the elements `0` to `i` of the input list. For example given `[1,2,3]`, you should return [1,3,6]."]},{"cell_type":"code","metadata":{"id":"ZkNBoocD5fW_","executionInfo":{"status":"ok","timestamp":1604084704820,"user_tz":300,"elapsed":680,"user":{"displayName":"celeste esquivel","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiOH_x2vPhIFIpCE9cr1ZkuaiQ2mn9UtMedgwY8=s64","userId":"09302908931913256973"}},"outputId":"a040854e-d9f1-4b09-d897-131350e04e88","colab":{"base_uri":"https://localhost:8080/"}},"source":["def cumilative_sum(lst):\n"," for i in range(1,len(lst)):\n"," lst[i] += lst[i-1]\n"," return lst\n","\n","cumilative_sum([1,2,3])"],"execution_count":22,"outputs":[{"output_type":"execute_result","data":{"text/plain":["[1, 3, 6]"]},"metadata":{"tags":[]},"execution_count":22}]},{"cell_type":"markdown","metadata":{"id":"frdUQxoc2fo6"},"source":["3b. Imagine you have a normal distributed random variable `x`. For example `x` can be grades on this exam. Using the normal distribution generator and histogram functions from lecture (provided below) and `cumulative_sum` from previous question to compute what is the value of $x_{90}$ in $\\sigma$ such that 90% of the values $x$ are below $x_{90}$. In other words:"]},{"cell_type":"markdown","metadata":{"id":"BIjALSQc2fo9"},"source":["$$\n","\\int_{-\\infty}^{x_{90}} N(x;\\mu=0,\\sigma=1) dx = 0.9\n","$$"]},{"cell_type":"code","metadata":{"id":"AWhdFuec2fpA","executionInfo":{"status":"ok","timestamp":1604085002754,"user_tz":300,"elapsed":646,"user":{"displayName":"celeste esquivel","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiOH_x2vPhIFIpCE9cr1ZkuaiQ2mn9UtMedgwY8=s64","userId":"09302908931913256973"}}},"source":["import math,random\n","\n","def arange(x_min,x_max,steps=10):\n"," step_size=(x_max-x_min)/steps\n"," x=x_min\n"," out = list()\n"," for i in range(steps):\n"," out.append(x)\n"," x+=step_size\n"," return out\n","\n","def generate_normal(N,m=0,s=1):\n"," out = list() \n"," \n"," while len(out)<N:\n","# for _ in range(int(float(N)/2)+1):\n"," U1=random.random()\n"," U2=random.random()\n"," \n"," out.append(s*math.sqrt(-2*math.log(U1))*math.cos(2*math.pi*U2)+m)\n"," out.append(s*math.sqrt(-2*math.log(U1))*math.sin(2*math.pi*U2)+m)\n","\n"," return out[:N]\n","\n","def histogram(data, n_bins=10,x_min=None, x_max=None):\n"," if x_min==None:\n"," x_min=min(data)\n"," if x_max==None:\n"," x_max=max(data)\n"," total_sum = sum(data)\n"," x90 = 0.9 * total_sum \n"," bin_edges = arange(x_min,x_max,n_bins)\n"," bin_edges.append(x_max)\n","\n"," hist=[0]*n_bins\n"," \n"," for d in data:\n"," for i in range(n_bins):\n"," if d>=bin_edges[i] and d<bin_edges[i+1]:\n"," hist[i]+=1\n"," break\n"," \n"," return hist,bin_edges\n","\n","def q6(data_points):\n"," data = generate_normal(data_points)\n"," hist,bin_edges = histogram(data)\n"," sort(bin_edges)\n"," cum_sum = cumilative_sum(data)\n"," x90 = 0.9 * data_points\n"," var = 0\n"," for i in range(len(hist)):\n"," var += hist[i] \n"," if var > x90:\n"," return i-1\n","\n"],"execution_count":26,"outputs":[]},{"cell_type":"code","metadata":{"id":"ME5rmTan6Lim","executionInfo":{"status":"ok","timestamp":1604085005979,"user_tz":300,"elapsed":712,"user":{"displayName":"celeste esquivel","photoUrl":"https://lh3.googleusercontent.com/a-/AOh14GiOH_x2vPhIFIpCE9cr1ZkuaiQ2mn9UtMedgwY8=s64","userId":"09302908931913256973"}},"outputId":"e5d17e79-05d3-4320-eaa0-2979e3716827","colab":{"base_uri":"https://localhost:8080/"}},"source":["data = generate_normal(10)\n","data"],"execution_count":27,"outputs":[{"output_type":"execute_result","data":{"text/plain":["[-0.7595019467250037,\n"," 2.0062746412991204,\n"," -1.3185421292106545,\n"," 2.5314632573441704,\n"," -0.5709567060204296,\n"," 1.720784953755478,\n"," -1.2561401171688849,\n"," -0.9156177596515729,\n"," 0.73817276640953,\n"," 0.5991503540550571]"]},"metadata":{"tags":[]},"execution_count":27}]},{"cell_type":"markdown","metadata":{"id":"i6QmvdQX2fpS"},"source":["4a. Recall in quiz 1 we implemented parts of a poker game in python. The solution to quiz 1 is below. Modify the `flush`, `straight`, and `straight_flush` functions to return `None` instead of `False` and the highest card (value-wise) instead of \"True\"."]},{"cell_type":"code","metadata":{"id":"EN8i9CRJ2fpW"},"source":["suits = [\"Clubs\", \"Diamonds\", \"Hearts\", \"Spades\"]\n","values = list(range(2,11)) + [ \"Jack\", \"Queen\", \"King\", \"Ace\"]\n","\n","def make_deck():\n"," deck=list()\n"," for suit in suits:\n"," for value in values:\n"," deck.append((suit,value))\n"," return deck\n","\n","def flush(hand):\n"," suit=None\n"," for card in hand:\n"," if suit:\n"," if not card[0]==suit:\n"," return False\n"," else:\n"," suit=card[0]\n"," return True\n","\n","def consecutive(lst): \n"," return sorted(lst) == list(range(min(lst), max(lst)+1)) \n","\n","def straight(hand): \n"," return consecutive([values.index(card[1]) for card in hand])\n","\n","def straight_flush(hand):\n"," return stright(hand) and flush(hand)"],"execution_count":null,"outputs":[]},{"cell_type":"markdown","metadata":{"id":"jr_9iprj2fpq"},"source":["4b. Add two additional functions `three_of_a_kind` and `pair` that work in an analogous manner."]},{"cell_type":"code","metadata":{"id":"TdBh4CNT2fpu"},"source":[""],"execution_count":null,"outputs":[]}]}
1 change: 1 addition & 0 deletions Labs/Lab-1-Solutions.ipynb

Large diffs are not rendered by default.

Loading