lab4¶
New topics: dictionaries, exceptions, venv
count_vowels(s)¶
Write a function count_vowels(s) that returns the number of letters
a, e, i, o, u, A, E, I, O, U in
a given string s (the return value is of type integer).
Examples:
In [ ]: count_vowels('This is a test')
Out[ ]: 4
In [ ]: count_vowels('aoeui')
Out[ ]: 5
In [ ]: count_vowels('aoeuiAOEUI')
Out[ ]: 10
In [ ]: count_vowels('N0 v0w3ls @t @ll ln thls strlng')
Out[ ]: 0
count_chars(s)¶
Implement a function count_chars(s) which takes a string s and
returns a dictionary. The dictionary’s keys are the set of characters
that occur in string s. The value for each key is the number of
times that this character occurs in the string s.
Examples:
In [ ]: count_chars('x')
Out[ ]: {'x': 1}
In [ ]: count_chars('xxx')
Out[ ]: {'x': 3}
In [ ]: count_chars('xxxyz')
Out[ ]: {'x': 3, 'y': 1, 'z': 1}
In [ ]: count_chars('Hello World')
Out[ ]: {'H': 1, 'e': 1, 'l': 3, 'o': 2, ' ': 1, 'W': 1, 'r': 1, 'd': 1}
count_sub_in_file(filename, s)¶
Write a function count_sub_in_file(filename, s) that takes two
arguments: the substring s (of type string) and a filename (of type
string). The function should return the number of occurrences of s
in the file given through filename. (Note that each string object
has a method count that should be used here.)
You can test your function count_sub_in_file on Alice’s Adventures
in Wonderland:
How often does the substring “Alice” occur? Expect more than one hundred.
How often does the substring “Rabbit” occur? Expect a smaller number.
Modify the function count_sub_in_file(filename, s) so that if the
file with name filename cannot be opened, the integer value -1
is returned (instead of the number of substrings s).
traffic_light(load)¶
A function traffic_light(load) that takes a floating point number
load. The function should return the string:
“green” for values of
loadbelow 0.7.“amber” for values of
loadequal to or greater than 0.7 but smaller than 0.9“red” for values of
loadequal to 0.9 or greater than 0.9
Example:
In [ ]: traffic_light(0.5)
Out[ ]: 'green'
venv and rich¶
Create a virtual environment (using
python -m venv venv) and activate the environmentInstall the
richpython package usingpipInstall the
cowsaypython package usingpipExplore the capabilities of
richandcowsayby running (and inspecting) each of the example programs:
No code needs to be submitted for this task.
Questions:
what features of the
richpackage might be useful for your work (if any)?
pixi and rich (optional)¶
Optional advanced task if you feel ambitious, and want to learn about pixi.
install pixi
create a new directory and change into that directory:
All pixi installation files will be in this directory and below. You can later safely delete this directory and all the contents. You can also inspect the files and (hidden) subdirectories that pixi creates.
using the installed
pixitool, install python 3.12 or 3.13, andrichandcowsay.Hint: for
richa conda-forge package and a PyPI package exists (so this can be installed as a conda package or via pip). Forcowsaythere is no conda-package, so this has to be installed via pip.activate the pixi environment (
pixi shell) and execute some of the rich examples (such aspython emoji.py) check your installation was successful.Alternatively, instead of activating the pixi environment, you can also run the program
emoji.pythrough the pixi environment using the commandpixi run python emoji.py.
Please submit your file lab4.py for this assignment.
Additional (voluntary) tasks are available in lab4-extra.
End of lab4.