Computer Science, asked by jagritishakya78742, 3 days ago

Create a function that takes a list containing more than one sublists as an argument or parameter. Each sublist has 2 elements. The first element is the numerator and the second element is the denominator.

Return the sum of the fractions rounded to the nearest whole number.

For example: Consider the input to be [[18, 13], [4, 5]] with two sublists,

Step 1: First, consider the sublist [18, 13]. Divide the first number in this list i.e. 18 by the second number in this list i.e. 13 using list indexing method.

Step 2: Next, consider the second sublist [4, 5]. Divide the first number in this list i.e. 4 by the second number in this list i.e. 5 using list indexing method. There can be more than two sublists. So, perform Step 1 and Step 2 using for loop.

Step 3: Calculate the sum of fractions by adding the results obtained in Step 1 and Step 2.

Step 4: Round off the result to the nearest whole number using the round() function. The syntax of round() function is:

Syntax: round(float_num, num_of_decimals)

where,

float_num is the number to be rounded.
num_of_decimals is the number of decimal places to be considered while rounding. If not specified, number will be rounded to nearest whole number.
For example:

round(2.18,1) will give 2.2
round(2.18) will give 2

Answers

Answered by aryanroy2204
3

Answer:

Everything explained in the comments

Explanation:

def return_average(alist):

   newlist = [] # list to append to

   for sublist in alist: # for loop for going through all elmenets

       newlist.append(sublist[0]/sublist[1]) # dividing 1st element by 2nd element and appending

   sum_of_list = sum(newlist) # sum(list) adds up all the numbers

   return round(sum_of_list) # round will give us our nearest whole number

# SAMPLE TEST

mylist = [

   [18,13],

   [4,5]

   ] # 18/13 = 1.XYZ < 1.5, 4/5 = 0.8, 1.XYZ + 0.8 = round(2.XYZ < 2.5) == 2

output = return_average(mylist)

print(output) # 2

Answered by vinod04jangid
0

Answer:

def ListFunction(List, num_of_decimals):

  new_list = []

  for subList in List:

      new_list.append(subList[0]/subList[1])

  sum_of_list = sum(new_list)

  return round(sum_of_list, num_of_decimals)

List = [[18,9],[4,2]]

num_of_decimals = int(input("Enter number of decimal places for ounding"))

result = ListFunction(List, num_of_decimals)

print(result)

Explanation:

First we define a list List with 2 sublists inside it. Then we take user input for the decimal place to which we want to round our value. We call the function ListFunction( ) with the argument of List and num_of_decimals. We have defined a new list named new_list then we iterate through our list List and append the fraction value of 1st and 2nd element of each sublist into new_list. Then we add all the elements of new_list and return it's rounded figure back to the function.

#SPJ2

Similar questions