#!/usr/bin/env python # coding: utf-8 # # 2.18 Programming for Geoscientists class test 2012. # ## 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. # # Test instructions # # * This test contains **5** questions each of which should be answered. # * Write your program in a Python cell just under each question. # * You can write an explanation of your solution as comments in your code. # * 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. # * Save your work regularly. # * 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 # **1.** Ignore this question from 2012 as it deals with material no longer covered. # **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: # $$y(t) = v_0t − gt^2,$$ # where $v_0=6.0$ and $g=9.8$. # # * Store these two lists, t and y, in a new list t1. # * Write out a table with a column of t and a column of y values by traversing the data in the nested t1 list. # * You may use list or NumPy array for t and y. # * Print out a table header with the column names ’t’ and ’y’. # * For printing the table, iterate the nested list t1, do not access the previously computed t and y lists directly. # * Print out the table t1 using format specifiers so that the columns are aligned. # **3.** The factorial of n, written as n!, is defined as: # $$n! = n(n − 1)(n − 2)...2\cdot1,$$ # with the special cases # $$1! = 1, 0! = 1.$$ # For example, $4! = 4\cdot3\cdot2\cdot1 = 24$, and $2! = 2\cdot1 = 2$. # # * Write a function fact(n) that returns $n!$. You **must** not use the *fact* function from the math module. # * Return 1 immediately if $x$ is 1 or 0, otherwise use a loop to compute $n!$. # * The function must be called *fact* and take a single argument called n. # * The software should check that the supplied value is a non-negative integer. If it is not, raise a ValueError exception. # **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) # 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. # # * The input file contains blank lines and lines starting with a ’#’, which you must ignore when reading in the data. # * You may use list or NumPy array for temperature and density. # * Plot the variation of density against temperature. # * Label the x axis "Temperature (Celsius)" and the y axis "Density ($kg/m^3$)". # * Use the plot title "Density of air at different temperatures, at 1 atm pressure". # * Display a legend with the label ’Air’. # **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. # # * Use a Python dictionary to store the data. # * All numerical values should be of type float. # * Print out the dictionary without any formatting. # In[ ]: