diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2023-04-18 20:20:03 +0100 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2023-04-18 20:20:03 +0100 |
| commit | ec3eb82c6fbf7b795c2646713b23538127571937 (patch) | |
| tree | 443be57b24eb2394bcb159e3e896a6a65d8f2dd0 /challenge-213/eric-cheung/python | |
| parent | 2005e6fc7cd50af32a06e25d8829f925b14cd1e3 (diff) | |
| download | perlweeklychallenge-club-ec3eb82c6fbf7b795c2646713b23538127571937.tar.gz perlweeklychallenge-club-ec3eb82c6fbf7b795c2646713b23538127571937.tar.bz2 perlweeklychallenge-club-ec3eb82c6fbf7b795c2646713b23538127571937.zip | |
- Added solutions by Mark Anderson.
- Added solutions by Leo Manfredi.
- Added solutions by W. Luis Mochan.
- Added solutions by Niels van Dijke.
- Added solutions by Luca Ferrari.
- Added solutions by Laurent Rosenfeld.
- Added solutions by Robert DiCicco.
Diffstat (limited to 'challenge-213/eric-cheung/python')
| -rwxr-xr-x | challenge-213/eric-cheung/python/ch-1.py | 21 | ||||
| -rwxr-xr-x | challenge-213/eric-cheung/python/ch-2.py | 157 |
2 files changed, 178 insertions, 0 deletions
diff --git a/challenge-213/eric-cheung/python/ch-1.py b/challenge-213/eric-cheung/python/ch-1.py new file mode 100755 index 0000000000..84012d3bd2 --- /dev/null +++ b/challenge-213/eric-cheung/python/ch-1.py @@ -0,0 +1,21 @@ +
+## arrListInput = [1, 2, 3, 4, 5, 6] ## Example 1
+## arrListInput = [1, 2] ## Example 2
+arrListInput = [1] ## Example 3
+
+arrListOutput = []
+
+if len(arrListInput) > 1:
+ arrListInput.sort()
+
+ for nElem in arrListInput:
+ if nElem % 2 == 0:
+ arrListOutput.append(nElem)
+
+ for nElem in arrListInput:
+ if nElem % 2 == 1:
+ arrListOutput.append(nElem)
+else:
+ arrListOutput = arrListInput
+
+print (arrListOutput)
diff --git a/challenge-213/eric-cheung/python/ch-2.py b/challenge-213/eric-cheung/python/ch-2.py new file mode 100755 index 0000000000..05dfcf5909 --- /dev/null +++ b/challenge-213/eric-cheung/python/ch-2.py @@ -0,0 +1,157 @@ +
+## Example 1:
+## arrInputRouteList = [[1, 2, 6], [5, 6, 7]]
+## nSource = 1
+## nDestination = 7
+
+## Example 2:
+## arrInputRouteList = [[1, 2, 3], [4, 5, 6]]
+## nSource = 2
+## nDestination = 5
+
+## Example 3:
+arrInputRouteList = [[1, 2, 3], [4, 5, 6], [3, 8, 9], [7, 8]]
+nSource = 1
+nDestination = 7
+
+nSourceIndx = -1
+nDestinationIndx = -1
+
+arrOutputRouteList = []
+
+for nIndx, arrLoop in enumerate(arrInputRouteList):
+ if nSource in arrLoop:
+ nSourceIndx = nIndx
+ elif nDestination in arrLoop:
+ nDestinationIndx = nIndx
+
+## print (nSourceIndx)
+## print (nDestinationIndx)
+
+if nSourceIndx < 0 or nDestinationIndx < 0:
+ print (-1)
+elif nSourceIndx == nDestinationIndx:
+ nStartIndx = arrInputRouteList[nSourceIndx].index(nSource)
+ nEndIndx = arrInputRouteList[nDestinationIndx].index(nDestination)
+
+ if nEndIndx < nStartIndx:
+ for nIndx in range(nStartIndx, nEndIndx - 1, -1):
+ arrOutputRouteList.append(arrInputRouteList[nSourceIndx][nIndx])
+ else:
+ for nIndx in range(nStartIndx, nEndIndx + 1):
+ arrOutputRouteList.append(arrInputRouteList[nSourceIndx][nIndx])
+elif len(arrInputRouteList) <= 2:
+ ## Check Intersection
+ arrIntersection = [nElem for nElem in arrInputRouteList[nSourceIndx] if nElem in arrInputRouteList[nDestinationIndx]]
+ if len(arrIntersection) == 0:
+ print (-1)
+ else:
+ ## Source Array Part
+ nStartIndx = arrInputRouteList[nSourceIndx].index(nSource)
+ nEndIndx = arrInputRouteList[nSourceIndx].index(arrIntersection[0])
+
+ if nEndIndx < nStartIndx:
+ for nIndx in range(nStartIndx, nEndIndx, -1):
+ arrOutputRouteList.append(arrInputRouteList[nSourceIndx][nIndx])
+ else:
+ for nIndx in range(nStartIndx, nEndIndx):
+ arrOutputRouteList.append(arrInputRouteList[nSourceIndx][nIndx])
+
+ ## Destination Array Part
+ nStartIndx = arrInputRouteList[nDestinationIndx].index(arrIntersection[0])
+ nEndIndx = arrInputRouteList[nDestinationIndx].index(nDestination)
+
+ if nEndIndx < nStartIndx:
+ for nIndx in range(nStartIndx, nEndIndx - 1, -1):
+ arrOutputRouteList.append(arrInputRouteList[nDestinationIndx][nIndx])
+ else:
+ for nIndx in range(nStartIndx, nEndIndx + 1):
+ arrOutputRouteList.append(arrInputRouteList[nDestinationIndx][nIndx])
+
+ print (arrOutputRouteList)
+else:
+ ## Check Intersection
+ arrIntersection = [nElem for nElem in arrInputRouteList[nSourceIndx] if nElem in arrInputRouteList[nDestinationIndx]]
+
+ if len(arrIntersection) == 0:
+ nInterSourceIndx = -1
+ nInterDestinationIndx = -1
+
+ nInterSourceElem = -1
+ nInterDestinationElem = -1
+
+ for nIndx, arrLoop in enumerate(arrInputRouteList):
+ if nIndx == nSourceIndx or nIndx == nDestinationIndx:
+ continue
+
+ arrIntersection = [nElem for nElem in arrInputRouteList[nSourceIndx] if nElem in arrInputRouteList[nIndx]]
+ if len(arrIntersection) > 0:
+ nInterSourceIndx = nIndx
+ nInterSourceElem = arrIntersection[0]
+
+ arrIntersection = [nElem for nElem in arrInputRouteList[nDestinationIndx] if nElem in arrInputRouteList[nIndx]]
+ if len(arrIntersection) > 0:
+ nInterDestinationIndx = nIndx
+ nInterDestinationElem = arrIntersection[0]
+
+ if nInterSourceIndx < -1 or nInterDestinationIndx < -1:
+ print (-1)
+ elif nInterSourceIndx == nInterDestinationIndx:
+ ## Source Array Part
+ nStartIndx = arrInputRouteList[nSourceIndx].index(nSource)
+ nEndIndx = arrInputRouteList[nSourceIndx].index(nInterSourceElem)
+
+ if nEndIndx < nStartIndx:
+ for nIndx in range(nStartIndx, nEndIndx, -1):
+ arrOutputRouteList.append(arrInputRouteList[nSourceIndx][nIndx])
+ else:
+ for nIndx in range(nStartIndx, nEndIndx):
+ arrOutputRouteList.append(arrInputRouteList[nSourceIndx][nIndx])
+
+ ## Inter Array Part
+ nStartIndx = arrInputRouteList[nInterSourceIndx].index(nInterSourceElem)
+ nEndIndx = arrInputRouteList[nInterSourceIndx].index(nInterDestinationElem)
+
+ if nEndIndx < nStartIndx:
+ for nIndx in range(nStartIndx, nEndIndx, -1):
+ arrOutputRouteList.append(arrInputRouteList[nInterSourceIndx][nIndx])
+ else:
+ for nIndx in range(nStartIndx, nEndIndx):
+ arrOutputRouteList.append(arrInputRouteList[nInterSourceIndx][nIndx])
+
+ ## Destination Array Part
+ nStartIndx = arrInputRouteList[nDestinationIndx].index(nInterDestinationElem)
+ nEndIndx = arrInputRouteList[nDestinationIndx].index(nDestination)
+
+ if nEndIndx < nStartIndx:
+ for nIndx in range(nStartIndx, nEndIndx - 1, -1):
+ arrOutputRouteList.append(arrInputRouteList[nDestinationIndx][nIndx])
+ else:
+ for nIndx in range(nStartIndx, nEndIndx + 1):
+ arrOutputRouteList.append(arrInputRouteList[nDestinationIndx][nIndx])
+
+ print (arrOutputRouteList)
+ else:
+ ## Source Array Part
+ nStartIndx = arrInputRouteList[nSourceIndx].index(nSource)
+ nEndIndx = arrInputRouteList[nSourceIndx].index(arrIntersection[0])
+
+ if nEndIndx < nStartIndx:
+ for nIndx in range(nStartIndx, nEndIndx, -1):
+ arrOutputRouteList.append(arrInputRouteList[nSourceIndx][nIndx])
+ else:
+ for nIndx in range(nStartIndx, nEndIndx):
+ arrOutputRouteList.append(arrInputRouteList[nSourceIndx][nIndx])
+
+ ## Destination Array Part
+ nStartIndx = arrInputRouteList[nDestinationIndx].index(arrIntersection[0])
+ nEndIndx = arrInputRouteList[nDestinationIndx].index(nDestination)
+
+ if nEndIndx < nStartIndx:
+ for nIndx in range(nStartIndx, nEndIndx - 1, -1):
+ arrOutputRouteList.append(arrInputRouteList[nDestinationIndx][nIndx])
+ else:
+ for nIndx in range(nStartIndx, nEndIndx + 1):
+ arrOutputRouteList.append(arrInputRouteList[nDestinationIndx][nIndx])
+
+ print (arrOutputRouteList)
|
