hello
I have this code hasReachedBasement: aFloor "comment stating purpose of message" ^ aFloor = -1 initialize "comment stating purpose of message" super initialize. position := 1. floor := 0 moveFloorDown: aFloor "comment stating purpose of message" ^ floor := aFloor - 1 moveFloorUp: aFloor "comment stating purpose of message" ^ floor := aFloor + 1 reachedBasement: input2 input2 withIndexDo: [ :element :index | element = $( ifTrue: [ self moveFloorUp: floor ] ifFalse: [ self moveFloorDown: floor ]. (self hasReachedBasement: floor) ifTrue: [ position := index ] ]. position ifNotNil: [ ^ position ] This works very fine with test input given by the challenge but as soon as I do it with this : https://gist.github.com/RoelofWobben/3be69976a6dd3531f01558b0ac563556 I see as answer : 6111 where I expect to see 1797 Anyone who can see what I did wrong ? Roelof |
Not an answer to your specific question, but your #move... methods look very odd. I think you meant something like moveUpAFloor floor := floor + 1. moveDownAFloat floor := floor - 1. hasReachedBasement ^floor = -1 AH! Now I see it. You are reporting the LAST time that the lift reached the basement, but you were suppose to report the FIRST time. You want reachedBasement: input input withIndexDo: [:element :index | element = $( ifTrue: [self moveUpAFloor]. element = $) ifTrue: [self moveDownAFloor]. self hasReachedBasement ifTrue: [^index]]. "HERE" ^0 Or using a standard linear-search method, reachedBasement: input |floor| floor := 0. "ground" ^input findFirst: [:each | each = $( ifTrue: [floor := floor + 1]. "up" each = $) ifTrue: [floor := floor - 1]. "down" floor = -1 "basement"] On Tue, 18 Dec 2018 at 09:49, Roelof Wobben <[hidden email]> wrote: hello |
Op 18-12-2018 om 01:30 schreef Richard
O'Keefe:
Thanks again Roelof |
Free forum by Nabble | Edit this page |