{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 2.18 Programming for Geoscientists class test - 14:00-16:00 18th December 2014" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#Test instructions\n", "\n", "* This test contains **5** questions each of which should be answered.\n", "* Write your program in a Python cell just under each question.\n", "* You can write an explanation of your solution as comments in your code.\n", "* In each case your solution program must fulfil all of the instructions - please check the instructions carefully and double check that your program fulfils all of the given instructions.\n", "* Save your work regularly.\n", "* At the end of the test you should email your IPython notebook document (i.e. this document) to [Gerard J. Gorman](http://www.imperial.ac.uk/people/g.gorman) at g.gorman@imperial.ac.uk" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Question 1** \n", "\n", "* Correct the 5 errors in the following program.\n", "* In each case add a comment explaining the error and the fix.\n", "* To verify your answer remember that $sin ^2( x ) + cos ^2( x ) = 1$." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from math import sin, cos\n", "x = pi /4\n", "1_val = sin^2( x ) + cos^2( x )\n", "print 1_VAL" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Question 2**\n", "\n", "* Create a python list containing 101 evenly spaced values between -1.0 and 1.0.\n", "* Show how this could be more efficently done using NumPy.\n", "* Print out the values using a formatted print statement." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Question 3**\n", "\n", "* a. Write a python function that evaluates the equation below for input variables $x$ and $t$: $$f(x, t) = e^{-(x-3t)^2} \\sin \\left( 3\\pi \\left( x-t \\right) \\right)$$\n", "* b. Generate an array of $x$ values in the range $[-3.0, 10]$.\n", "* c. Using this array of x values, plot the function for $t=0.0$, $t=1.0$ and $t=2.0$." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Question 4**\n", "\n", "* Write a program to read the data file [seismic.dat](https://raw.githubusercontent.com/ggorman/Introduction-to-programming-for-geoscientists/master/notebook/data/seismic.dat) and store the magnitude of the earthquakes (*i.e.* the last column) in an numpy array.\n", "* How many earthquakes are there above magnitude 6.0?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Question 5** The file [human_evolution.txt](https://raw.githubusercontent.com/ggorman/Introduction-to-programming-for-geoscientists/master/notebook/data/human_evolution.txt) holds information about various human species and their hight, weight, and brain volume. \n", "\n", "* Write a program that reads this file and stores the data in a python dictionary humans.\n", "* The dictionary key should correspond to the specie name (e.g., “homo erectus”), and the value should be a list containing the corresponding data. For example, humans[\"H. habilis\"] should give [\"2.2 - 1.6\", \"1.0 – 1.5\", \"33 – 55\", \"660\"].\n", "* Write out the humans dictionary in a formatted tabular form." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 2", "language": "python", "name": "python2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.12" } }, "nbformat": 4, "nbformat_minor": 0 }