code
in the search window at the bottom of
the screen, which should cause a clickable icon to appear). Open the
folder that you created (by selecting File and Open
Folder from the top menu). Next, install the Pylance and
Jupyter extensions. You should remember how to do this from the first
lecture, but there are also instructions.
Create a new Jupyter notebook called hello.ipynb
.
(For example, you could select File and then New
File... from the top menu.) In the cell at the top of the
notebook, enter
print("Six slippery snakes slowly slithered south.")
SHIFT-ENTER
and check that the message is printed.
(There is more about basic interaction with VS Code in
this video.)
SHIFT-ENTER
, and make sure that you understand
what is going on. Guided by the comments in the multi-pangrams
notebook, download open any other notebooks that will help you to
consolidate your understanding. Edit the code in various ways
to add extra functionality or try different approaches or improve
the documentation.
Introduction to Python | View | Download | |
List comprehension | View | Download | |
Control structures | View | Download | |
Data Types | View | Download | |
String formatting | View | Download | |
Defining functions | View | Download | |
Lists | View | Download | |
Multi-pangrams | View | Download | |
Mutability and equality | View | Download | |
Scope of variables | View | Download | |
Strings | View | Download |
SQRT(x)
which
returns the square root of a given positive number $x$. Of course
there are built-in functions math.sqrt
and
numpy.sqrt
for this in the math
and
numpy
libraries, and in practice you would use those.
However, the aim here is to write your own function as an exercise,
and enter in the online test system. Some questions in that system
will allow you to import libraries, but this one will not.
np.sqrt()
.
SQRT
(not sqrt
or square_root
or
anything else) and it must accept a single argument called x
and not have any other arguments. It is very common when programming
that you need to follow specifications like this, to ensure that your
code will interact correctly with other code.
SQRT(x)
with a return
statement to return
the final answer.
SQRT(100)
in your notebook to check that your function works correctly. You should
remove such lines before entering your code in the online test; just enter
the function definition.
filter_doubled(words)
which accepts a list
words
of words, and returns the sublist containing only
the doubled words from the original list. For example, if
words=["bonbon","aardvark","yoyo"]
then
filter_doubled(words)
should return
["bonbon","yoyo"]
. You might or might not want to start
by defining a separate function is_doubled(word)
which
checks whether a single word is doubled; the online test system will
accept your work either way. With the right approach, you can make
your code very short.
filter_doubled(words)
, so that counts as the "external
interface" of your code and must follow the specified naming conventions.
If you chose to define any auxilary functions, those will not be part
of the external interface so you can name them however you like.
As in the previous task, the code that you submit should not contain
any print statements or test cases.
pisano_sequence(N,n)
and
pisano_period(N)
as follows.
pisano_sequence(N,n)
should return a
list containing the first $n$ Fibonacci numbers, reduced modulo $N$.
If $n$ and $N$ are not both strictly positive integers, then your
function should return False
. You should include a
docstring in your function definition.
pisano_sequence(N,n)
will start to repeat. For
example, when $N=2$ the sequence is $0,1,1,0,1,1,0,1,1,0,\dotsb$,
repeating with period $3$. The function pisano_period(N)
should return the period for the Pisano sequence modulo $N$.
It should return False
if $N$ is not a strictly positive
integer. You should include a docstring in your function
definition.
pisano_sequence(N,n)
and pisano_period(N)
are part of your external interface and so must follow the specified
naming conventions. Again, your code should not print anything or
contain any test cases.