Posts

Search

Python: String Representations of Objects

Implement two vehicle classes:

Car:
The constructor for Car must take two arguments. The first of them is its maximum speed, and the second one is a string that denotes the units in which the speed is given: either "km/h" or "mph".The class must be implemented to return a string based on the arguments. For example, if car is an object of class Car with a maximum speed of 120, and the unit is "km/h", then printing car prints the following string: "Car with the maximum speed of 120 km/h", without quotes. If the maximum speed is 94 and the unit is "mph", then printing car prints in the following string: "Car with the maximum speed of 94 mph", without quotes.

Boat:
The constructor for Boat must take a single argument denoting its maximum speed in knots.The class must be implemented to return a string based on the argument. For example, if boat is an object of class Boat with a maximum speed of 82, then printing boat prints the following string: "Boat with the maximum speed of 82 knots", without quotes.The implementations of the classes will be tested by a provided code stub on several input files. Each input file contains several queries, and each query constructs an object of one of the classes.  It then prints the string representation of the object to the standard output. 

Constraints
1 ≤ the number of queries in one test file ≤ 100 

Input Format Format for Custom Testing
In the first line, there is a single integer, q, the number of queries.Then, q lines follow. In the ith of them, there are space-separated parameters. The first of them denotes the vehicle type to be constructed, and the remaining parameters denote the values passed for the constructor of the object.

Sample Case 0
Sample Input

STDIN           Function
-----           -------
2             → number of queries, q = 2
car 151 km/h  → query parameters = ["car 151 km/h", "boat 77"]
boat 77
Sample Output

Car with the maximum speed of 151 km/h
Boat with the maximum speed of 77 knots
Explanation

There are 2 queries. In the first of them, an object of class Car with the maximum speed of 151 in km/h is constructed, and then its string representation is printed to the output. In the second query, an object of class Boat is constructed with the maximum speed of 77 knots, and then its string representation is printed to the output.

Sample Case 1
Sample Input

STDIN         Function
-----         --------
3           → number of queries, q = 2
boat 101    → query parameters = ["boat 101", "car 120 mph", "car 251 km/h"]
car 120 mph
car 251 km/h
Sample Output

Boat with the maximum speed of 101 knots
Car with the maximum speed of 120 mph
Car with the maximum speed of 251 km/h
Explanation

There are 3 queries. In the first of them, an object of class Boat with the maximum speed of 101 knots is constructed, and then its string representation is printed to the output. In the second query, an object of class Car with the maximum speed of 120 in mph is constructed, and then its string representation is printed to the output. In the third query, an object of class Car with the maximum speed of 251 in km/h is constructed, and then its string representation is printed to the output.Your implementation of all the classes will be tested by a provided code stub on several input files. Each input file contains several queries, and each query constructs an object of one of the classes and prints the area of this object to the standard output with exactly 2 decimal points.

Constraints
1 ≤ the number of queries in one test file ≤ 105
1 ≤  the value of all parameters passed to construct the objects ≤ 103

Input Format Format for Custom Testing:
In the first line, there is a single integer, q, the number of queries.Then, q lines follow. In the ith of them, there are space-separated parameters. The first of them denotes the shape to be constructed, and the remaining parameters denote the parameters for the constructor.

Sample Case 0
Sample Input

STDIN             Function
-----             --------
2              →  number of queries, q = 2
circle 1       →  query parameters = ["circle 1", "rectangle 2 3"]
rectangle 2 3

Sample Output
3.14
6.00

Explanation
There are 2 queries. In the first of them, an object of class Circle with radius 1 is constructed. Then, the value of its area property, with exactly 2 decimal points, is printed to the output. Since the radius of the circle is 1, then the printed area is 3.14 (pi * radius2). In the second query, the object of class Rectangle is constructed with side lengths of 2 and 3. Then, the value of its area property, with exactly 2 decimal points, is printed to the output. Since the side lengths are 2 and 3, then the printed area is 6.00.

Sample Case 1
Sample Input

STDIN            Function
-----            --------
3              → number of queries, q = 3
rectangle 5 7  → query parameters = ["rectange 5 7", "rectangle 7 5", "circle 1000"]
rectangle 7 5
circle 1000

Sample Output
35.00
35.00
3141592.65

Explanation
There are 3 queries. In the first of them, an object of class Rectangle with side lengths of 5 and 7 is constructed. Then, the value of its area property (5 * 7 = 35), with exactly 2 decimal points, is printed to the output (35.00). The second query likewise returns the same result, since (7 * 5 = 35). In the third query, an object of class Circle with radius 1000 is constructed. Then, the value of its area property, with exactly 2 decimal points is printed to the output. Since the radius of the circle is 1000, then the printed area is (pi * 10002) = 3141592.65.

Solution :

import math
import os
import random
import re
import sys
class Car:
    def __init__(self, a,b):
        self.a =a
        self.b=b
        
    def __str__(self):  
        return "Car with the maximum speed of %s %s"%(self.a,self.b)
class Boat: 
    def __init__(self, a):
        self.a = a
    def __str__(self):  
        return "Boat with the maximum speed of %s knots"%(self.a)

    
if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')
    q = int(raw_input())
    queries = []
    for _ in xrange(q):
        args = raw_input().split()
        vehicle_type, params = args[0], args[1:]
        if vehicle_type == "car":
            max_speed, speed_unit = int(params[0]), params[1]
            vehicle = Car(max_speed, speed_unit)
        elif vehicle_type == "boat":
            max_speed = int(params[0])
            vehicle = Boat(max_speed)
        else:
            raise ValueError("invalid vehicle type")
        fptr.write("%s\n" % vehicle)
    fptr.close()