Skip to content

Commit

Permalink
Add Lab Assignment Solutions for Fall 2022
Browse files Browse the repository at this point in the history
  • Loading branch information
Mahrjose committed Jun 21, 2023
1 parent 9e62c67 commit 440ef77
Show file tree
Hide file tree
Showing 8 changed files with 2,304 additions and 0 deletions.
278 changes: 278 additions & 0 deletions Lab Assignment Solutions/Fall 2022/Assignment 02/Lab02_21101048.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,278 @@
# ========Lab 02============
# Name: Mirza Mahrab Hossain Rudra
# Student ID: 21101048
# Section 15
# ==========================


class CircularArray:
def __init__(self, lin, st, sz):
# Initializing Variables
self.start = st
self.size = sz
self.cir = [None] * len(lin)

# if lin = [10, 20, 30, 40, None]
# then, CircularArray(lin, 2, 4) will generate
# cir = [40, null, 10, 20, 30]

# To Do.
# Hints: set the values for initialized variables

index = self.start

for i in range(len(lin)):
self.cir[index] = lin[i]
index = (index + 1) % len(self.cir)

# Print from index 0 to len(cir) - 1
def printFullLinear(self): # Easy
# To Do

for i in range(len(self.cir)):
if i == len(self.cir) - 1:
print(self.cir[i])
else:
print(self.cir[i], end=", ")

# Print from start index and total size elements
def printForward(self): # Easy
# To Do

index = self.start

for i in range(self.size):
if i == self.size - 1:
print(self.cir[index])
else:
print(self.cir[index], end=", ")

index = (index + 1) % len(self.cir)

def printBackward(self): # Easy
# To Do

index = (self.start + self.size - 1) % len(self.cir)

for i in range(self.size):
if i == self.size - 1:
print(self.cir[index])
else:
print(self.cir[index], end=", ")

index -= 1
if index < 0:
index = len(self.cir) - 1

# With no null cells
def linearize(self): # Medium
# To Do

l = [None] * (self.size)
index = self.start

i = 0
while i < self.size:
l[i] = self.cir[index]
index = (index + 1) % len(self.cir)
i += 1

self.cir = l

# Do not change the Start index
def resizeStartUnchanged(self, newcapacity): # Medium
# To Do
newCircular = [None] * newcapacity

index = self.start
s = self.start
for i in range(self.size):
newCircular[index] = self.cir[s]
index = index + 1
s = (s + 1) % len(self.cir)

self.cir = newCircular

# This method will check whether the array is palindrome or not
def palindromeCheck(self): # Hard
# To Do

flag = False

index = self.start
j = (self.start + self.size - 1) % len(self.cir)

