Question
''' KIT103 Assignment 4: Fun with Numbers Submission script Name: Genemo Muhammed ID: 539650 Enter your answers to each question below by completing each function.
'''
KIT103 Assignment 4: Fun with Numbers
Submission script
Name: Genemo Muhammed
ID: 539650
Enter your answers to each question below by completing each
function. After answering a question run this script and test
your implementation in the IPython console.
'''
sid = '539650' #Replace this with your student ID as a string
#%% Question 5: GCD from a prime factorisations
from collections import Counter
def prime_factor_gcd(a, b):
'''Returns gcd(a, b), calculated from their prime factorisations.'''
factors_a = Counter(factor_list(a))
factors_b = Counter(factor_list(b))
exp = factors_a & factors_b
gcd = 1
for p in exp:
gcd *= p ** exp[p]
return gcd
#%% Question 6: convert_base
import string
digits = string.digits + string.ascii_uppercase
# Q6a: Record your FOUR test cases here. Remember to replace the example one provided.
convert_base_test_cases = [
('42', 10, 10, '42'), # For each test case briefly explain why you chose it/what it's testing
(None, None, None, None), # Explanation...
(None, None, None, None), # Explanation...
(None, None, None, None) # Explanation...
]
# Q6b: Implement your solution
#Declare any additional helper function here
def convert_base(n, b1, b2):
'''
Converts n, a string representing a number in base b1, to a
string representing the same value in base b2. 2 <= b1, b2 <= 36.
'''
return None
#%% Question 7: Vigenere Cipher
from string import ascii_uppercase
ALPHABET = ascii_uppercase + ' '
'''
Q7a: Test cases
Briefly describe the test cases and why you chose them:
'''
encryption_tests = [
('', '', ALPHABET, ''), #use this structure
('', '', ALPHABET, ''), #replace ALPHABET if using different sequence of symbols
]
decryption_tests = [ #add test cases
]
def to_numbers(text, symbols=ALPHABET):
'''Returns a list of the positions of characters from ``text`` in ``symbols``.'''
return [ symbols.index(a) for a in text if a in symbols ]
def to_text(indices, symbols=ALPHABET):
'''Returns a string using the given ``indices`` into ``symbols``.'''
return ''.join(symbols[n] for n in indices)
def vencrypt(message, key, symbols=ALPHABET):
'''
Encrypts the ``message`` using a Vigenere cipher with the given ``key``.
Ignores any character not in ``symbols``.
'''
cipher = to_numbers(message, symbols)
shifts = to_numbers(key, symbols)
for i in range(len(cipher)):
cipher[i] = cipher[i] #<-- Q7b: You must replace this with the encryption expression
return to_text(cipher, symbols)
def vdecrypt(ciphertext, key, symbols=ALPHABET):
'''
Decrypts the ``ciphertext`` using a Vigenere cipher with the given ``key``.
'''
#Q7c: Implement this function
return None
# End of answers
#%% Provided functions for Question 5
from math import floor, sqrt
def primes(n):
'''Returns the set of primes between 2 and n, inclusive.'''
primes = set(range(2, n+1))
for k in range(2, floor(sqrt(n))+1):
if k in primes:
primes.difference_update(range(k**2, n+1, k))
return primes
def primes_list(n):
'''Returns a sorted list of primes between 2 and n, inclusive.'''
return sorted(primes(n))
def factors_for(n):
'''Returns the list of prime factors of n.'''
factors = []
iprimes = iter(primes_list(n))
while n > 1:
p = next(iprimes)
while n % p == 0:
n = n // p
factors.append(p)
return factors
# End of provided functions
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started