A201

Assignment 6

Reeborg Closes Windows and Printing Tables

Individual work on this lab and assignment.

In this lab and assignment you will gain experience using conditional statements, while loops and some string formatting.

In Lab

Though you are to try to program you own solution in this lab, as in all this course's lab exercises, you are encouraged to seek help from other students as well as the lab instructors if you're stuck.

Do RUR-PLE Lession 22. This is preparation for the first part of the assignment. A solution is given in the Assignment section below, but do your best to solve it on your own. The experience will be very helpful in doing the assignment. After at most 45 minutes on this part of the lab, move on so you have time for the rest of it.

This lab and assignment are the last that we will be using RUR-PLE lessons or programming Reeborg the robot. Lesson 23 provides additional exercises that you may find helpful to do on your own and does not introduce new functionality. The remaining lessons introduce new material at a rapid pace, much of which will not be covered in this course. Hopefully you enjoy programming Reeborg and find it a good learning experience, but in future assignments we turn to application areas of more real-world use.

Here's today's "fill in the dots" game:

# lab6.py by chaynes@indiana.edu
import fpformat
import math

def print_numbers(low, hi, increment):
    """Print floating point numbers, one per line, starting with low.
    Following numbers are obtained by adding increment to the previous 
    number. This continues up to but not including the first number greater
    than hi.
    >>> print_numbers(1, 3, .5)
    1.0
    1.5
    2.0
    2.5
    3.0
    >>>
    """
    #.
    #.
    #.
    #.

def squareroot_table(low, hi, increment):
    """Same as print_numbers, except that on each line the number is followed by
    its square root.
    >>> squareroot_table(1, 3, .5)
    1 1.0
    1.5 1.22474487139
    2.0 1.41421356237
    2.5 1.58113883008
    3.0 1.73205080757
    >>>
    """
    #.
    #.
    #.
    #.

def right_justify_float(x, size, precision):
    """Returns a string of length `size` in which the the value of `x` is right justified
    with `precision` digits after the decimal point.
    >>> right_justify_float(3.14159, 8, 3)
    '   3.142'
    >>>
    """
    #.
    #.

def formatted_squareroot_table(low, hi, increment):
    """Same as square_root_table, except the numbers are printed with three
    digits of precision (after the decimal point) and right-justified in 
    10 columns.
    >>> formatted_squareroot_table(1, 3, .5)
         1.000     1.000
         1.500     1.225
         2.000     1.414
         2.500     1.581
         3.000     1.732
    >>>
    """
    #.
    #.
    #.
    #.
    #.


Hint:

  • For the right_justify_float function, recall from the class notes the right_justify function:

    def right_justify(n, string):
        """
        Returns the string with enough blanks added at the beginning to
        to make it n characters long.
        >>> right_justify(5, 'foo')
        '  foo'
        >>>
        """
        return ' ' * (n - len(string)) + string
    

    and the fpformat module fix function that takes a number x and a positive integer digs (for "digits") and returns a floating point string with digs digits after the decimal point:

    >>> import fpformat
    >>> fpformat.fix(2.58, 1)
    '2.6'
    >>> fpformat.fix(-2.0 / 3, 3)
    '-0.667'
    >>> fpformat.fix(3, 4)
    '3.0000'
    

Before you leave, turn in your application file as Lab 6 using Oncourse, and read the assignment below. Ask your lab instructor if the assignment is not clear.

Assignment

Due 3pm, February 21st

Part 1

Complete the following "fill in the dots" file:

# a6.py, by chaynes@indiana.edu
def average1(n):
    """
    Prompt for n numbers, and print their average.
    """
    #...
    #...
    while count < n: 
        sum = sum + float(raw_input('Number: ')) 
        #...
    #...

def average2():
    """
    Prompt repeatedly for numbers, or <Enter> (empty string) to quit,
    and print out the number of numbers and their average.
    """
    #.
    #.
    #.
    #.
    #.
    #.
    #.
    #.
    #.

