NML Says

Library 9

Todays exercises look like those from sessions 6 and 8.

Today you must write the tests, and then 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.
  3. You may work in pairs
  4. This time hand in as repos re the README of our materials.

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
31
32
33
34
35
36
37
38
39
40
41
42
'''
    testsuite.py
    
    Copyright (c) 2025 Author
    Licensed under the BSD-3 License,
    please refer to the LICENSE document
'''

import unittest
from lib9 import *

class Testing(unittest.TestCase):
    '''
    OSD.9.0
    Copyright (c) 2025 
    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))

    '''
    OSD.9.1
    Copyright (c) 2025
    Licensed under the BSD-3 License,
    please refer to the LICENSE document
    '''
    def test_VigenE0(self):
        self.assertTrue(vigenE(), xyx)
    # etc 
    # etc


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

The file lib9.py must be modelled after lib8.py and lib6.py.

Exercise OSD.9.1

Write a function vigenE(s, key) that encrypts and returns a string s with the Vigenére Cipher, a substitution cipher where each character is substituted with the character from another alphabet according to a key. Please refer to the quote of Aumasson above.

All characters MUST be converted to uppercase, punctuation and spaces must be removed. All positions will be modulo the length of the alphabets.

Example:

1
2
ciphertext = vigenE('DUH', 'THEY DRINK THE TEA')  
print(ciphertext)   #  `WBLBXYLHRWBLWYH`

Exercise OSD.9.2

Write a function vigenD(key, s) that decrypts and returns a string s that was encrypted with The Vigenére Cipher re OSD.9.1.

Exercise OSD.9.3

Write a function bigram(s) that receives a text (string) and returns a dictionary of bigram frequencies. For info on bigrams pls refer to https://en.wikipedia.org/wiki/bigram

Exercise OSD.9.4

Write a function trigram(s) that receives a text (string) and returns a dictionary of trigram frequencies. For info on trigrams pls refer to https://en.wikipedia.org/wiki/trigram

Exercise OSD.9.5

Now based on OSD.8.3, OSD.9.3, and OSD.9.4 try to write a function n_gram(s, n).

The function must take s, a string, as input with an integer n where n > 0.

The function must be capable of creating

  • letter frequencies re OSD.8.3,
  • bigram frequencies re OSD.9.4, and
  • trigram frequencies re OSD.9.4

where n is 1, 2, and 3 respectively.