# Homework 3
### Due Date:  Tuesday, September 20th at 11:59 PM

---
## Problem 1:  `Git` and recovering from a mistake
You will do this problem in the Jupyter notebook so I can see your output.  Once again, you will work with your playground repository.

**NOTE:** At the beginning of each cell, you MUST type `%%bash`.  If you don't do that then you will not be able to work with the necessary bash commands.

Follow the following steps for this problem:

1. First cell:
  1. Type `cd /tmp` to enter the temporary directory
  2. git clone url_to_your_playground_repo
2. Second cell:
  1. Go into your local `playground` directory (`cd /tmp/playground`)
  2. Type `git pull origin mybranch1`
  3. `ls`
3. Third cell:
  1. Go into your local `playground` directory (`cd /tmp/playground`)
  2. Type `git status`
4. Fourth cell:
  1. Go into your local `playground` directory (`cd /tmp/playground`)
  2. Type `git reset --hard origin/master`
  3. `ls`
5. Fifth cell:
  1. Go into your local `playground` directory (`cd /tmp/playground`)
  2. Type `git status`

The whole point of this problem was to show you how to get your local repo back to an earlier state.  In this exercise, you accidentally merged something to `master` that you didn't want.  Rather than starting to delete things all over the place, you can simply reset your `HEAD` to a previous commit.

```bash
%%bash
cd /tmp
git clone https://github.com/HeyItsRiddhi/playground.git
```

    fatal: destination path 'playground' already exists and is not an empty directory.

```bash
%%bash
cd /tmp/playground
git pull origin mybranch1
ls
```

    Merge made by the 'recursive' strategy.
     books.md | 1 +
     1 file changed, 1 insertion(+)
     create mode 100644 books.md
    README.md
    books.md
    feature.txt
    intro.md
    world.md

    From https://github.com/HeyItsRiddhi/playground
     * branch            mybranch1  -> FETCH_HEAD

```bash
%%bash
cd /tmp/playground
git status
```

    On branch master
    Your branch is ahead of 'origin/master' by 2 commits.
      (use "git push" to publish your local commits)
    nothing to commit, working tree clean

```bash
%%bash
cd /tmp/playground
git reset --hard origin/master
ls
```

    HEAD is now at 4f54b54 Shared attribution between Joe & Sally
    README.md
    feature.txt
    intro.md
    world.md

```bash
%%bash
cd /tmp/playground
git status
```

    On branch master
    Your branch is up-to-date with 'origin/master'.
    nothing to commit, working tree clean

## Problem 2:  `Git` and checking out a single file
Sometimes you don't want to merge an entire branch from the upstream but just one file from it. There is a direct use case for such a situation. Suppose I've made an error in this homework (or a lecture) and want to correct it. I fix the mistake in the upstream repo. In the meantime you have edited some other files and you really don't want to manually ignore my older copies of those files. Rather, you want to fix just one file from this new branch. This is how you do it.

As usual, be sure to type in `%%bash` before you write any `bash` commands in a cell.

**Note:** The steps below assume that you have already cloned the `playground` repo in this notebook.

1. First cell:
  1. Go into the `playground` repo and fetch the changes from the master branch of the `course` remote.
2. Second cell:
  1. `git checkout course/master -- README.md`.  The `--` means that `README.md` is a file (as opposed to a `branch`).
  2. `cat README.md`.  This just looks at the updated file.
3. Third cell:
  1. `git status`
  2. Commit the changes to your local repo with an appropriate commit message.
  3. `git status`
  4. Push the changes to your remote repo.

```bash
%%bash
cd /tmp/playground/
git fetch course
```

    From https://github.com/IACS-CS-207/playground
     * [new branch]      master     -> course/master

```bash
%%bash
cd /tmp/playground/
git checkout course/master -- README.md
cat README.md
```

    # Playground Repo

    For practicing.

```bash
%%bash
cd /tmp/playground/
git status
git commit -m "updated README.md with course README.md"
git status
git push
```

    On branch master
    Your branch is ahead of 'origin/master' by 1 commit.
      (use "git push" to publish your local commits)
    nothing to commit, working tree clean
    On branch master
    Your branch is ahead of 'origin/master' by 1 commit.
      (use "git push" to publish your local commits)
    nothing to commit, working tree clean
    On branch master
    Your branch is ahead of 'origin/master' by 1 commit.
      (use "git push" to publish your local commits)
    nothing to commit, working tree clean

    To https://github.com/HeyItsRiddhi/playground.git
       4f54b54..4ac63eb  master -> master

