Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions week18/kayode/switch_sort.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
Author: Kayode
---

Switch Sort

Have the function SwitchSort(arr) take arr which will be an an array consisting of integers 1...size(arr) and determine what the fewest number of steps is in order to sort the array from least to greatest using the following technique: Each element E in the array can swap places with another element that is arr[E] spaces to the left or right of the chosen element. You can loop from one end of the array to the other. For example: if arr is the array [1, 3, 4, 2] then you can choose the second element which is the number 3, and if you count 3 places to the left you'll loop around the array and end up at the number 4. Then you swap these elements and arr is then [1, 4, 3, 2]. From here only one more step is required, you choose the last element which is the number 2, count 2 places to the left and you'll reach the number 4, then you swap these elements and you end up with a sorted array [1, 2, 3, 4]. Your program should return an integer that specifies the least amount of steps needed in order to sort the array using the following switch sort technique.

The array arr will at most contain five elements and will contain at least two elements.

Examples
Input: [3,1,2]
Output: 2

Input: [1,3,4,2]
Output: 2
97 changes: 97 additions & 0 deletions week18/kayode/switch_sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
def SwitchSort(arr):
def loopcounter(start, times, leng, dir):
if dir == "r":
end= start + times
while end >= leng:
end= end - leng
return end
elif dir == "l":
end= start - times
while end < 0:
end= end + leng
return end

def swapper(swaparr, start, end):
s= swaparr.copy()
vals= s[start]
vale= s[end]
s[start]= vale
s[end]= vals
return s


sarr= arr.copy()
sarr.sort()
ope= []
alen= len(arr)

if arr == sarr:
return print(0)

for i in range(alen):
if arr[i] != sarr[i]:
ope.append([i, arr[i]])


reslen= []
for i, j in ope:
temparr= arr.copy()
stepcounter= 1
steprecord= []
results= []

lend= loopcounter(i, j, alen, "l")
rend= loopcounter(i, j, alen, "r")

if lend != i:
lresult= swapper(temparr, i, lend)
steprecord.append({"steps":stepcounter, "array":lresult})
results.append(lresult)
if rend != i:
rresult= swapper(temparr, i, rend)
steprecord.append({"steps":stepcounter, "array":rresult})
results.append(rresult)
if rend == i and lend == i:
continue
# print(steprecord)

while sarr not in results:
tempresults= []
for k in steprecord:
if k['steps'] == stepcounter:
tempresults.append(k)

stepcounter= stepcounter + 1

for p in tempresults:
tempope= []
for k in range(alen):
if sarr[k] != p['array'][k]:
tempope.append([k, p['array'][k]])

for m, n in tempope:
lend= loopcounter(m, n, alen, "l")
rend= loopcounter(m, n, alen, "r")

if lend != m:
lresult= swapper(p['array'], m, lend)
steprecord.append({"steps":stepcounter, "array":lresult})
results.append(lresult)
if rend != m:
rresult= swapper(p['array'], m, rend)
steprecord.append({"steps":stepcounter, "array":rresult})
results.append(rresult)

for k in steprecord:
if k['array'] == sarr:
reslen.append(k['steps'])

print(reslen)
return print(min(reslen))




# SwitchSort([3,1,2])
# SwitchSort([1,2,3])
SwitchSort([1,3,4,2])
14 changes: 14 additions & 0 deletions week19/kayode/matrix_determinant.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
Author: Kayode
---

Matrix Determinant

Have the function MatrixDeterminant(strArr) read strArr which will be an array of integers represented as strings. Within the array there will also be "<>" elements which represent break points. The array will make up a matrix where the (number of break points + 1) represents the number of rows. Here is an example of how strArr may look: ["1","2","<>","3","4"]. The contents of this array are row1=[1 2] and row2=[3 4]. Your program should take the given array of elements, create the proper matrix, and then calculate the determinant. For the example above, your program should return -2. If the matrix is not a square matrix, return -1. The maximum size of strArr will be a 6x6 matrix. The determinant will always be an integer.

Examples
Input: ["5","0","<>","0","5"]
Output: 25

Input: ["1","2","4","<>","2","1","1","<>","4","1","1"]
Output: -4
69 changes: 69 additions & 0 deletions week19/kayode/matrix_determinant.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
def MatrixDeterminant(strArr):
def matrix_calc(r, length):
if length > 2:
detrow= []

#recreate the rows first
tr= r.copy()
fr= tr.pop(0)
print(tr)
print(length)
for i in range(length):
tr2= tr.copy()
tr2len= len(tr2)
tr3= []
for j in range(tr2len):
mrow= tr2[j].copy()
mrow.pop(i)
tr3.append(mrow)
tr3len= len(tr3)
detrow.append(matrix_calc(tr3, tr3len))

determinant= 0
for i, j in enumerate(fr):
if i % 2 == 0:
determinant= determinant + (j * detrow[i])
else:
determinant= determinant - (j * detrow[i])

return determinant
elif length == 2:
determinant= (r[0][0] * r[1][1]) - (r[0][1] * r[1][0])
return determinant



srows= []
temp= []
rows= []
for i in strArr:
if i == "<>":
srows.append(temp)
temp= []
else:
temp.append(i)
else:
srows.append(temp)

rowlen= len(srows[0])
if len(srows) != rowlen:
return print(-1)
for i in srows:
if len(i) != rowlen:
return print(-1)

for i in srows:
temprow= []
for j in i:
temprow.append(int(j))
rows.append(temprow)


# print(srows)
print(rows)
print(matrix_calc(rows, rowlen))


# MatrixDeterminant(["5","0","<>","0","5"])
# MatrixDeterminant(["1","2","4","<>","2","1","1","<>","4","1","1"])
MatrixDeterminant(["2","1","<>","4","1"])