Header Ads Widget

Python : Average Function | Hackerrank certification solution

Python : Average Function | Hackerrank certification solution

Implement a function that:

  1. Is named avg
  2. Takes a variable number of integer arguments; it is guaranteed that at least one argument will be passed
  3. Returns the average value of the passed arguments as a float
The implementation will be tested by a provided code stub on several input files. Each input file contains one line with space-separated arguments for the function. The function will be called with those arguments, and the returned result will be printed to the output with exactly 2 decimal places.

Example

3 arguments are read and passed to the function: 1, 2, and 3. The average is calculated to be (1+2+3) / 3=2.00. This is then returned as a float to be printed.

Constraints
  • 1<=number of arguments for the functions <=100
  • -100 ≤ value of passed arguments ≤ +100

Python : Average Function | Hackerrank certification solution

 The objective of the code is Returns the average value of the passed arguments as a float .


Sample Input
STDIN     Function
-----     -----
7      →  arguments = 7

Sample Output:

7.00

Explanation

The function will be called with 2 arguments having values 2 and 5. The average of those numbers is 3.5. This value is returned and will be printed to the output with 2 decimal places.

Code:-


import math
import os
import random
import re
import sys



# write your code here
def avg(*l):
s=sum(l)
return s/len(l)

if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
nums = list(map(int, input().split()))
res = avg(*nums)
fptr.write('%.2f' % res + '\n')

fptr.close()



EasycodingZone

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