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.
제출 코드
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 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
작성 흐름
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 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))