Skip to content

Instantly share code, notes, and snippets.

@lin7sh
Created February 1, 2016 01:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lin7sh/4c02cff6377bbfe369df to your computer and use it in GitHub Desktop.
Save lin7sh/4c02cff6377bbfe369df to your computer and use it in GitHub Desktop.
{
"metadata": {
"name": "",
"signature": "sha256:9aaa56a65db5dee4b72ed3dcbca0acb157c6bb103a4ba3ed90af6c0628c16b6f"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"Introduction to programming - Assignment 2015/2016"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Submit to Blackboard before Wednesday, 23rd of March at 12:00.**\n",
"\n",
"**5 marks attainable, corresponding to 60% of the course grade (provided the attendance criteria have been met)**"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In this exercise you should write a standalone program that when executed prompts the user for the name of an <a href=\"http://openbabel.org/wiki/XYZ_(format)#Additional_Comments\">XYZ file</a> and after the user enters this name the program prints out the chemical formula of the structure present in the file.\n",
"\n",
"Accompanying this notebook is the file <a href=\"protein.xyz\">protein.xyz</a> which contains the structure of an adduct of a Hemoglobin monomer and crystalization water molecules. Your program should be able to produce the chemical formula for protein.xyz. However, your solution should be general, work for any other well formed XYZ file with atoms identified by their chemical symbol and will be tested against different molecules.\n",
"\n",
"Full marks will be awarded to any implementation that produces the correct result against the protein.xyz file and a blind test case.\n",
"\n",
"If your implementation does not fully work for a generic case, partial marks will be awarded for correct implementation of the functions below."
]
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Implementation suggestion"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"A working solution can be obtained by appropriately combining the following functions."
]
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": [
"get_element(*string*) - 1 mark"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This function should receive one string as its argument (a line of the XYZ file) and return another string with the letter(s) of the chemical symbol present on that line."
]
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": [
"match_element(*string*,*list*) - 1 mark"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This function receives two arguments: a string containing a chemical symbol (argument 1) and a list containing pairs of chemical symbols strings and integers (argument 2). The form of argument 2 is:\n",
"\n",
" [[element_0,number_0],[element_1,number_1],[element_2,number_2],....]\n",
"\n",
"where *element_i* are strings and *number_i* are integers.\n",
"\n",
"The function should return a list of the form:\n",
"\n",
" [[element_i,number_i],i]\n",
" \n",
"where *element_i* is the chemical symbol string (argument 1), and *i* is the position of the pair [element_i,number_i] within the original list (argument 2).\n",
"\n",
"If the pair [element_i,number_i] cannot be found within the list, the function should return a list of the form\n",
"\n",
" [[element_i,0],length]\n",
" \n",
"where *length* is the length of the input list (argument 2). "
]
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": [
"add_element(*list1*,*list2*) - 1 mark"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This function receives two lists, list1 has the same form as argument 2 of *match_element()*\n",
"\n",
" [[element_0,number_0],[element_1,number_1],[element_2,number_2],....]\n",
" \n",
"and list2 has the same form as the output of *match_element()*\n",
"\n",
" [[element_i,number_i],i]\n",
" \n",
"The function should output a list the same as list1 but where element index *i* takes the form\n",
"\n",
" [element_i,number_i+1]"
]
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": [
"make_formula(*list*) - 1 mark"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This function receives as argument a list of the form\n",
"\n",
" [[element_0,number_0],[element_1,number_1],[element_2,number_2],....]\n",
" \n",
"and outputs a string with the chemical formula\n",
"\n",
" 'element_0number_0element_1number_1element_2number_2....'\n",
" \n",
"If any of *number_i* is equal to 1, as is conventional in chemistry, it should be supressed from the chemical formula."
]
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Instructions"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You should submit to Blackboard a single file called chemical_formula.py, containing a standalone Python script with your program.\n",
"\n",
"If you choose to follow the suggested implementation, it is important that you define the functions (name and argument order) as indicated, such that your functions can be correctly tested."
]
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment