Skip to content
Open
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
724 changes: 724 additions & 0 deletions Exams/Final/Copy_of_Final(1).ipynb

Large diffs are not rendered by default.

460 changes: 460 additions & 0 deletions Exams/Mid-term/Copy_of_Exam(1).ipynb

Large diffs are not rendered by default.

755 changes: 755 additions & 0 deletions Labs/Lab-1/Copy_of_Lab_1.ipynb

Large diffs are not rendered by default.

1,144 changes: 1,144 additions & 0 deletions Labs/Lab-2/Copy_of_Lab_2.ipynb

Large diffs are not rendered by default.

728 changes: 728 additions & 0 deletions Labs/Lab-3/Copy_of_Lab_3.ipynb

Large diffs are not rendered by default.

269 changes: 269 additions & 0 deletions Labs/Lab-4/Copy_of_Exam.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,269 @@
{
"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.1"
},
"colab": {
"name": "Copy of Exam.ipynb",
"provenance": []
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "zShtCZTtkgHN",
"colab_type": "text"
},
"source": [
"# Mid-term Exam\n",
"\n",
"[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github//afarbin/DATA1401-Spring-2020/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": "markdown",
"metadata": {
"id": "aw8J18IwkgHQ",
"colab_type": "text"
},
"source": [
"1. 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": "kEBRdsuFkgHU",
"colab_type": "code",
"colab": {}
},
"source": [
"def first_alphabetically(lst):\n",
" 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']\n",
" states.sort()\n",
" return states[0]\n",
" "
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {
"id": "xylKXe6VkgHu",
"colab_type": "text"
},
"source": [
"should return the string `\"Alabama\"`. Note that you can compare strings:"
]
},
{
"cell_type": "code",
"metadata": {
"id": "sBHzr4gWkgHw",
"colab_type": "code",
"outputId": "80dbfb54-d7eb-480a-a965-82d918cdbce4",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 35
}
},
"source": [
"first_alphabetically(\"lst\")"
],
"execution_count": 0,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"'Alabama'"
]
},
"metadata": {
"tags": []
},
"execution_count": 31
}
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "n4dETTaUkgH6",
"colab_type": "text"
},
"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": "VA91-PWEkgH8",
"colab_type": "text"
},
"source": [
"**2**. 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": "markdown",
"metadata": {
"id": "MF_MwcczkgH-",
"colab_type": "text"
},
"source": [
"3. 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": "markdown",
"metadata": {
"id": "IX3Y4-fukgIA",
"colab_type": "text"
},
"source": [
"4. 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": "markdown",
"metadata": {
"id": "cQ3IU4HwkgIC",
"colab_type": "text"
},
"source": [
"5. 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": "markdown",
"metadata": {
"id": "7590pjz1kgIE",
"colab_type": "text"
},
"source": [
"6. 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": "FBFYjSt3kgIF",
"colab_type": "text"
},
"source": [
"$$\n",
"\\int_{-\\infty}^{x_{90}} N(x;\\mu=0,\\sigma=1) dx = 0.9\n",
"$$"
]
},
{
"cell_type": "code",
"metadata": {
"id": "aI1Lc3BHkgIG",
"colab_type": "code",
"colab": {}
},
"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",
" \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"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "GElElvuAkgIO",
"colab_type": "code",
"colab": {}
},
"source": [
""
],
"execution_count": 0,
"outputs": []
}
]
}
Loading