Assume we have a text file (e.g. numbers.txt
) containing integers separated by spaces:
1 2 1 3 1 4
In module filestats.py
, create function compute_file_statistics()
that takes a path to a text file as its argument, reads in all the numbers, and returns a named tuple Statistics
with fields mean
, median
, min
, max
. The statistics names tuple shall be defined as:
Statistics = namedtuple('Statistics', 'mean median min max')
Suggestions:
compute_statistics()
that will accept a list of numbers as input and will produce the required data structure. Than, the main function may just read the data in, and pass them to this function.
>>> from filestats import compute_file_statistics >>> compute_file_statistics('numbers.txt') Statistics(mean=2.0, median=1.5, min=1, max=4)
Let's have a text file, e.g. capitals.csv
(the .csv extension stands for “comma-separated values”) containing a pair of strings on each line. The first string is a name of a country, the second string is a name of its capital:
Czech Republic,Prague USA,Washington Germany,Berlin Russia,Moscow
In module 'geography.py', create function load_capitals()
that takes a path to a file containing countries and their capitals as an argument, and reads it into a dictionary.
>>> from geography import load_capitals >>> capitals = load_capitals('capitals.csv') >>> print(capitals) {'Czech Republic': 'Prague', 'USA': 'Washington', 'Germany': 'Berlin', 'Russia': 'Moscow'}The order of the individual key-value pairs may be different.
In module utils.py
, create function all_elements_unique()
that checks whether a collection (given as input to the function) has all items unique.
>>> from utils import all_elements_unique >>> all_elements_unique('abcdef') True >>> all_elements_unique([1, 2, 7]) True >>> all_elements_unique('abracadabra') False >>> all_elements_unique([1, 1, 2, 7]) False
In module texttools.py
, create function get_unique_words(fpath1, fpath2)
which takes paths to 2 files, and returns a 2 tuple:
Given e.g. the following files text1.txt
and text2.txt
When I was one, I had just begun.
When I was two, I was nearly new.
the result of executing the function may look like this:
>>> from texttools import get_unique_words >>> first, second = get_unique_words('text1.txt', 'text2.txt') >>> print(first) {'one', 'had', 'just', 'begun'} >>> print(second) {'two', 'nearly', 'new'}
Again, the order of individual words in the printouts of the resulting sets may differ.
Work on the next graded homework: working with files.
And: