NML Says

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

  1. Be written in python, and, apart from testing what they are required to test, they must
  2. Contain a copyright notice re the example below.

Model

You MUST use the following as a model:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
'''
    testsuite.py
    
    Copyright (c) 2025 Authors
    Licensed under the BSD-3 License,
    please refer to the LICENSE document
'''

import unittest
from lib8 import *

class Testing(unittest.TestCase):
    '''
    OSD.8.0
    Copyright (c) 2025 Niels Müller Larsen
    Licensed under the BSD-3 License,
    please refer to the LICENSE document
    '''
    def test_isOdd1(self):
        self.assertTrue(isOddInt(1))
    def test_isOdd2(self):
        import sys
        self.assertTrue(isOddInt(sys.maxsize))
    def test_isOdd3(self):
        self.assertFalse(isOddInt(2**53))
    def test_isOdd4(self):
        self.assertTrue(isOddInt(2**63-1))

if __name__ == '__main__':
    unittest.main()

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

  1. v2p(f, mode='metric') where f is a floating point number with the unit miles per hour, or kilometers per hour; this function must return a pace with a unit of mm:ss/m or mm:ss/km, calls for a tuple, eh?
  2. p2v(p, mode='metric') where p is a tuple of mm:ss/m or mm: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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
# point.py
# class definition of a point
# x and y are integers
class Point:
    def __init__(self, x, y):
        self.x = x
        self.y = y
    
    def distance(self, o):
        return #...

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
# geopoint.py
# class definition of a point
# lat and lon are are floats
class GeoPoint:
    def __init__(self, lat, lon):
        self.lon = lat
        self.lat = lon
    
    def distance(self, o):
        return #...

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:

1
2
ciphertext = caesarE('Jacta est alea', 3)  
print(ciphertext)   #  `MDFWDHVWDOHD`

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:

1
2
3
4
5
6
7
$ cat test.txt
qwert,
abc 12a34
hello world!
$ cat test.txt | wc
       3       5      30
$

When verifying, do not forget the invisible characters.