Search This Blog

Sanjay is trying to help Vijay to find the Area of a Triangle. But for some reason he is trying to find some relation between area and pi. let's help him find that based on the condition below. - Hands On- Python - Using Float Hacker Rank Questions and Answers Course ID: 55193

 Python - Using Float 1

Sanjay is trying to help Vijay to find the Area of a Triangle. But for some reason he is trying to find some relation between area and pi. let's help him find that based on the condition below.

Function 'triangle' takes three parameters called 'n1', 'n2' as float and 'n2' as integer.

  • Formula for 
            *Area:(n1*n2)/2
  • Return both values Area and Pi rounded off to 'n3' decimal places.     
NOTE: You can get the value of pi from math module.

Input Format for Custom Testing:

Input from stdin will be processed as follows and passed to the function.
  • The first line contains an float n1, the Height of the triangle.
  • The second line contains an float n2, the base of the triangle.
  • The third line contains an integer 'n3' which specifies the number of decimal places to be rounded off.
Sample Case 0
Sample Input

STDIN      Function
-----      --------
9       →  n1 = 9
5       →  n2 = 5
4       →  n3 = 4

Sample Output

(22.5, 3.1416)
Explanation
  • Because the value of Area at one decimal place it stops at 5, but for pi it returns to 4 decimal places which is the value of n3.
Answer:
#!/bin/python3

import math
import os
import random
import re
import sys


#
# Complete the 'tria' function below.
#
# The function is expected to return an INTEGER.
# The function accepts following parameters:
#  1. FLOAT n1
#  2. FLOAT n2
#  3. INTEGER n3
#

def triangle(n1, n2, n3):
    # Write your code here
    Area = round(((n1*n2)/2),n3)
    PI = round(math.pi,n3)
    return Area,PI

if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    n1 = float(input().strip())

    n2 = float(input().strip())

    n3 = int(input().strip())

    result = triangle(n1, n2, n3)

    fptr.write(str(result) + '\n')

    fptr.close()



Click on Image for better understanding:



No comments:

Post a Comment

If you have any doubts, Please let us know.