reset password
Author Message
msargent
Posts: 519
Posted 20:46 Nov 12, 2012 |

I can't pass the last two tests in the test suite. I don't know which function/method is failing, as the tests are for the solution val. Does anyone have a suggestion for a test for from? Also, here is my code for from(), I can't figure out what's wrong with it. I'm passing all the other tests. 

Edit: When I added code to print out extension it is always the same as initial, but newNeighborsOnly (used to generate the extension) passed its test. 

 

   def from(initial: Stream[(Block, List[Move])],

           explored: Set[Block]): Stream[(Block, List[Move])] = {

   if (initial isEmpty)  initial

   else {

     // Determine the states that are neighbors of initial.

     val extension: Stream[(Block, List[Move])] = newNeighborsOnly(initial, explored)

     println(extension)//fpr testing purposes

     // Determine the block positions they occupy

     val newExplored: Set[Block]= extension.map(x => x._1).toSet

     println(newExplored)//for testing purposes

     // Build and return a new stream that includes:

     //   the initial stream

     //   the extension of the initial stream

     //   what from produces from that extension

     // On the recursive call to from include the new states in explored

        if (newExplored.isEmpty){

       initial // this is meant to end the recursion when there are no new positions to move to

     }  

     else from(initial ++ extension, newExplored ++ explored) 

   }

  }

 

Here is the rest:

   /**

   * The stream of all paths that begin at the starting block.

   */

  lazy val pathsFromStart: Stream[(Block, List[Move])] = from(Stream((startBlock, List())), Set())

  /**

   * Returns a stream of all possible pairs of the goal block along

   * with the history how it was reached.

   */

  lazy val pathsToGoal: Stream[(Block, List[Move])] = pathsFromStart.filter(x => x._1.b1 == goal && x._1.isStanding)

 

  /**

   * The (or one of the) shortest sequence(s) of moves to reach the

   * goal. If the goal cannot be reached, the empty list is returned.

   *

   * Note: the `head` element of the returned list should represent

   * the first move that the player should perform from the starting

   * position.

   */

  lazy val solution: List[Move] = {

   if (pathsToGoal.isEmpty) List()

    else pathsToGoal.head._2    

  }

Last edited by msargent at 21:40 Nov 12, 2012.
rabbott
Posts: 1649
Posted 21:50 Nov 12, 2012 |
See embedded comments

msargent wrote:

I can't pass the last two tests in the test suite. I don't know which function/method is failing, as the tests are for the solution val. Does anyone have a suggestion for a test for from? Also, here is my code for from(), I can't figure out what's wrong with it. I'm passing all the other tests. 

Edit: When I added code to print out extension it is always the same as initial, but newNeighborsOnly (used to generate the extension) passed its test. 

 

   def from(initial: Stream[(Block, List[Move])],

           explored: Set[Block]): Stream[(Block, List[Move])] = {

   if (initial isEmpty)  initial

   else {

     // Determine the states that are neighbors of initial.

     val extension: Stream[(Block, List[Move])] = newNeighborsOnly(initial, explored)

     println(extension)//fpr testing purposes

     // Determine the block positions they occupy

     val newExplored: Set[Block]= extension.map(x => x._1).toSet

     println(newExplored)//for testing purposes

     // Build and return a new stream that includes:

     //   the initial stream

     //   the extension of the initial stream

     //   what from produces from that extension

     // On the recursive call to from include the new states in explored

        if (newExplored.isEmpty){

       initial // this is meant to end the recursion when there are no new positions to move to

     }  

// Will this ever return anything? Normally you should both return something and

// call from() recursively to generate the rest of the Stream.

// Also see additional note below.

// In this problem, since there are a relatively small number of possible states, 

// it will terminate and produce a result. But normally you can't count on that.

// The point of using a Stream is to produce early results which can be checked before

// computing more. That way the program can stop as soon as it finds a solution.

     else from(initial ++ extension, newExplored ++ explored) 

   }

  }

 

Here is the rest:

   /**

   * The stream of all paths that begin at the starting block.

   */

  lazy val pathsFromStart: Stream[(Block, List[Move])] = from(Stream((startBlock, List())), Set())

  /**

   * Returns a stream of all possible pairs of the goal block along

   * with the history how it was reached.

   */

  lazy val pathsToGoal: Stream[(Block, List[Move])] = pathsFromStart.filter(x => x._1.b1 == goal && x._1.isStanding)

 

  /**

   * The (or one of the) shortest sequence(s) of moves to reach the

   * goal. If the goal cannot be reached, the empty list is returned.

   *

   * Note: the `head` element of the returned list should represent

   * the first move that the player should perform from the starting

   * position.

   */

  lazy val solution: List[Move] = {

   if (pathsToGoal.isEmpty) List()

    else pathsToGoal.head._2    

  }

Last edited by rabbott at 21:56 Nov 12, 2012.