Library 8
Todays exercises look like those from session 6.
Today you must not write the functions.
You must write tests for the functions then we shall write the functions.
All the solutions to the following exercises MUST
- Be written in python, and, apart from testing what they are required to test, they must
- Contain a copyright notice re the example below.
Model
You MUST use the following as a model:
|
|
In this example the individual example is lines 13 through 27. You MUST insert your name in your solution corresponding to line 15 above.
So now your exercise is writing comprehensive tests for the exercises below.
Submit your solutions a zip to nmla@iba.dk.
Exercise OSD.8.1
Write a function factorial(n)
where n
is a
positive integer. The function must return an
integer, -1
if there is an error.
Exercise OSD.8.2
Write a function max_of_3(a, b, c) where
a,
b, and
c` are numbers. The function must return
the largest of the three numbers.
Exercise OSD.8.3
Write a function word_freq(s, dic)
where s
is
a string, the function must find the words of s
and count them. dic
is a dictionary of the counted
word frequencies.
Exercise OSD.8.4
Write a function unique_words(dic)
where
dic
is a dictionary of word frequencies. The
function must return a collection of distinct
words from dic
.
Exercise OSD.8.5
Write a function is_perfect(n)
that checks
whether n
is a perfect number, True
, or
not, False
.
Exercise OSD.8.6
For humans and race horses velocity is not kilometers per hour or miles per hour (mph), but rather minutes per mile, or minutes per kilometer.
This is often called pace.
Write two functions
v2p(f, mode='metric')
wheref
is a floating point number with the unit miles per hour, or kilometers per hour; this function must return a pace with a unit ofmm:ss/m
ormm:ss/km
, calls for a tuple, eh?p2v(p, mode='metric')
wherep
is a tuple ofmm:ss/m
ormm:ss/km
. This function must return a floating point number containing the corresponding velocity.
Both functions may take metric
or imperial
for
mode
Exercise OSD.8.7
Write a function password_entropy(pwd)
where pwd
is a string containing a prospective password. The
entropy E
is calculated as
E = log2(N ** L)
where N
is the number of allowed characters in the
password, and L
is the length of the password.
Exercise OSD.8.8
Write a function acc_interest(a, t, i)
where
a
is an amount, t
is the number of terms,
and i
is the interest such
that 5% will be given as 5
. The function must
return the the accumulated after
after t
terms given the interest i
.
Exercise OSD.8.9
Write a function vol_sphere(r)
that returns
the volume of a sphere with the radius r
.
Exercise OSD.8.10
Write a method distance(p1, p2)
that
calculates and returns the distance between
p1
and p2
. The distance is the 2-dimensional
distance in the plane. p1
and p2
are objects
defined by
|
|
Exercise OSD.8.11
Same as OSD.8.10 except the distance must be between two points of latitude, longitude on the surface of the earth. Use
|
|
The distance must be calculated with the
Vincenty Formulae as implemented in geopy
.
Exercise OSD.8.12
Write a function acronym(s)
that receives a
phrase as input and returns the corresponding
acronym as string. An acronym is constructed
from the first letter of each word in s
.
An acronym must be all caps even if the word
in the constituent string
does not have caps.
Exercise OSD.8.13
Write a function numerologize(s)
that receives
a string, a name, as input. The function returns
a number as the sum of the numeric values of the
letters in the string. a
is 1, b
is 2, …
z
is 26. Space is 0
. No punctuation.
Exercise OSD.8.14
Write a function caesarE(s, key)
that encrypts
and returns a string s
with Caesars Cipher,
a substitution cipher where each character
is substituted with the character key
steps down the alphabet.
All characters will be converted to uppercase, punctuation and spaces will be removed. All positions will be modulo the length of the alphabet.
Example:
|
|
Exercise OSD.8.15
Write a function caesarD(s, key)
that decrypts
and returns a string s
that was encrypted with
Caesars Cipher re OSD.8.14.
Exercise OSD.8.16
Write a function that emulates the Unix command wc
(word count).
The function wc(s)
receives a string as input,
and returns a tuple of three values,
the number of lines, the number of words,
and the number of characters in s
.
Example:
|
|
When verifying, do not forget the invisible characters.