[Edu-sig] GvR question (correction)

André Roberge andre.roberge at gmail.com
Wed May 4 07:04:10 CEST 2005


André Roberge wrote:
> Dan Schellenberg wrote:
> [snip]
> 
>>On lesson 17 http://gvr.sourceforge.net/lessons/rfrank/step17/  I have 
>>been unable to contrive a solution that I think my students would 
>>understand at this stage.  The problem is very straightforward, but the 
>>difficulty comes in assigning a proper while loop to the code block that 
>>defines how long Guido continues to pick up garbage (ie. none of the 
>>built in conditionals provide a good boolean for this problem, though 
>>right_is_clear is workable, just not eloquent).  I have solved the 
>>problem using the aforementioned right_is_clear conditional in the while 
>>loop (see first example below), and have also solved it (much more 
>>eloquently in my opinion) using recursion.  However, I would like to 
>>have a more eloquent solution without having to use recursion, as I am 
>>not sure that I want to introduce that concept at this stage.  Below are 
>>my two solutions.  Anyone have an idea on how to improve the readability 
>>of these solutions for the students?
> 
> 
> Here's my non-recursive solution in rur-ple, which could be easily 
> translated in GvR.  Actually, I tried to keep your structure intact as 
> much as possible.  It does not use the right_is_clear conditional.

Oops... misses garbage along the top (north) wall. My test case world 
did not have garbage there.
Required modifications noted below ... as well as some moving
parts of the code from the "definition" section to
the "execution" section - it might be easier to understand
this way.


> 
> # definitions
> 
> def turn_right():
>      repeat(turn_left, 3)
> 
> def turn_around():
>      turn_left()
>      turn_left()
> 
> def facing_south():   # not built-in yet in rur-ple;
>                        # hence need to use return
>      turn_around()
>      answer = facing_North()
>      turn_around()
>      return answer
> 
> def go_south_west_corner():
>      while not facing_south():
>          turn_left()
>      while front_is_clear():
>          move()
>      turn_right()
>      while front_is_clear():
>          move()
>      turn_right()  # thus facing north
> 
> def go_north_east_corner():
>      while not facing_North():
>          turn_left()
>      while front_is_clear():
>          move()
>      turn_right()
>      while front_is_clear():
>          move()
> 
> # the following definition is modified from yours
> 
> def pickup_column_and_come_back(): # normally facing north
>      while front_is_clear():
>          while next_to_a_beeper():
>              pick_beeper()
>          move()

            while next_to_a_beeper():   # forgot garbage there
                pick_beeper()

>      turn_around()                  # now facing south
>      while front_is_clear():
>          move()

## remove end of this definition; put in section below

> 
> # end of definitions
> # =================
> # begin algorithm per se
> 
> go_south_west_corner()
> 
> while facing_North():
>      pickup_column_and_come_back()   # face south after
        turn_left()               # now facing east; may end that way
        if front_is_clear():
            move()
            turn_left()           # ends next column, face North

> 
> # exits at south-east corner, facing east
> 
> turn_left()
> while front_is_clear():
>      move()
> 
> while carries_beepers():
>      put_beeper()
> 
> go_south_west_corner()
> turn_off()
> ---------------------------------------------------
> 
> André



More information about the Edu-sig mailing list