FinalSpring2020 230
FinalSpring2020 230
FinalSpring2020 230
3. (20 points) (Simple) Drawing and Saving a triangle of height specified by user.
Write a program that inputs an integer n and outputs a triangle of height n as shown
below. If n>0, the triangle is up, if n<0, the triangle is down. Test your program with n=5
and n= -5. Output on the console and also save the outputs to a file called out_triangle.txt
Sample run (should look the same in console and text file):
4. (30 points) (Medium) Find the largest sequence of positive numbers
Write a program that takes as input a sequence of integers on one line. The purpose is to
find the largest sequence of positive numbers. If there are more than one, we are satisfied
with one of them. Solve it using:
a. Two loops
b. One loop
Sample runs:
B = [1 2 1
2 3 2
1 2 1]
C = [1 2 3 2 1
2 3 4 3 2
3 4 5 4 3
2 3 4 3 2
1 2 3 2 1]
a. (10 pts) Write a recursive function my_pal_Vector(V) that takes as input a vector V and
returns a (Boolean) number: 1 (True) if the vector is a palindrome and 0 (False) if the
vector is not a Palindrome. You are not allowed to use loops or slicing.
Hint: In general for a vector V of N elements, V is a palindrome if the first element and
last element of V are equal, and the internal sequence V(2:N-1) is also a palindrome.
b. (15 pts) Write a recursive function my_pal_Matrix_R(A) that takes as input a matrix A
and returns a (Boolean) number: 1 if all the rows of A are palindromes, and 0 if any of
the rows is not a Palindrome.
Hint: In general for a Matrix A of M rows, A is a palindrome in rows if the first row is a
Palindrome and the remaining Matrix A(2:M,:) is also a palindrome.
c. (5 pts) Use the above functions in a main program to test them for the following cases:
Stick to the output to get full grade on this part.
6. (30 points) (Simple) Abstract Data Types (ADTs) for Points and Circles
In this problem, we want to design a data type Point for representing points in cartesian
(x,y) coordinates. We then want to use the Point ADT to design a data type Circle to
represents a circle represented by the point coordinates of its center and its radius.
a. (8 points) Design the ADT class Point which is represented by two floats or ints x and y.
Include the needed methods to support the following:
Initializing or instantiating an object of type Point as follows: P = Point(a,b). The
method takes the arguments a and b, then makes sure they are of type floats or
integers. Include an assertion with message “Bad Point Data” to make sure the
inputs are of types either integers or floats. The method then sets the caretsian x
and y values of a and b respectively.
Converting or casting a Point object to a string when the str() function is applied
to the object of type Point. The method should cast the point object P with
coordinates a and b into into the string ‘Point (a,b)’. See example runs below.
Implementing the method distance() which is used to compute the distance from
a Point object P = Point(a,b) to a point object Q = Point(c,d) using the following
syntax: P.distance(Q). Include an assertion with message “Bad Distance Input” to
make sure that Q is of type Point. The method should return the distance between
P and Q rounded to 2 decimal digits. See example run below.
b. (15 points) Design the ADT class Circle, which has a center P represented as as Point ADT
class object. The Circle also includes a radius rad, which can be of type float or integer.
Include the needed methods to support the following:
Initializing or instantiating an object of type Circle as follows: C = Circle(p, r),
where p is an object of type Point and r is a float or integer representing the radius
of the circle. The method takes the arguments p and r as input arguments. Include
assertion with message “Bad Circle Input!” to make sure the following holds: type
of p is Point, type of r is int or float.
Converting or casting a Circle object to a string when the str() function is applied
to the object of type circle. The method should cast the circle object C with center
p = Point (a,b) and radius r into the string ‘Circle ( Center = Point (a,b), Radius = r)’.
See example runs below.
Overload the method __add__() to support the addition ‘+’ of two Circle objects.
The method will return a Circle object, whose radius is the sum of the two Circle
objects’ radii, and the new center coordinates are produced by taking the average
of the coordinates of the two circles’ centers.
Implementing a method contains(), which checks if a given point Point object q =
Point(c,d) is inside the circle object C using the following syntax: C.contain(q).
Include assertion with message “Bad Contain Input!” to make sure the following
holds: type of q is of type Point.This method should return a Boolean True or False
indicating whether the point q lies within the circle C.
c. (7 points) Using the above classes, write the code in the main program to run the following
tests:
Declare two points with Coordinates (3.5,6) and (2,4) print them.
Print the distance between the two created points
Declare a circle C1 with center (2,4) and radius 2.3, and print it.
Check if the points (1.5,3) and (10.5, 3) are contained in the created Circle C1.
Declare another circle C2 with center (3.5,6) and radius 6.2, and print it
Print the sum of the two circles C1+C2
Here is a sample run and make sure you follow the format shown below.