A function line_averages(filename) that takes a string filename which contains the name of a file to be processed. The function should open and read that file. The file is expected to contain numbers that are separated by commas (the format is known as a comma-separated-value file, short csv). The function should compute the average value for every line, and return the average values in a list. For example, if we call line_averages("data.csv") and file data.csv reads:
1,2
1,1,1,1
-1,0,1
42,17
then the function should return the list [1.5, 1.0, 0.0, 29.5].
We provide the following function (to be included in
training4.py) which downloads this webpage and returns it as a string:
import urllib
def noaa_string():
"""Fetch from the Internet and return the current NOAA METAR
weather observation data for EDDH (Hamburg Airport) as a string.
"""
url = "http://tgftp.nws.noaa.gov/data/observations/metar/decoded/EDDH.TXT"
noaa_data_string = urllib.request.urlopen(url).read()
return noaa_data_string.decode("utf-8")
The library urllib.request allows to access a webpage like a file
through its urlopen() function, and you should include import
urllib.request at the beginning of your file training4.py.
Call the function noaa_string from the Python prompt and
inspect the return value.
Your task is to write a function noaa_temperature(s) which should
take a string s as returned from noaa_string() as the input
argument, extract the temperature in degree Celsius from the
string, and return this temperature as an integer number:
In [ ]: noaa_temperature(noaa_string())
Out[ ]: 10
NOAA may at times change the number and order of lines in the data,
but you can assume that the format of the line containing the
temperature data does not change.
A function seq_sqrt(xs) which takes a list of non-negative
numbers xs with elements [x0, x1, x2, ..., xn], and returns
the list [sqrt(x0), sqrt(x1), sqrt(x2), ..., sqrt(xn)]. In
other words, the function takes a list of numbers, and returns a
list of the same length that contains the square root for each
number in the list.
A function mean(xs) that takes a sequence xs of numbers, and returns the (arithmetic) mean (i.e. the average value).
Example:
In [ ]: mean([0, 1, 2])
Out[ ]: 1.0
A function wc(filename) that returns the number of words in file
filename. The name wc stands for Word Count. To split a string s into words, use s.split() for this
exercise (i.e. the behaviour of the split() method is here used to define what a word is).
Example 1: For a file data.txt with content:
One Two
a function call wc('data.txt') should return 2.
Example 2: For a file data.txt with content:
One Two
Three Four Five
a function call wc('data.txt') should return 5.
You can test your function on the
Alice in Wonderland book and should
expect that this has more than 10,000 words.
(If you use Linux or OSX, you can download a file using offered at
a URL using wget URL from the a terminal. For this example, try
wget https://www.desy.de/~fangohr/teaching/py4cs2024/labs/lab4/19033.txt)