what is wrong here. Tests are green but the outcome with real code is much to high

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

what is wrong here. Tests are green but the outcome with real code is much to high

Roelof Wobben
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


Reply | Threaded
Open this post in threaded view
|

Re: what is wrong here. Tests are green but the outcome with real code is much to high

Richard O'Keefe
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

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


Reply | Threaded
Open this post in threaded view
|

Re: what is wrong here. Tests are green but the outcome with real code is much to high

Roelof Wobben
Op 18-12-2018 om 01:30 schreef Richard O'Keefe:
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

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



Thanks again

Roelof