The midterm exam is coming up on Tuesday, October 17. The exam will cover everything up through and including files. The exam will consist of two problems. The first problem will be a short problem in loop writing, while the second problem will be a longer problem involving classes and reading data from a file.

The midterm will be an in-class exam. You will be able to bring a laptop and your usual Python development environment. If you do not have access to a laptop, you can use the computers in the room instead. Those computers have Anaconda and PyCharm installed on them.

Sample Questions

1. Write a program that prompts the user to enter two positive integers a and b with a < b and then prints the list of all the perfect squares that are between a and b, inclusive.

Here is an example of the output your program should produce.

Enter a: 12
Enter b: 89
  16
  25
  36
  49
  64
  81

Solution

2. Write a program that prompts the user to enter two positive integers a and b and then computes and prints the least common multiple of those integers. The least common multiple is the smallest integer that is a multiple of both a and b. For example, if a is 120 and b is 100 your program should print 600.

Solution

3. This is a problem concerning rectangles. Each rectangle will be described by a class with member variables width and height.

Give your class a method

fitsInside(self,other)

that returns True if the rectangle self can fit inside the rectangle other, and False if it can not. To determine whether one rectangle fits inside another, it may be necessary to rotate one of the rectangles. For example, the rectangle with a width of 20 units and a height of 10 units will fit inside a rectangle with a width of 15 and a height of 25 after we rotate the first rectangle by 90 degrees. Use the class you have written to construct a program that can read the dimensions of N rectangles from a file and determine whether or not the rectangles form a nesting sequence in which each rectangle fits inside the rectangle that comes after it in the file. You may assume that the first number in the file tells you what N is.

For example, given the input file

5
10 10
14 20
24 20
24 40
50 30

your program should say that the file contains a nesting sequence.

Given the input file

4
10 10
20 15
18 18
30 30

your program should say that the file does not contain a nesting sequence.

Solution

4. Construct a class that can represent times. The class will have member variables hours and minutes. Equip the class with a member function

__add__(self,other)

that can add two time values. Use this add function to write a program that reads a list of time values from a file and computes and prints their sum. For example given a file with the contents

3 20
4 15
6 35
2 25

your program should print a total time value of 16 hours and 35 minutes.

Solution

5. Construct a Python program that can read a list of fractions from a file and print the smallest and largest fractions found in the file.

For example, given an input file that looks like:

12/5
94/37
25/3
90/57

your program should print

The smallest fraction is 90/57.
The largest fraction is 25/3.

Start by constructing a class that can represent fractions, and equip the class with operator overloads of two of the comparison operators, < and >, To overload the < operator add a method with the name of __lt__. To overload the > add a method with the name of __gt__.

To split each line in the input file into a numerator and a denominator, use the code

line.split('/')

Solution