-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemory_management.py
187 lines (147 loc) · 5.98 KB
/
memory_management.py
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# ====================================
# Name: Gustavo A. Rassi
# Project: Swift's Memory Management using python
# CCOM4025-LB5
# ====================================
import platform
import os
import random
class Memory:
# Constructor
def __init__(self):
self.rows = 10 # 10 rows for 2d array (matrix)
self.columns = 10 # 10 columns for 2d array (matrix)
self.original_array_1d = [] # Original Vector
self.original_array_2d = [] # Original Matrix
self.modified_array_1d = [] # Vector used for swapping
self.modified_array_2d = [] # Matrix used for swapping
#==========================================
# Menu of options
def displayMenu(self):
print("Menu:")
print("========================================================")
print("1. Create and initialize matrix")
print("2. Print matrix")
print("3. Access and print element from matrix and equivalent in vector")
print("4. Access and replace element from matrix and equivalent in vector")
print("5. Swap arrays")
print("0. Exit program")
print("========================================================")
#==========================================
# Initialize vector
def initializeArray1D(self):
for row in self.original_array_2d:
for element in row:
self.original_array_1d.append(element)
self.modified_array_1d = self.original_array_1d
#==========================================
# Initialize matrix
def initializeArray2D(self):
for _ in range(self.rows):
row = []
for _ in range(self.columns):
random_integer = random.randint(1,100)
row.append(random_integer)
self.original_array_2d.append(row)
self.modified_array_2d = self.original_array_2d
#==========================================
def _accessAndPrintElement(self, row, col):
# Access and print element in the 2D array
print(f"Matrix: Element in 2D array at ({row}, {col}) is {self.original_array_2d[row][col]}.")
self.getArray2D()
# Calculate index for 1D array equivalent
index = row * len(self.original_array_2d[0]) + col
# Access and print element in the 1D array
print(f"\nVector: Equivalent element in 1D array is {self.original_array_1d[index]}.")
# Print 1D array
print(self.getArray1D())
#==========================================
# Access and swap elements
def _accessAndReplaceElement(self, row, col):
# Extract element from 2d array (matrix)
element = self.original_array_2d[row][col]
# Calculate index to find the same element in both 2d & 1d arrays
index = row * len(self.original_array_2d[0]) + col
self.original_array_2d[row][col] = self.original_array_1d[index]
self.original_array_1d[index] = element
#==========================================
# Prints vector
def getArray1D(self):
return self.modified_array_1d
#==========================================
# Print matrix
def getArray2D(self):
for row in self.modified_array_2d:
print(row)
#==========================================
# Swap all the elements between matrix and vector
def swapElements(self):
print("2D array before swap:\n")
self.getArray2D()
input("press any key to continue...")
print("1D array before swap:\n")
print(self.getArray1D())
input("press any key to continue...")
# Copies elements from 2d array (matrix) to temporary 1D array by rows
temp_array = []
for row in self.modified_array_2d:
temp_array += row
# Elements from 1d array (vector) are copied to the 2d array (matrix)
index = 0
for i in range(self.rows):
for j in range(self.columns):
self.modified_array_2d[i][j] = self.modified_array_1d[index]
index += 1
# Elements from temporary array are passed to the 2d array (vector)
_index = 0
for element in temp_array:
self.modified_array_1d[_index] = element
_index += 1
print("2D array after swap:\n")
self.getArray2D()
input("press any key to continue...")
print("1D array after swap:\n")
print(self.getArray1D())
#=========================================================================
# MAIN
# Object instantiation
memory = Memory()
option = None
while option != 0:
# Display menu of options
memory.displayMenu()
option = int(input("Please select an option\n> "))
if option == 1:
if len(memory.getArray1D()) != 0:
print("Matrix is already initialized.")
else:
memory.initializeArray2D()
print("Matrix has been initialized.")
memory.initializeArray1D()
elif option == 2:
print("Array 2d:")
memory.getArray2D()
elif option == 3:
print("Enter the following values for the element you want to access and print...")
row = int(input("Row: "))
col = int(input("Column: "))
memory._accessAndPrintElement(row, col)
elif option == 4:
print("Enter the following values for the element you want to access and replace.")
row = int(input("Row: "))
column = int(input("Column: "))
memory._accessAndReplaceElement(row, column)
print("Element is replaced succesfully!")
elif option == 5:
memory.swapElements()
print("Arrays have swapped succesfully!")
input("Press any key to continue...")
elif option == 0: # Exit the program
print("Thank you for using the program! Exiting...")
else:
print(f"**Option {option} does not exist.**")
input("Press any key to continue...")
if platform.system() == "Windows":
os.system("cls") # Clear screen on windows
else:
os.system("clear") # Clear screen on mac