Lines 77
PointSort (4)
##### Permissions
Owner: jsimones
Group Owner: SnortSnort
Viewable by Everyone
Editable by All Siafoo Users
Need a quick chart or graph for your blog? Try our reStructured Text renderer.
You are viewing an old version of this snippet. View Latest Version

# Stou's I/O and sorting assignment 0

 Language Python
# 's
`  1"""  2Simple I/O "Assignment"  3  4The "assignment":  5  61. Load a file of arbitrary length that is composed of 3 tab-separated columns  7   with floating point numbers representing x, y, z coordinates of points. Lines  8   starting with the pound character (i.e. #) are comments and your program  9   should ignore them. 10 112. Accept file on the command lines: 12   \$ ./a.out some_data.dat 13 143. Use STL sort to sort all data points beyond the first by their distance from 15   the first data point (shortest to longest). 16 174. Output sorted list as x y z distance use only 4 significant figures (output 18   only once we can compare speed and correctness with a script later). 19 205. (bonus) Implement your own sorting method and compare to STL method. 21 226. (bonus) Try some of the Boost libraries 23 24Python suggestions: 25 261. Use the standard sorting routine and define your own comparator function 27 28Oh and I suggest sticking to the standard libraries (STL and Python)... if you 29decide to use stuff from Boost and numpy it would probably be good to implement 30your own too and compare them. 31 32""" 33import sys 34 35def dist(x, y, z, x0, y0, z0): 36    return ((x - x0)**2 + (y - y0)**2 + (z - z0)**2)**0.5 37 38def get_coords(file, delim='\t', skip=['#', '\n']): 39    """ 40    Return a list of x,y,z tuples from the coordinates listed in file. Lines 41    starting with # and empty lines are ignored. 42 43    file : file 44        An open file object containing x,y,z data. 45    delim : str 46        String on which split each line of data into x,y,z components. 47    skip : list 48        List of strings. A line will be skipped if it begins with any of these. 49 50    """ 51    xyz = [] 52    for line in file: 53        if any(line.startswith(i) for i in skip): 54            continue 55        xyz.append(tuple(float(i) for i in line.split(delim))) 56    return xyz 57 58def dist_sort(xyz, keyfunc=None): 59    """ 60    xyz : list 61        List of x,y,z tuples. 62    keyfunc : function 63        A function that returns a key to sort by, given an x,y,z,r tuple (r is 64        distance from x0,y0,z0), i.e. a comparator. 65 66    """ 67    def default_keyfunc(xyzr): 68        return xyzr[-1] 69 70    if keyfunc is None: 71        keyfunc = default_keyfunc 72 73    x0, y0, z0 = xyz[0] 74 75    xyzr = [] 76    for x, y, z in xyz: 77        r = dist(x, y, z, x0, y0, z0) 78        xyzr.append((x, y, z, r)) 79 80    return sorted(xyzr, key=keyfunc) 81 82def pretty_print(xyzr): 83    """ 84    Print the contents of xyzr, a list of 4-tuples. 85 86    """ 87    for x, y, z, r in xyzr: 88        print '%.4f\t%.4f\t%.4f\t%.4f' % (x, y, z, r) 89 90def main(): 91    filename = sys.argv[1] 92 93    file = open(filename, 'r') 94    xyz = get_coords(file) 95    file.close() 96 97    xyzr = dist_sort(xyz) 98    pretty_print(xyzr) 99100if __name__ == '__main__':101    main()102103### todo:104### 1) try writing my own sorting function105### 2) try redoing everything using numpy`