Instructions
Objective
Write a python assignment program to implement IP addresses in programming language.
Requirements and Specifications
Source Code
import unittest
import random
class IPAddressConverter:
def numToIpAddress(self, num):
comps = []
for i in range(4):
comps.insert(0, str(num % 256))
num = num // 256
return ".".join(comps)
def ipAdressToNum(self, ip):
parts = ip.split(".")
res = 0
for i in range(4):
res = res * 256 + int(parts[i])
return res
class IPAddressConverterTest(unittest.TestCase):
def test_numToIpAddress_1(self):
num = 2130706433
exp = "127.0.0.1"
converter = IPAddressConverter()
res = converter.numToIpAddress(num)
print('NumToIPAddress Test #1')
print('Expected: ', exp)
print('Actual: ', res)
print()
self.assertEqual(res, exp, "FAIL!")
def test_numToIpAddress_2(self):
num = 0
exp = "0.0.0.0"
converter = IPAddressConverter()
res = converter.numToIpAddress(num)
print('NumToIPAddress Test #2')
print('Expected: ', exp)
print('Actual: ', res)
print()
self.assertEqual(res, exp, "FAIL!")
def test_numToIpAddress_3(self):
num = 1
exp = "0.0.0.1"
converter = IPAddressConverter()
res = converter.numToIpAddress(num)
print('NumToIPAddress Test #3')
print('Expected: ', exp)
print('Actual: ', res)
print()
self.assertEqual(res, exp, "FAIL!")
def test_numToIpAddress_4(self):
num = 255
exp = "0.0.0.255"
converter = IPAddressConverter()
res = converter.numToIpAddress(num)
print('NumToIPAddress Test #4')
print('Expected: ', exp)
print('Actual: ', res)
print()
self.assertEqual(res, exp, "FAIL!")
def test_numToIpAddress_5(self):
num = 256
exp = "0.0.1.0"
converter = IPAddressConverter()
res = converter.numToIpAddress(num)
print('NumToIPAddress Test #5')
print('Expected: ', exp)
print('Actual: ', res)
print()
self.assertEqual(res, exp, "FAIL!")
def test_numToIpAddress_6(self):
num = 65536
exp = "0.1.0.0"
converter = IPAddressConverter()
res = converter.numToIpAddress(num)
print('NumToIPAddress Test #6')
print('Expected: ', exp)
print('Actual: ', res)
print()
self.assertEqual(res, exp, "FAIL!")
def test_numToIpAddress_7(self):
num = 16777216
exp = "1.0.0.0"
converter = IPAddressConverter()
res = converter.numToIpAddress(num)
print('NumToIPAddress Test #7')
print('Expected: ', exp)
print('Actual: ', res)
print()
self.assertEqual(res, exp, "FAIL!")
def test_numToIpAddress_8(self):
num = 4294967295
exp = "255.255.255.255"
converter = IPAddressConverter()
res = converter.numToIpAddress(num)
print('NumToIPAddress Test #8')
print('Expected: ', exp)
print('Actual: ', res)
print()
self.assertEqual(res, exp, "FAIL!")
def test_numToIpAddress_9(self):
num = 777777777
exp = "46.91.242.113"
converter = IPAddressConverter()
res = converter.numToIpAddress(num)
print('NumToIPAddress Test #9')
print('Expected: ', exp)
print('Actual: ', res)
print()
self.assertEqual(res, exp, "FAIL!")
def test_numToIpAddress_10(self):
num = 100000000
exp = "5.245.225.0"
converter = IPAddressConverter()
res = converter.numToIpAddress(num)
print('NumToIPAddress Test #10')
print('Expected: ', exp)
print('Actual: ', res)
print()
self.assertEqual(res, exp, "FAIL!")
def test_ipAddressToNum_1(self):
ip = "127.0.0.1"
exp = 2130706433
converter = IPAddressConverter()
res = converter.ipAdressToNum(ip)
print('IPAddressToNum Test #1')
print('Expected: ', exp)
print('Actual: ', res)
print()
self.assertEqual(res, exp, "FAIL!")
def test_ipAddressToNum_2(self):
ip = "0.0.0.0"
exp = 0
converter = IPAddressConverter()
res = converter.ipAdressToNum(ip)
print('IPAddressToNum Test #2')
print('Expected: ', exp)
print('Actual: ', res)
print()
self.assertEqual(res, exp, "FAIL!")
def test_ipAddressToNum_3(self):
ip = "0.0.0.1"
exp = 1
converter = IPAddressConverter()
res = converter.ipAdressToNum(ip)
print('IPAddressToNum Test #3')
print('Expected: ', exp)
print('Actual: ', res)
print()
self.assertEqual(res, exp, "FAIL!")
def test_ipAddressToNum_4(self):
ip = "0.0.0.255"
exp = 255
converter = IPAddressConverter()
res = converter.ipAdressToNum(ip)
print('IPAddressToNum Test #4')
print('Expected: ', exp)
print('Actual: ', res)
print()
self.assertEqual(res, exp, "FAIL!")
def test_ipAddressToNum_5(self):
ip = "0.0.1.0"
exp = 256
converter = IPAddressConverter()
res = converter.ipAdressToNum(ip)
print('IPAddressToNum Test #5')
print('Expected: ', exp)
print('Actual: ', res)
print()
self.assertEqual(res, exp, "FAIL!")
def test_ipAddressToNum_6(self):
ip = "0.1.0.0"
exp = 65536
converter = IPAddressConverter()
res = converter.ipAdressToNum(ip)
print('IPAddressToNum Test #6')
print('Expected: ', exp)
print('Actual: ', res)
print()
self.assertEqual(res, exp, "FAIL!")
def test_ipAddressToNum_7(self):
ip = "1.0.0.0"
exp = 16777216
converter = IPAddressConverter()
res = converter.ipAdressToNum(ip)
print('IPAddressToNum Test #7')
print('Expected: ', exp)
print('Actual: ', res)
print()
self.assertEqual(res, exp, "FAIL!")
def test_ipAddressToNum_8(self):
ip = "255.255.255.255"
exp = 4294967295
converter = IPAddressConverter()
res = converter.ipAdressToNum(ip)
print('IPAddressToNum Test #8')
print('Expected: ', exp)
print('Actual: ', res)
print()
self.assertEqual(res, exp, "FAIL!")
def test_ipAddressToNum_9(self):
ip = "46.91.242.113"
exp = 777777777
converter = IPAddressConverter()
res = converter.ipAdressToNum(ip)
print('IPAddressToNum Test #9')
print('Expected: ', exp)
print('Actual: ', res)
print()
self.assertEqual(res, exp, "FAIL!")
def test_ipAddressToNum_10(self):
ip = "5.245.225.0"
exp = 100000000
converter = IPAddressConverter()
res = converter.ipAdressToNum(ip)
print('IPAddressToNum Test #10')
print('Expected: ', exp)
print('Actual: ', res)
print()
self.assertEqual(res, exp, "FAIL!")
class MonoalphabeticCipher:
def generateKey(self):
letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
result = ""
while len(letters) > 0:
r = random.randint(0, len(letters)-1)
result = result + letters[r]
del letters[r]
self.key = result
return result
def encrypt(self, plaintext):
result = ""
for c in plaintext:
if c.isalpha():
if c.islower():
result += self.key[ord(c) - ord('a')]
else:
result += self.key[ord(c.lower()) - ord('a')].upper()
else:
result += c
return result
def decrypt(self, ciphertext):
result = ""
for c in ciphertext:
if c.isalpha():
if c.islower():
result += chr(ord('a') + self.key.index(str(c)))
else:
result += chr(ord('a') + self.key.index(str(c.lower()))).upper()
else:
result += c
return result
class MonoalphabeticCipherTest(unittest.TestCase):
def _is_valid_key(self, key):
counters = {}
for c in key:
if not c.islower() or not c.isalpha():
return False
if c in counters:
return False
counters[c] = 1
return len(counters) == 26
def test_generateKey_1(self):
cipher = MonoalphabeticCipher()
print('Generate Key Test #1')
random.seed(1)
key = cipher.generateKey()
if len(key) != 26:
valid = False
else:
valid = self._is_valid_key(key)
self.assertTrue(valid, "Key is not valid!")
print()
def test_generateKey_2(self):
cipher = MonoalphabeticCipher()
print('Generate Key Test #2')
random.seed(2)
key = cipher.generateKey()
if len(key) != 26:
valid = False
else:
valid = self._is_valid_key(key)
self.assertTrue(valid, "Key is not valid!")
print()
def test_generateKey_3(self):
cipher = MonoalphabeticCipher()
print('Generate Key Test #3')
random.seed(3)
key = cipher.generateKey()
if len(key) != 26:
valid = False
else:
valid = self._is_valid_key(key)
self.assertTrue(valid, "Key is not valid!")
print()
def test_generateKey_4(self):
cipher = MonoalphabeticCipher()
print('Generate Key Test #4')
random.seed(4)
key = cipher.generateKey()
if len(key) != 26:
valid = False
else:
valid = self._is_valid_key(key)
self.assertTrue(valid, "Key is not valid!")
print()
def test_generateKey_5(self):
cipher = MonoalphabeticCipher()
print('Generate Key Test #5')
random.seed(5)
key = cipher.generateKey()
if len(key) != 26:
valid = False
else:
valid = self._is_valid_key(key)
self.assertTrue(valid, "Key is not valid!")
print()
def test_generateKey_6(self):
cipher = MonoalphabeticCipher()
print('Generate Key Test #6')
random.seed(6)
key = cipher.generateKey()
if len(key) != 26:
valid = False
else:
valid = self._is_valid_key(key)
self.assertTrue(valid, "Key is not valid!")
print()
def test_generateKey_7(self):
cipher = MonoalphabeticCipher()
print('Generate Key Test #7')
random.seed(7)
key = cipher.generateKey()
if len(key) != 26:
valid = False
else:
valid = self._is_valid_key(key)
self.assertTrue(valid, "Key is not valid!")
print()
def test_generateKey_8(self):
cipher = MonoalphabeticCipher()
print('Generate Key Test #8')
random.seed(8)
key = cipher.generateKey()
if len(key) != 26:
valid = False
else:
valid = self._is_valid_key(key)
self.assertTrue(valid, "Key is not valid!")
print()
def test_generateKey_9(self):
cipher = MonoalphabeticCipher()
print('Generate Key Test #9')
random.seed(9)
key = cipher.generateKey()
if len(key) != 26:
valid = False
else:
valid = self._is_valid_key(key)
self.assertTrue(valid, "Key is not valid!")
print()
def test_generateKey_10(self):
cipher = MonoalphabeticCipher()
print('Generate Key Test #10')
random.seed(10)
key = cipher.generateKey()
if len(key) != 26:
valid = False
else:
valid = self._is_valid_key(key)
self.assertTrue(valid, "Key is not valid!")
print()
def test_encrypt_1(self):
cipher = MonoalphabeticCipher()
cipher.key = "abcdefghijklmnopqrstuvwxyz"
plaintext = ''
exp = ''
res = cipher.encrypt(plaintext)
print('Encrypt Test #1')
print('Expected: ', exp)
print('Actual: ', res)
print()
self.assertEqual(res, exp, 'FAIL!')
def test_encrypt_2(self):
cipher = MonoalphabeticCipher()
cipher.key = "abcdefghijklmnopqrstuvwxyz"
plaintext = '!!!'
exp = '!!!'
res = cipher.encrypt(plaintext)
print('Encrypt Test #2')
print('Expected: ', exp)
print('Actual: ', res)
print()
self.assertEqual(res, exp, 'FAIL!')
def test_encrypt_3(self):
cipher = MonoalphabeticCipher()
cipher.key = "abcdefghijklmnopqrstuvwxyz"
plaintext = 'hello'
exp = 'hello'
res = cipher.encrypt(plaintext)
print('Encrypt Test #3')
print('Expected: ', exp)
print('Actual: ', res)
print()
self.assertEqual(res, exp, 'FAIL!')
def test_encrypt_4(self):
cipher = MonoalphabeticCipher()
cipher.key = "abcdefghijklmnopqrstuvwxyz"
plaintext = 'Hello'
exp = 'Hello'
res = cipher.encrypt(plaintext)
print('Encrypt Test #4')
print('Expected: ', exp)
print('Actual: ', res)
print()
self.assertEqual(res, exp, 'FAIL!')
def test_encrypt_5(self):
cipher = MonoalphabeticCipher()
cipher.key = "abcdefghijklmnopqrstuvwxyz"
plaintext = 'Hello World!'
exp = 'Hello World!'
res = cipher.encrypt(plaintext)
print('Encrypt Test #5')
print('Expected: ', exp)
print('Actual: ', res)
print()
self.assertEqual(res, exp, 'FAIL!')
def test_encrypt_6(self):
cipher = MonoalphabeticCipher()
cipher.key = "zyxwvutsrqponmlkjihgfedcba"
plaintext = ''
exp = ''
res = cipher.encrypt(plaintext)
print('Encrypt Test #6')
print('Expected: ', exp)
print('Actual: ', res)
print()
self.assertEqual(res, exp, 'FAIL!')
def test_encrypt_7(self):
cipher = MonoalphabeticCipher()
cipher.key = "zyxwvutsrqponmlkjihgfedcba"
plaintext = '!!!'
exp = '!!!'
res = cipher.encrypt(plaintext)
print('Encrypt Test #7')
print('Expected: ', exp)
print('Actual: ', res)
print()
self.assertEqual(res, exp, 'FAIL!')
def test_encrypt_8(self):
cipher = MonoalphabeticCipher()
cipher.key = "zyxwvutsrqponmlkjihgfedcba"
plaintext = 'hello'
exp = 'svool'
res = cipher.encrypt(plaintext)
print('Encrypt Test #8')
print('Expected: ', exp)
print('Actual: ', res)
print()
self.assertEqual(res, exp, 'FAIL!')
def test_encrypt_9(self):
cipher = MonoalphabeticCipher()
cipher.key = "zyxwvutsrqponmlkjihgfedcba"
plaintext = 'Hello'
exp = 'Svool'
res = cipher.encrypt(plaintext)
print('Encrypt Test #9')
print('Expected: ', exp)
print('Actual: ', res)
print()
self.assertEqual(res, exp, 'FAIL!')
def test_encrypt_10(self):
cipher = MonoalphabeticCipher()
cipher.key = "zyxwvutsrqponmlkjihgfedcba"
plaintext = 'Hello World!'
exp = 'Svool Dliow!'
res = cipher.encrypt(plaintext)
print('Encrypt Test #10')
print('Expected: ', exp)
print('Actual: ', res)
print()
self.assertEqual(res, exp, 'FAIL!')
def test_decrypt_1(self):
cipher = MonoalphabeticCipher()
cipher.key = "abcdefghijklmnopqrstuvwxyz"
ciphertext = ''
exp = ''
res = cipher.decrypt(ciphertext)
print('Decrypt Test #1')
print('Expected: ', exp)
print('Actual: ', res)
print()
self.assertEqual(res, exp, 'FAIL!')
def test_decrypt_2(self):
cipher = MonoalphabeticCipher()
cipher.key = "abcdefghijklmnopqrstuvwxyz"
ciphertext = '!!!'
exp = '!!!'
res = cipher.decrypt(ciphertext)
print('Encrypt Test #2')
print('Expected: ', exp)
print('Actual: ', res)
print()
self.assertEqual(res, exp, 'FAIL!')
def test_decrypt_3(self):
cipher = MonoalphabeticCipher()
cipher.key = "abcdefghijklmnopqrstuvwxyz"
ciphertext = 'hello'
exp = 'hello'
res = cipher.decrypt(ciphertext)
print('Decrypt Test #3')
print('Expected: ', exp)
print('Actual: ', res)
print()
self.assertEqual(res, exp, 'FAIL!')
def test_decrypt_4(self):
cipher = MonoalphabeticCipher()
cipher.key = "abcdefghijklmnopqrstuvwxyz"
ciphertext = 'Hello'
exp = 'Hello'
res = cipher.encrypt(ciphertext)
print('Decrypt Test #4')
print('Expected: ', exp)
print('Actual: ', res)
print()
self.assertEqual(res, exp, 'FAIL!')
def test_decrypt_5(self):
cipher = MonoalphabeticCipher()
cipher.key = "abcdefghijklmnopqrstuvwxyz"
ciphertext = 'Hello World!'
exp = 'Hello World!'
res = cipher.decrypt(ciphertext)
print('Decrypt Test #5')
print('Expected: ', exp)
print('Actual: ', res)
print()
self.assertEqual(res, exp, 'FAIL!')
def test_decrypt_6(self):
cipher = MonoalphabeticCipher()
cipher.key = "zyxwvutsrqponmlkjihgfedcba"
exp = ''
ciphertext = ''
res = cipher.decrypt(ciphertext)
print('Decrypt Test #6')
print('Expected: ', exp)
print('Actual: ', res)
print()
self.assertEqual(res, exp, 'FAIL!')
def test_decrypt_7(self):
cipher = MonoalphabeticCipher()
cipher.key = "zyxwvutsrqponmlkjihgfedcba"
exp = '!!!'
ciphertext = '!!!'
res = cipher.decrypt(ciphertext)
print('Decrypt Test #7')
print('Expected: ', exp)
print('Actual: ', res)
print()
self.assertEqual(res, exp, 'FAIL!')
def test_decrypt_8(self):
cipher = MonoalphabeticCipher()
cipher.key = "zyxwvutsrqponmlkjihgfedcba"
ciphertext = 'svool'
exp = 'hello'
res = cipher.decrypt(ciphertext)
print('Decrypt Test #8')
print('Expected: ', exp)
print('Actual: ', res)
print()
self.assertEqual(res, exp, 'FAIL!')
def test_decrypt_9(self):
cipher = MonoalphabeticCipher()
cipher.key = "zyxwvutsrqponmlkjihgfedcba"
ciphertext = 'Svool'
exp = 'Hello'
res = cipher.decrypt(ciphertext)
print('Decrypt Test #9')
print('Expected: ', exp)
print('Actual: ', res)
print()
self.assertEqual(res, exp, 'FAIL!')
def test_decrypt_10(self):
cipher = MonoalphabeticCipher()
cipher.key = "zyxwvutsrqponmlkjihgfedcba"
ciphertext = 'Svool Dliow!'
exp = 'Hello World!'
res = cipher.decrypt(ciphertext)
print('Decrypt Test #10')
print('Expected: ', exp)
print('Actual: ', res)
print()
self.assertEqual(res, exp, 'FAIL!')
Similar Samples
Dive into our extensive collection of programming homework samples at ProgrammingHomeworkHelp.com. Explore real-life examples showcasing our expertise in Python, Java, C++, and more. Each sample demonstrates our commitment to clarity, correctness, and efficiency in solving diverse programming challenges. Discover how our solutions can assist you in mastering programming concepts and achieving academic success.
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python
Python