Search
Assume we have a text file (e.g. numbers.txt) containing integers separated by spaces:
numbers.txt
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:
filestats.py
compute_file_statistics()
Statistics
mean
median
min
max
Statistics = namedtuple('Statistics', 'mean median min max')
Suggestions:
compute_statistics()
>>> 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:
capitals.csv
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.
load_capitals()
>>> from geography import load_capitals >>> capitals = load_capitals('capitals.csv') >>> print(capitals) {'Czech Republic': 'Prague', 'USA': 'Washington', 'Germany': 'Berlin', 'Russia': 'Moscow'}
In module utils.py, create function all_elements_unique() that checks whether a collection (given as input to the function) has all items unique.
utils.py
all_elements_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:
texttools.py
get_unique_words(fpath1, fpath2)
Given e.g. the following files text1.txt and text2.txt
text1.txt
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: