[Python 문제풀이] 16일차

2020. 7. 24. 11:0003. Resources/Python

728x90
반응형

Day_16

Intro

Q22. avoidObstacles

You are given an array of integers representing coordinates of obstacles situated on a straight line.
Assume that you are jumping from the point with coordinate 0 to the right. You are allowed only to make jumps of the same length represented by some integer.
Find the minimal length of the jump enough to avoid all the obstacles.

제출 코드

def avoidObstacles(inputArray):
    inputSort = sorted(inputArray)

    StepLength = 1
    ArrIdx = 0
    CurrStep = 0


    while 1:

        CurrObs = inputSort[ArrIdx]

        if CurrObs == CurrStep:
            ArrIdx = 0
            StepLength = StepLength + 1
            CurrStep = 0
            continue
        elif CurrObs < CurrStep:
            ArrIdx = ArrIdx + 1
            if ArrIdx == len(inputSort):
                break        
            continue
        else:
            CurrStep = CurrStep + StepLength
            continue

    return StepLength

작성 흐름

# Test case
inputArray = [1, 4, 10, 6, 2]

# 값을 정렬
inputSort = sorted(inputArray)
print(inputSort)

StepLength = 1
ArrIdx = 0
CurrStep = 0
NoPassFlag = 0

while 1:
    print('Current Step Length: %d' % (StepLength))

    CurrObs = inputSort[ArrIdx]
    print('Current obstacle: %d' % (CurrObs))
    print('Current step: %d' % (CurrStep))

    if CurrObs == CurrStep:
        print("Can't pass current obstacles")
        ArrIdx = 0
        StepLength = StepLength + 1
        CurrStep = 0
        print("Add step length")
        print(" ")
        continue
    elif CurrObs < CurrStep:
        print("Add obstacle index")
        ArrIdx = ArrIdx + 1
        if ArrIdx == len(inputSort):
            break        
        continue
    else:
        print("Add step length to current step ")
        CurrStep = CurrStep + StepLength
        continue

print("Break Step Length: %d" % (StepLength))
반응형

'03. Resources > Python' 카테고리의 다른 글

[Python 문제풀이] 18일차  (0) 2020.07.26
[Python 문제풀이] 17일차  (0) 2020.07.25
[Python 문제풀이] 15일차  (0) 2020.07.23
[Python 문제풀이] 14일차  (0) 2020.07.22
[Python 문제풀이] 13일차  (0) 2020.07.21