{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 2.18 Programming for Geoscientists class test 2012. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Note: 2013 was the first time the exam was run using iPython rather than using a text editor and the command line. This iPython version was created for the 2013 class so that they would know what to expect." ] }, { "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": [ "**1.** Ignore this question from 2012 as it deals with material no longer covered." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**2.** Write a program, that creates a *list* $t$ with 6 values, 0.1, 0.2, ..., 0.6. Compute a corresponding *list* $y$ of $y(t)$ values using the formula:\n", "$$y(t) = v_0t − gt^2,$$\n", "where $v_0=6.0$ and $g=9.8$.\n", "\n", "* Store these two lists, t and y, in a new list t1.\n", "* Write out a table with a column of t and a column of y values by traversing the data in the nested t1 list.\n", "* You may use list or NumPy array for t and y.\n", "* Print out a table header with the column names ’t’ and ’y’.\n", "* For printing the table, iterate the nested list t1, do not access the previously computed t and y lists directly.\n", "* Print out the table t1 using format specifiers so that the columns are aligned." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**3.** The factorial of n, written as n!, is defined as:\n", "$$n! = n(n − 1)(n − 2)...2\\cdot1,$$\n", "with the special cases\n", "$$1! = 1, 0! = 1.$$\n", "For example, $4! = 4\\cdot3\\cdot2\\cdot1 = 24$, and $2! = 2\\cdot1 = 2$.\n", "\n", "* Write a function fact(n) that returns $n!$. You **must** not use the *fact* function from the math module.\n", "* Return 1 immediately if $x$ is 1 or 0, otherwise use a loop to compute $n!$.\n", "* The function must be called *fact* and take a single argument called n.\n", "* The software should check that the supplied value is a non-negative integer. If it is not, raise a ValueError exception." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**4.** A table of temperatures and densities, in units of degrees ($C$) and $kg/m^3$, are given in the file [data/density_air.dat](https://raw.githubusercontent.com/ggorman/Introduction-to-programming-for-geoscientists/master/notebook/data/density_air.dat)\n", "Write a program that reads in the data from file into a list for temperature (first column) and density (second column) and plots the variation of density against temperature.\n", "\n", "* The input file contains blank lines and lines starting with a ’#’, which you must ignore when reading in the data.\n", "* You may use list or NumPy array for temperature and density.\n", "* Plot the variation of density against temperature.\n", "* Label the x axis \"Temperature (Celsius)\" and the y axis \"Density ($kg/m^3$)\".\n", "* Use the plot title \"Density of air at different temperatures, at 1 atm pressure\".\n", "* Display a legend with the label ’Air’." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**5.** Based on the data in the file [data/constants.txt](https://raw.githubusercontent.com/ggorman/Introduction-to-programming-for-geoscientists/master/notebook/data/constants.txt), make a dictionary where the keys are the names of the physical constant and the values are a tuple containing the numerical value and the units.\n", "\n", "* Use a Python dictionary to store the data.\n", "* All numerical values should be of type float.\n", "* Print out the dictionary without any formatting." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "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 }