## Problem 3
This problem is related to the Lecture 4 exercises.
1. Open the `languages.txt` file.  This file contains all the languages that students listed as their primary language in the course survey.
2. Load the language strings from the file into a list.
3. Use the `Counter` method from the `collections` library to count the number of occurrences of each element of the list.
  + **NOTE:**  It is not necessary to use the `most_common()` method here.
4. Create a bar plot to display the frequency of each language.  Be sure to label the x-axis!
  + Remember, to create plots in the notebook you must put the line `%matplotlib inline` at the beginning of your notebook.
  + Be sure to import matplotlib:  `import matplotlib.pyplot as plt`.
  + To generate the bar plot write `plt.bar(x_coords, freqs)`.  You need to define `x_coords` and `freqs`.
  + **Hint:** You may want to use the `numpy` `arange` function to create `x_coords`.  Remember, `x_coords` is the x-axis and it should have points for each distinct language.
  + **Hint:** To get `freqs`, you may want to use the `values()` method on your result from step 3.  That is, `freqs = result_from_3.values()`.
  + **Hint:** To label the x-axis you should use `plt.xticks(x_coords, labels)` where labels can be accessed through the `keys()` method on your result from step 3.

```python
import collections
import numpy
import matplotlib.pyplot as plt

#get data
file = open('../../lectures/L4/languages.txt', 'r')
languages = file.read().splitlines()
file.close()

#calculate the amount of student that know each language
freq = collections.Counter(languages)

#plot data
freqs = freq.values()
labels = freq.keys()
x_coords = numpy.arange(8)
plt.bar(x_coords,freqs)
plt.xticks(x_coords, labels)
plt.xlabel("Languages")
plt.ylabel("Students")
plt.title('Languages students in the class know')
plt.show()
```

---
## Problem 4
In chemical kinetics, the reaction rate coefficient for a given reaction depends on the temperature of the system.  The functional relationship between the reaction rate coefficient and temperature is given by the Arrhenius rate:
\begin{align}
k\left(T\right) = A T^{b}\exp\left(-\frac{E}{RT}\right)
\end{align}
where $A$, $b$, and $E$ are parameters, $R = 8.314 \dfrac{\textrm{J}}{\textrm{mol} \textrm{ K}}$  is the universal gas constant, and $T$ is the temperature.

Write a function which returns $k\left(T\right)$ given $A$, $b$, $E$, and $T$.  Here are a few requirements:
* The function should test for exceptions where necessary.
* Pass the parameters $A$, $b$, and $E$ in as a list.
* Make $R$ a keyword argument to the function.

```python
import math
import numpy as np

def arrhenius_rate(params, T, R=8.314):
    #Check that A,b,E is passed as a list
    if(len(params) != 3 or type(params) != list):
        print("ERROR: First argument needs to be a list of 3")
    else:
        [A,b,E] = params
        #Check that A,b,E is passed are numbers
        if ((type(A) != int and type(A) != float) or
            (type(b) != int and type(b) != float) or
            (type(E) != int and type(E) != float)):
            print("Error: All numbers in list must be of type integer or float")
        else:
            #Calculate k(T)
            kt = A*T**(b)*np.exp(-E/(R*T))
            return kt

```

---
## Problem 5
Using `numpy` arrays, plot $k\left(T\right)$ for $T\in\left(0, 5000\right]$ for three different sets of parameters $\left\{A, b, E\right\}$.  Make sure all three lines are on the same figure and be sure to label each line.  You may use the function from Problem 2.  You may want to play with the parameters a little bit to get some nice curves but you won't lose points for ugly curves either (as long as they're correct!).

```python
#array of temperatures
temps = np.arange(0,5000)

# 3 different sets of parameters
args = [[10,2,0.1],[20,2,0.11],[30,2,0.12]]

# array to hold 3 data sets for 3 diff paramseters
plots =[]

#create 3 data sets to plot
for arg in args:
    kt = list(map(lambda x: arrhenius_rate(arg,int(x)), temps))
    plots.append(kt)
```

```python
import matplotlib.pyplot as plt

#plot lines
plt.plot(temps, plots[0], lw=2, ls='-', label=r'$A,b,E = 10,2,0.1$')
plt.plot(temps, plots[1], lw=2, ls='-', label=r'$A,b,E = 10,2,0.11$')
plt.plot(temps, plots[2], lw=2, ls='-', label=r'$A,b,E = 10,2,0.12$')

#display legend and define axises & plot title
plt.legend()
plt.xlabel(r'$Temperature$', fontsize=16)
plt.ylabel(r'$k(T)$', fontsize=16)
plt.title('Arrhenius Rate with 3 parameters', fontsize=16)
plt.show()

```

```python

```