def inorder(x, y, z):
    """
    Return a boolean value indicating if the arguments are in ascending or
    descending order. Try to do this with a single (rather complicated) boolean
    expression.
    >>> inorder(1, 3, 3)
    True
    >>> inorder(1, 1, 5)
    True
    >>> inorder(0, 3, -1)
    False
    >>> inorder(1, 0, 2)
    False
    >>>
    """
    #...

def ordinal_suffix(n):
    """
    Returns the ordinal suffix of a positive integer n.  The ordinal
    suffix 'st', 'nd', or 'rd', when the last digit of n is 1, 2, or 3,
    respectively, and is 'th' otherwise, with the exception of 11
    through 13, for which it is 'th'.        
    >>> ordinal_suffix(3)
    'rd'
    >>> ordinal_suffix(11)
    'th'
    >>> ordinal_suffix(123)
    'rd'
    >>>
    """
    #.
    #.
    #.
    #.
    #.
    #.
    #.
    #.
    #.
    #.
    #.


The following transcript illustrates use of the average1 and average2 functions:

>>> average1(2)
Number: 3.5
Number: 2
Average = 2.75
>>> average2()
Number, or <Enter> to quit: 5
Number, or <Enter> to quit: -3.4
Number, or <Enter> to quit: 2
Number, or <Enter> to quit:
The average of 3 numbers is 1.2

Part 2

Write a program called gohome.rur that returns Reeborg to the initial starting position: the lower left facting east. This is the exercise at the end of RUR-PLE Lesson 32, with the following added directions.

  • Define a function turn_around() that turns Reeborg 180 degrees.
  • Use turn_around() to redefine the Lesson 32 facing_south() function without unnecessary repetition.
  • Define a function face_south() using code from Lesson 32 as its body.
  • Define a function move_to_wall() that uses to front_is_clear() function to repeatedly move Reeborg in the direction he is facing until he has a wall in front.
  • Define a function go_home() that moves Reeborg to the lower left position, facing east. Make use of the above functions (and as needed any of Reeborgs built-in functions). Assume the only walls are those around the edge of the world.
  • End the program with calls to go_home() and turn_off() and test it with Reeborg at several locations and facing in various directions.

Part 3

world3.jpg

Imagine that after Lesson 22, Reeborg visits the house of another friend, defined by the rain3.wld world file, which has a number of new architectural features. There is a sun room on the north side (well, maybe a shade room, since sun rooms are better on the south side), a nook on the east side, and corner windows in the bay on the south side. The picture at right shows what it looks like, with beepers indicating the windows.

The new architectural features cause Reeborg trouble when using the rain2.rur program (a solution to the Lesson 22 lab exercise). Try it! When he runs into trouble, eliminate the nearby architectural feature that caused the trouble and try it again. There are multiple problems. Write a program named rain3.rur that allows Reeborg to close the windows in this house by placing beepers as in the above picture. (Since Reeborg is a used robot, with very limited abilities, there are other architectural features that he would be unable to navigate. But the new program that handles this house, as well as the earlier ones, is considerably better than the rain2.rur program.)

The owner of the new house is aware of Reeborg's limitations and suggests the following approach.

  • In addition to the functions in the rain2.rur program, include in an all_clear() function that returns True if Reeborg senses that there are no walls to the immediate left, right, or front. (If he hasn't turned since his last move, it will be clear behind also.)
  • Then modify the while loop in rain2.rur is several ways. When it is clear to the right, turn right and move forward to investigate. Then there are a couple of possibilities.
    • If it is all clear, Reeborg concludes that he has just moved through a window, in which case he moves back, plants a beeper, and turns back to the direction he was going before noticing the window.
    • If it is not all clear, but it is clear to the right, Reeborg concludes that there is a bay with a corner window, and plants a beeper without moving back or changing direction at that time.
  • Then to get out of tight spaces, like the east nook, Reeborg turns left as many times as necessary to find the front clear.
  • Finally, he takes a step forward and goes to the beginning of the main loop that stops when he runs into a beeper.

As always, be sure your name, username, and lab section start time are included in a comment at the start of each file you submit.

Submit your gohome.rur, rain3.rur, and a6.py files via Oncourse (of course being careful to use Assignment 6 in the Assignments tool).