Skip to main content

Featured

Introduction to Numpy Library in Python

Introduction to Numpy Library in Python NUMPY - Introduction Introduction     NUMPY stands for Numerical Python. Numpy is one of the libraries for the python programming language. It is used for high level mathematical calculations. It has the large collection of functions for working with the multi-dimensional arrays, matrices, linear algebra, Fourier transform, etc. Installation of Numpy 1. Open the command or terminal on your system. 2. Enter the following command below.      #installation of NUMPY using PIP      pip install numpy       (OR)       #installation of NUMPY using CONDA      conda install numpy 3. Now, NUMPY installed on your system. To verify that use the command below.      #check for version of NUMPY      numpy --version  Tip : Use Anaconda Navigator , it comes with built-in software of JUPYTER NOTEBOOKS and pre-installed packages like Nump...

Program for Hackerrank String Object Manipulation Code in Python

Python - Hackerrank String Object Manipulation Program

Python - Hackerrank String Object Manipulation Program
Python - Hackerrank String Object Manipulation Program


Source Code

Aim :

    To program a Hackerrank string object manipulation code in python.

Algorithm :

  • Get the required input from the user by using input statement for 'q'(number of vehicle) and 'args'(Type of vehicle with respective speed limit).
  • Instantiation the Car() and Boat() class and their function with passing parameter as 'args'.
  • Write the required __str__ function definition for the class Car() and Boat().
  • The required information is successfully updated in the given file (junk.txt).


Program :

#!/bin/python3
import math
import os
import random
import re
import sys

class Car:
    def __init__(self, max_speed, speed_unit):
        self.speed = max_speed
        self.unit = speed_unit
    def __str__(self):
            return "Car with the maximum speed of {} {}".format(self.speed, self.unit)
class Boat:
    def __init__(self, max_speed):
        self.speed = max_speed
    def __str__(self):
            return "Boat with the maximum speed of {} knots".format(self.speed)

if __name__ == '__main__':
    os.environ['OUTPUT_PATH'] = 'junk.txt'
    fptr = open(os.environ['OUTPUT_PATH'], 'w')
    q = int(input("Enter the number of vehicle : "))
    queries = []
    for _ in range(q):
        args = input("Enter vehicle with respective speed unit : ").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()

Output :

Enter the number of vehicle : 
2
Enter vehicle with respective speed unit : 
car 100 kmph
Enter vehicle with respective speed unit : 
boat 82

Text That Appear On A Given File Are Below :

Car with the maximum speed of 100 kmph
Boat with the maximum speed of 82 knots


Terminal Code :

Python - Hackerrank String Object Manipulation Program01
Python - Hackerrank String Object Manipulation Program01

Python - Hackerrank String Object Manipulation Program02
Python - Hackerrank String Object Manipulation Program02


Terminal Output :

Python - Hackerrank String Object Manipulation Output
Python - Hackerrank String Object Manipulation Output


Result :

    The output was executed successfully and result was verified.


Personal Writings for Readers!!!

Story behind single family described in Pandemic Family Book.


Read more on...

Paperback - https://amzn.to/34NS6Sk

Amazon Kindle - https://amzn.to/3aKtIng

Kobo Writing - https://bit.ly/3iQysNy

Comments

Popular Posts