Solutions
Contents
Solutions#
Question 1#
1
. Use the REPL (read eval print loop) to carry out the following calculations:
1
. \(3 + 8\)
2
. \(3 / 7\)
3
. \(456 / 21\)
4
. \(\frac{4 ^ 3 + 2}{2\times 5} - 5 ^ {\frac{1}{2}}\)
Click the toggle (“Click to Show”) to see a demo of this:
Question 2#
2
.. Install the Python plugin for VScode.
The How to install VScode plugins section describes how to do this and includes an animation with a demonstration.
Question 3#
3
. Use the command line and a python script written in VScode to solve the following problems:
1
. Find the solutions to the following equation: \(x ^ 2 - 3 x + 2 = 1\).
The code to do this:
import sympy as sym
x = sym.Symbol("x")
equation = sym.Eq(x ** 2 - 3 * x + 2, 1)
print(sym.solveset(equation))
{3/2 - sqrt(5)/2, sqrt(5)/2 + 3/2}
Click the toggle (“Click to Show”) to see a demo running this:
2
. Differentiate the following function: \(f(x) = \cos(x) / 4\)
import sympy as sym
x = sym.Symbol("x")
f = sym.cos(x) / 4
print(sym.diff(f, x))
-sin(x)/4
Click the toggle (“Click to Show”) to see a demo running this:
3
. Find the determinant of \(A = \begin{pmatrix} 1 / 5 & 1\\1 & 1\end{pmatrix}\).
import sympy as sym
A = sym.Matrix([[sym.S(1) / 5, 1], [1, 1]])
print(sym.det(A))
-4/5
Click the toggle (“Click to Show”) to see a demo running this:
4
. Count the number of ways of picking 2 letters from “ABCD” where order
does not matter.
import itertools
combinations = tuple(itertools.combinations("ABCD", r=2))
print(len(combinations))
6
Click the toggle (“Click to Show”) to see a demo running this:
5
. Simulate the probability of picking a red token from a bag with 3 red
rokens, 5 blue tokens and a yellow token.
import random
def pick_token():
"""
Pick a token from a bag with 3 red and 5 blue and a yellow
Returns
-------
str
Either "Red", "Blue" or "Yellow"
"""
bag = ["Red"] * 3 + ["Blue"] * 5 + ["Yellow"]
return random.choice(bag)
number_of_repetitions = 10000
random.seed(0)
selections_with_red = [
pick_token() == "Red" for repetition in range(number_of_repetitions)
]
print(sum(selections_with_red) / number_of_repetitions)
0.3349
Click the toggle (“Click to Show”) to see a demo running this:
6
. Obtain the first 5 terms of the sequence defined by:
$$
\left\{
\begin{array}{l}
a_0 = 0,\\
a_1 = 2,\\
a_n = 3 a_{n - 1} + a_{n - 2}, n \geq 2
\end{array}
\right.
$$
def get_sequence(n):
"""
A function to give the nth term of the sequence using the recursive
definition.
Parameters
----------
n: int
The index of the number
Returns
-------
int
The nth term of the sequence number
"""
if n == 0:
return 0
if n == 1:
return 2
return 3 * get_sequence(n - 1) + get_sequence(n - 2)
for n in range(5):
print(get_sequence(n=n))
0
2
6
20
66
Click the toggle (“Click to Show”) to see a demo running this:
Question 4#
4
. Install theMarkdown all in one
plugin for markdown in VScode and then:
1
. Create a new filemain.md
.
2
. Write some basic markdown in it.
3
. Use the plugin to preview the rendered markdown.
Go to
Code > Preferences > Extensions
.Search for
Markdown
and clickInstall
onMarkdown all in one
.Create a new file, write some markdown and click on save to save it as
main.md
Click on the
Open Preview to the Side
icon in the top right.
Click the toggle (“Click to Show”) to see a demo of this: