From 7579d184412cc66d8a2a4ad096e5c8dda0a477db Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Tue, 31 Aug 2021 17:06:23 +0100 Subject: - Added guest contributions by Eric Cheung. --- .../eric-cheung/excel-vba/Challenge_128.xlsm | Bin 0 -> 34597 bytes challenge-128/eric-cheung/excel-vba/ch-1.bas | 82 +++++++++++++++++++++ challenge-128/eric-cheung/excel-vba/ch-2.bas | 54 ++++++++++++++ 3 files changed, 136 insertions(+) create mode 100755 challenge-128/eric-cheung/excel-vba/Challenge_128.xlsm create mode 100755 challenge-128/eric-cheung/excel-vba/ch-1.bas create mode 100755 challenge-128/eric-cheung/excel-vba/ch-2.bas diff --git a/challenge-128/eric-cheung/excel-vba/Challenge_128.xlsm b/challenge-128/eric-cheung/excel-vba/Challenge_128.xlsm new file mode 100755 index 0000000000..b33d8f616e Binary files /dev/null and b/challenge-128/eric-cheung/excel-vba/Challenge_128.xlsm differ diff --git a/challenge-128/eric-cheung/excel-vba/ch-1.bas b/challenge-128/eric-cheung/excel-vba/ch-1.bas new file mode 100755 index 0000000000..f44af35acc --- /dev/null +++ b/challenge-128/eric-cheung/excel-vba/ch-1.bas @@ -0,0 +1,82 @@ +Attribute VB_Name = "ModTask_01" +Option Explicit +Option Base 1 +Public Const strMyTitle As String = "Eric Cheung" + +''Public Const nRowNum As Integer = 3 +''Public Const nColNum As Integer = 6 + +Public Const nRowNum As Integer = 3 +Public Const nColNum As Integer = 4 + +Function bIsMatrixOne _ +( _ + ByRef nMatrix, _ + nStartRowIndx As Integer, _ + nStartColIndx As Integer, _ + nEndRowIndx As Integer, _ + nEndColIndx As Integer _ +) As Boolean + + Dim nRowSubLoop As Integer, nColSubLoop As Integer + bIsMatrixOne = False + + For nRowSubLoop = nStartRowIndx To nEndRowIndx + For nColSubLoop = nStartColIndx To nEndColIndx + If nMatrix(nRowSubLoop, nColSubLoop) = 1 Then + bIsMatrixOne = True + Exit Function + End If + Next nColSubLoop + Next nRowSubLoop + +End Function + +Sub Task_01() + + Dim nBinArr(1 To nRowNum, 1 To nColNum) As Integer + Dim nRowLoop As Integer, nColLoop As Integer, nRowNestLoop As Integer, nColNestLoop As Integer + Dim nRowZeroMax As Integer, nColZeroMax As Integer + Dim nTempRowNum As Integer, nTempColNum As Integer + Dim strMsg As String + + 'Initialize +'' nBinArr(1, 1) = 1: nBinArr(1, 2) = 0: nBinArr(1, 3) = 0: nBinArr(1, 4) = 0: nBinArr(1, 3) = 1: nBinArr(1, 6) = 0 +'' nBinArr(2, 1) = 1: nBinArr(2, 2) = 1: nBinArr(2, 3) = 0: nBinArr(2, 4) = 0: nBinArr(2, 3) = 0: nBinArr(2, 6) = 1 +'' nBinArr(3, 1) = 1: nBinArr(3, 2) = 0: nBinArr(3, 3) = 0: nBinArr(3, 4) = 0: nBinArr(3, 3) = 0: nBinArr(3, 6) = 0 + + nBinArr(1, 1) = 0: nBinArr(1, 2) = 0: nBinArr(1, 3) = 1: nBinArr(1, 4) = 1 + nBinArr(2, 1) = 0: nBinArr(2, 2) = 0: nBinArr(2, 3) = 0: nBinArr(2, 4) = 1 + nBinArr(3, 1) = 0: nBinArr(3, 2) = 0: nBinArr(3, 3) = 1: nBinArr(3, 4) = 0 + + If Application.Sum(nBinArr) = nRowNum * nColNum Then + 'All 1 + nRowZeroMax = 0: nColZeroMax = 0 + Else + nRowZeroMax = 1: nColZeroMax = 1 + + For nRowLoop = 1 To nRowNum + For nColLoop = 1 To nColNum + If nBinArr(nRowLoop, nColLoop) = 0 Then + For nRowNestLoop = nRowLoop + 1 To nRowNum + For nColNestLoop = nColLoop + 1 To nColNum + If Not bIsMatrixOne(nBinArr, nRowLoop, nColLoop, nRowNestLoop, nColNestLoop) Then + nTempRowNum = nRowNestLoop - nRowLoop + 1 + nTempColNum = nColNestLoop - nColLoop + 1 + If nTempRowNum * nTempColNum > nRowZeroMax * nColZeroMax Then + nRowZeroMax = nTempRowNum + nColZeroMax = nTempColNum + End If + End If + Next nColNestLoop + Next nRowNestLoop + End If + Next nColLoop + Next nRowLoop + End If + + strMsg = "Max. SubMatrix having only : " & nRowZeroMax & " * " & nColZeroMax + + MsgBox strMsg, vbOKOnly, strMyTitle + +End Sub diff --git a/challenge-128/eric-cheung/excel-vba/ch-2.bas b/challenge-128/eric-cheung/excel-vba/ch-2.bas new file mode 100755 index 0000000000..9245ab3cee --- /dev/null +++ b/challenge-128/eric-cheung/excel-vba/ch-2.bas @@ -0,0 +1,54 @@ +Attribute VB_Name = "ModTask_02" +Option Explicit +Option Base 1 + +Sub Task_02() + + ''Const nTrainNum As Integer = 2 + Const nTrainNum As Integer = 6 + + Dim strArrivalArr(1 To nTrainNum) As String, strDepartArr(1 To nTrainNum) As String + Dim strPlatFormDepart() As String, nPlatFormNum As Integer + Dim nLoop As Integer, nSubLoop As Integer, nTemp As Integer + Dim strMsg As String + + ReDim strPlatFormDepart(1 To 1) + + 'Initialize +'' strArrivalArr(1) = "11:20": strArrivalArr(2) = "14:30" +'' strDepartArr(1) = "11:50": strDepartArr(2) = "15:00" + + strArrivalArr(1) = "10:20": strArrivalArr(2) = "11:00": strArrivalArr(3) = "11:10": strArrivalArr(4) = "12:20": strArrivalArr(5) = "16:20": strArrivalArr(6) = "19:00" + strDepartArr(1) = "10:30": strDepartArr(2) = "13:20": strDepartArr(3) = "12:40": strDepartArr(4) = "12:50": strDepartArr(5) = "20:20": strDepartArr(6) = "21:20" + + nPlatFormNum = 1 + + If nTrainNum > 1 Then + For nLoop = 1 To nTrainNum + If nLoop > 1 Then + nTemp = nPlatFormNum + For nSubLoop = 1 To nPlatFormNum + If strArrivalArr(nLoop) < strPlatFormDepart(nSubLoop) Then + nTemp = nTemp - 1 + Else + strPlatFormDepart(nSubLoop) = strDepartArr(nLoop) + Exit For + End If + Next nSubLoop + If nTemp = 0 Then + nPlatFormNum = nPlatFormNum + 1 + ReDim Preserve strPlatFormDepart(1 To nPlatFormNum) + strPlatFormDepart(nPlatFormNum) = strDepartArr(nLoop) + End If + Else + strPlatFormDepart(nLoop) = strDepartArr(nLoop) + End If + Next nLoop + End If + + strMsg = "Minimum " & nPlatFormNum & " platform(s) is/are needed" + + MsgBox strMsg, vbOKOnly, strMyTitle + +End Sub + -- cgit