Individual Assessment Coversheet
To be attached to the front of the assessment.
eVision Number: _____________________________________________________
Indicate Yes No
Plagiarism report attached
Declaration:
I declare that this assessment is my own original work except for source material explicitly acknowledged. I also
declare that this assessment or any other of my original work related to it has not been previously, or is not being
simultaneously, submitted for this or any other course. I also acknowledge that I am aware of the Institution’s
policy and regulations on honesty in academic work as set out in the Eduvos Conditions of Enrolment, and of the
disciplinary guidelines applicable to breaches of such policy and regulations.
Signature Date
Eduvos (Pty) Ltd. (formerly Pearson Institute of Higher Education) is registered with the Department of Higher Education and Training as a private higher education
institution under the Higher Education Act, 101, of 1997. Registration Certificate number: 2001/HE07/008
Lecturer’s Comments:
Marks Awarded: %
Signature Date
Individual Assessment Coversheet 2021 | V1.0
Question 1
CODE:
def string_Permutation(w = ""):
if not w:
return []
Final_result = string_Permutation(w[1:])
if w[0] not in Final_result:
Final_result.append(w[0])
return Final_result
name = input("Enter name:")
print(string_Permutation(name))
Output:
(Patra, 2020)
Question 2.1
CODE:
A
#Question 2.1
from collections import deque
from queue import LifoQueue
class Empty(Exception):
pass
class ArrayStack:
def __init__(self):
self._data = []
def __len__(self):
return len(self._data)
def is_empty(self):
return len(self._data) == 0
def push(self,e):
self._data.append(e)
def top(self):
if self.is_empty():
raise Empty('Stack is empty')
return self._data[-1]
def pop(self):
if self.is_empty():
raise Empty('Stack is empty')
return self._data.pop()
def transfer(self):
S = [10,25,15,7]
T = []
print("Stack S: ",S)
print("Lenght of stack S before the transfer:",len(S))
print("Lenght of stack T before the transfer:",len(T))
print("Item at the top of stack S:",S[0])
while S:
T.append(S.pop())
print("The transfer was succesfull")
print("Item at the top of stack T",T[0])
print("Lenght of stack S after Transfer:",len(S))
print("Stack T after the transfer: ",T)
print("Stack S after the transfer:", S)
arr = []
ArrayStack.transfer(arr)
(GeeksforGeeks, 2022)
(c)
Output:
B)
Question 2.2
The item at the top of the stack is 10, T is empty
The transfer function uses the first stack (Stack S), which then pop the last value of this
stack off from stack, when this value is popped off of the stack, the value will then
append to the second stack(Stack T).
This will happen in a loop until The stack S is empty and stack T is full with Stack S’s
values, but stack T will have Stack S’s values in the reverse order since the last value of
Stack S is popped first and appended first to Stack T.
The length of Stack S before the transfer is 4 and the length of stack T before the
transfer is 0
When the first loop is done the length of Stack S would be 3 and T would be 1.
When the transfer is done the length of Stack S is 0 and the length of stack T is 4. The
item at the top of stack T is now 7m, and stack S is empty.
(Anderson, n.d.)
Question 2.3
I would firstly find the length of the stacks, stack T has a length of 4.
If I minus 1 from the length of the stack I would get the last value. From there I would
pop the last value from stack T and append it to stack S.
This will give us the output of S =[9,4,5], since the new value wil be placed op top of the
older stack.
I will repeat this by using a downward counting loop. So the variable will start at 3( the
last value in the stack), then the value will go to 2(The 2nd last item in the stack). This
will then pop this item off Stack T and then append the item to stack S, where stack S
would be the following S=[8,9,4,5]
This loop will end when Stack T is empty and all the items will be inserted into stack S
by using the push function of the hash table class.
Question 3.1
class HashTable(object):
def __init__(self, size=18):
self.array = [None] * size #Initiate array with empty values
def hash(self, key):
lenght = len(self.array) #Hash function
return hash(key) % lenght
def add(self,key,data):
index = self.hash(key)
if self.array[index] is not None:
for kcp in self.array[index]: #Before storing a key value, check if
it exists
if kcp[0] == key:
kcp[1] = data #If key is found, update the new value
break
else:
self.array[index].append([key, data]) #If loop is not broken,
add the new key pair to the end
else:
self.array[index] = [] #Empty index, create a list and add key pair
value
self.array.append([key,data])
def retrieve(self,key):
index = self.hash(key)
if self.array[index] is None:
raise KeyError()
else: #kvp = key value pair
for kvp in self.array[index]: # loop through all kvp, to see if
the key exist, if it exits return the value
if kvp[0] == key:
return kvp[1]
raise KeyError() #If there was no return, the key didnt exists
def Delete(self,key):
index = self.hash(key)
if self.array[index] is None:
raise KeyError()
else:
for kvp in self.array[index]:
if kvp[0] == key:
self.array.remove(kvp)
(Coderbook, 2019)
(TutorialsPoint, n.d.)
(freecodecamp, 2020)
Question 3.2
A)
from binarytree import Node
from binarytree import build
class binarytree:
def __init__(self, data):
self.left = None
self.right = None
self.data = data
# Insert Node
def insert(self, data):
if self.data:
if data < self.data:
if self.left is None:
self.left = binarytree(data)
else:
self.left.insert(data)
else:
if data > self.data:
if self.right is None:
self.right = binarytree(data)
else:
self.right.insert(data)
else:
self.data = data
def Preorder(self, root):
res = []
if root:
res.append(root.data)
res = res + self.Preorder(root.left)
res = res + self.Preorder(root.right)
return res
def inorder(self,root):
res = []
if root:
res = self.inorder(root.right)
res.append(root.data)
res = res + self.inorder(root.left)
return res
root = binarytree("M")
root.insert("A")
root.insert("F")
root.insert("X")
root.insert("N")
root.insert("U")
root.insert("E")
print(root.inorder(root))
print(root.Preorder(root))
nodes_1 = root.Preorder(root)
binary_tree = build(nodes_1)
print(binary_tree)
nodes_2 = root.inorder(root)
binary_tree2 = build(nodes_2)
print(binary_tree2)
(GeeksforGeeks, 2022)
(Balasubramaniam, 2020)
Output:
B)
(GeeksforGeeks, 2022)
References
Anderson, J. (n.d.). How to Implement a Python Stack. Retrieved from Real Python:
https://realpython.com/how-to-implement-python-stack/
Balasubramaniam, S. (2020, September 26). Using the Binary Tree Data Structure in Python. Retrieved
from SectionIO: https://www.section.io/engineering-education/binary-tree-data-structure-
python/
c. (n.d.). Implementing a Stack using a Python List. In https://realpython.com/how-to-implement-
python-stack/, Data Structures & Algorithms (pp. 233-235). Don Fowley.
Coderbook. (2019, January 21). How to Create a Hash Table From Scratch in Python. Retrieved from
Coderbook: https://coderbook.com/@marcus/how-to-create-a-hash-table-from-scratch-in-
python/
freecodecamp. (2020, January 25). Hash Table Exaplained: What it is and how to implement it. Retrieved
from Freecodecamp: https://www.freecodecamp.org/news/hash-tables/
GeeksforGeeks. (2022, November 06). Print Binary Tree in 2-Dimenstions. Retrieved from Geeksfor
Geeks: https://www.geeksforgeeks.org/print-binary-tree-2-dimensions/
GeeksforGeeks. (2022, July 13). Stack in Python. Retrieved from GeeksforGeeks:
https://www.geeksforgeeks.org/stack-in-python/
GeeksforGeeks. (2022, November 8). Tree Transversals. Retrieved from GeeksforGeeks.
Patra, S. K. (2020, September 16). Find all non repeated characters in a string. Retrieved from csinfo360:
https://www.csinfo360.com/2020/09/find-all-non-repeated-characters-in-string.html
TutorialsPoint. (n.d.). Python-Hash Table. Retrieved from tutorialsPoint:
https://www.tutorialspoint.com/python_data_structure/python_hash_table.htm