Data Structures and Algorithms using Python
Copyright © 2023 by Rance Necaise
Table Of Contents
Data Structures and Algorithms using Python
Copyright © 2023
by Rance Necaise

Chapter Exercises

Review Questions

R 1.
What is the difference between a simple data type and a complex data type?
R 2.
Give an example of procedural abstraction and an example of data abstraction.
R 3.
What is an abstract data type?
R 4.
What are some of the advantages to defining and using abstract data types?
R 5.
What is information hiding and why is it used?
R 6.
List the four categories into which the operations of an ADT can be divided.
R 7.
What is a precondition and what is a postcondition?

Exercises

E 1.
Complete the partial implementation of the Time class by implementing the remaining operations: add, and subtract.
E 2.
The definition of the Time ADT indicates that the arguments for creating a Time object must be specified within a given range. Modify the Time constructor from the program listing in section 1.6 Representing a Time Value to correctly verify the preconditions.
E 3.
Modify the Time constructor to make each of the three arguments optional, with an initial value of zero. When no argument is supplied to the constructor, the time should be initialized to 1.
E 4.
Complete the partial implementation of the LineSegment class by implementing the remaining operations: isHorizontal, isParallel, and midpoint.
E 5.
Add additional operations to the LineSegment class:
  1. isPerpendicular(otherLine):

    returns a Boolean indicating whether the line segment is perpendicular to the otherLine.

  2. intersects(otherLine):
    returns a Boolean indicating whether the line segment intersects the otherLine.
  3. bisects(otherLine):
    returns a Boolean indicating whether the line segment bisects the otherLine.

Programming Projects

P 1.
A click counter is a small hand-held device that contains a push button and a count display. To increment the counter, the button is pushed and the new count shows in the display. Clicker counters also contain a button that can be pressed to reset the counter to zero. Design and implement the Counter ADT that functions as a hand-held clicker.
P 2.
A cone is a three-dimensional geometric shape that consists of a circular base and a vertex. The non-base surface of the cone tapers smoothly from the base to the vertex. A cone is defined by its radius and height.
  • Cone(radius, height)
    Creates a new instance of a cone as defined by the given radius and height.
  • radius()
    Returns the cone's radius.
  • height()
    Returns the cone's height.
  • diameter()
    Returns the diameter of the cone's base.
  • volume()
    Returns the volume of the cone.
  • surfaceArea()
    Returns the total surface area of the cone.
  • isEqualTo(otherCone)
    Determines if this cone is identical to a second cone.
  • toString()
    Returns a string representation of the cone in the format cone{R, H}, where R is the radius of the cone and H is its height. Both values are given to two decimal places.

Provide a complete implementation for the Cone ADT.

P 3.
The use of the Student File Reader ADT makes it easy to extract student records from a text file no matter the format used to store the data. Implement a new version of the ADT to extract the data from a text file in which each record is stored on a separate line and the individual fields are separated by commas. For example, the following illustrates the format of a sample file containing three student records:

 10015, John, Smith, 2, 3.01
 10334, Jane, Roberts, 4, 3.81
 10208, Patrick, Green, 1, 3.95

P 4.
In the chapter, we defined and implemented the Student File Reader ADT for extracting student records from an external source. We can define and use a similar ADT for output.
  1. Design a Student File Writer ADT that can be used to display, or store to an output device, student records contained in a StudentRecord object.
  2. Provide an implementation of your ADT to output the records by displaying them to the terminal in a neatly formatted fashion.
  3. Provide an implementation of your ADT to output the records to a text file using the same format described in the text.
  4. Design and implement a complete program that extracts student records from a text file, sorts them by either student id or student name, and displays them to the terminal using your ADT. The choice of sort keys should be extracted from the user.
P 5.
Define and implement an ADT that can be used to represent a date in the Gregorian calendar for any date since November 24, 4713 BC.
P 6.
Python provides a numeric class for working with floating-point values. But not all real numbers can be represented precisely on a computer since they are stored as binary values. In applications where the precision of real numbers is important, we can use rational numbers or fractions to store exact values. A fraction, such as 7/8, consists of two parts, both of which are integers. The top value, which can be any integer value, is known as the numerator. The bottom value, which must be greater than zero, is known as the denominator.
  1. Define a Fraction ADT to represent and store rational numbers. The ADT should include all of the common mathematical and logical operations. In addition, your ADT should provide for the conversion between floating-point values and fractions and the ability to produce a string version of the fraction.
  2. Provide a Python implementation of your Fraction ADT.
Page last modified on August 24, 2023, at 06:54 AM