for i in range(self.size // 2):

if self.cir[index] == self.cir[j]:
flag = True
else:
flag = False
break

index = (index + 1) % len(self.cir)

j = j - 1
if j < 0:
j = len(self.cir) - 1

if flag == True:
print("This array is a palindrome")
else:
print("This array is NOT a palindrome")

# This method will sort the values by keeping the start unchanged
def sort(self):
# To Do

for i in range(self.size):

for j in range(i + 1, self.size):

x = (i + self.start) % len(self.cir)
y = (j + self.start) % len(self.cir)

if self.cir[x] > self.cir[y]:

temp = self.cir[x]
self.cir[x] = self.cir[y]
self.cir[y] = temp

# This method will check the given array across the base array and if they are equivalent interms of values return true, or else return false
def equivalent(self, cir_arr):
# To Do
isEqual = False
idx1 = self.start
idx2 = cir_arr.start

if self.size != cir_arr.size:
isEqual = False
return isEqual

else:
for _ in range(self.size):

if self.cir[idx1] == cir_arr.cir[idx2]:
isEqual = True

idx1 = (idx1 + 1) % len(self.cir)
idx2 = (idx2 + 1) % len(cir_arr.cir)

else:
isEqual = False
return isEqual

return isEqual

# the method take another circular array and returns a linear array containing the common elements between the two circular arrays.
def intersection(self, c2):
# To Do

cir1 = self.cir
idx1 = self.start
cir2 = c2.cir
temp = [None] * self.size
index3 = 0

for i in range(self.size):
idx2 = c2.start

for j in range(c2.size):

if cir1[idx1] == cir2[idx2]:
temp[index3] = cir1[idx1]
index3 += 1

idx2 = (idx2 + 1) % len(cir2)
idx1 = (idx1 + 1) % len(cir1)

linear = [None] * index3
for i in range(len(linear)):
linear[i] = temp[i]

return linear


# Tester class. Run this cell after completing methods in the upper cell and
# check the output


lin_arr1 = [10, 20, 30, 40, None]


print("==========Test 1==========")
c1 = CircularArray(lin_arr1, 2, 4)
c1.printFullLinear() # This should print: 40, None, 10, 20, 30
c1.printForward() # This should print: 10, 20, 30, 40
c1.printBackward() # This should print: 40, 30, 20, 10


print("==========Test 2==========")
c1.linearize()
c1.printFullLinear() # This should print: 10, 20, 30, 40


print("==========Test 3==========")
lin_arr2 = [10, 20, 30, 40, 50]
c2 = CircularArray(lin_arr2, 2, 5)
c2.printFullLinear() # This should print: 40, 50, 10, 20, 30
c2.resizeStartUnchanged(8) # parameter --> new Capacity
c2.printFullLinear() # This should print: None, None, 10, 20, 30, 40, 50, None


print("==========Test 4==========")
lin_arr3 = [10, 20, 30, 20, 10, None, None]
c3 = CircularArray(lin_arr3, 3, 5)
c3.printForward() # This should print: 10, 20, 30, 20, 10
c3.palindromeCheck() # This should print: This array is a palindrome


print("==========Test 5==========")
lin_arr4 = [10, 20, 30, 20, None, None, None]
c4 = CircularArray(lin_arr4, 3, 4)
c4.printForward() # This should print: 10, 20, 30, 20
c4.palindromeCheck() # This should print: This array is NOT a palindrome


print("==========Test 6==========")
lin_arr5 = [10, 20, -30, 20, 50, 30, None]
c5 = CircularArray(lin_arr5, 5, 6)
c5.printForward() # This should print: 10, 20, -30, 20, 50, 30
c5.sort()
c5.printForward() # This should print: -30, 10, 20, 20, 30, 50


print("==========Test 7==========")
lin_arr6 = [10, 20, -30, 20, 50, 30, None]
c6 = CircularArray(lin_arr6, 2, 6)
c7 = CircularArray(lin_arr6, 5, 6)
c6.printForward() # This should print: 10, 20, -30, 20, 50, 30
c7.printForward() # This should print: 10, 20, -30, 20, 50, 30
print(c6.equivalent(c7)) # This should print: True


print("==========Test 8==========")
lin_arr7 = [10, 20, -30, 20, 50, 30, None, None, None]
c8 = CircularArray(lin_arr7, 8, 6)
c6.printForward() # This should print: 10, 20, -30, 20, 50, 30
c8.printForward() # This should print: 10, 20, -30, 20, 50, 30
print(c6.equivalent(c8)) # This should print: True


print("==========Test 9==========")
lin_arr8 = [10, 20, 30, 40, 50, 60, None, None, None]
c9 = CircularArray(lin_arr8, 8, 6)
c6.printForward() # This should print: 10, 20, -30, 20, 50, 30
c9.printForward() # This should print: 10, 20, 30, 40, 50, 60
print(c6.equivalent(c9)) # This should print: False


print("==========Test 10==========")
lin_arr9 = [10, 20, 30, 40, 50, None, None, None]
c10 = CircularArray(lin_arr9, 5, 5)
c10.printFullLinear() # This should print: 40, 50, None, None, None, 10, 20, 30
lin_arr10 = [5, 40, 15, 25, 10, 20, 5, None, None, None, None, None]
c11 = CircularArray(lin_arr10, 8, 7)
c11.printFullLinear() # This should print: 10, 20, 5, None, None, None, None, None, 5, 40, 15, 25
output = c10.intersection(c11)
print(output) # This should print: [10, 20, 40]
Loading

0 comments on commit 440ef77

Please sign in to comment.