From ec3eb82c6fbf7b795c2646713b23538127571937 Mon Sep 17 00:00:00 2001 From: Mohammad S Anwar Date: Tue, 18 Apr 2023 20:20:03 +0100 Subject: - 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. --- challenge-213/eric-cheung/python/ch-1.py | 21 +++++ challenge-213/eric-cheung/python/ch-2.py | 157 +++++++++++++++++++++++++++++++ 2 files changed, 178 insertions(+) create mode 100755 challenge-213/eric-cheung/python/ch-1.py create mode 100755 challenge-213/eric-cheung/python/ch-2.py (limited to 'challenge-213/eric-cheung/python') 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) -- cgit