Header Ads Widget

String Transformation : python | hackerrank certification solution

 Problem:-

 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

• 1s the number of queries in one test file ≤ 100

nput Format Format for Custom Testing In the first line, there is a single integer, q, the

number of queries.

Then, g lines follow. In the h 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

2
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


Code(Python):-

import math
import os
import random
import re
import sys

class Car:
def __init__(self,max_speed,speed_limit):
self.m=max_speed
self.s=speed_limit
def __str__(self):
return f"Car with the maximum speed of {self.m} {self.s}"

class Boat:
def __init__(self,max_speed):
self.m=max_speed
def __str__(self):
return f"Boat with the maximum speed of {self.m} knots"

if __name__=='__main__':
fptr= open(os.environ['OUTPUT_PATH'],'w')
q=int(input())
queries=[]
for _ in range(q):
args=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()



Recommended Post :-

HCL Coding Questions:-

Capgemini Coding Questions:-

Companies interview:-

Full C course:-    

Key points:-

Cracking the coding interview:-

 Array and string:-

Tree and graph:-

Hackerearth Problems:-

Hackerrank Problems:-

Data structure:-

 MCQs:-

Post a Comment

0 Comments