Answer to Question #226565 in Python for Rahul

Question #226565
Problem Description :

The function accepts two positive integers ‘r’ and ‘unit’ and a positive integer array ‘arr’ of size ‘n’ as its argument ‘r’ represents the number of rats present in an area, ‘unit’ is the amount of food each rat consumes and each ith element of array ‘arr’ represents the amount of food present in ‘i+1’ house number, where 0 <= i

Note:

Return -1 if the array is null
Return 0 if the total amount of food from all houses is not sufficient for all the rats.
Computed values lie within the integer range.
Example:

Input:

r: 7
unit: 2
n: 8
arr: 2 8 3 5 7 4 1 2
1
Expert's answer
2021-08-16T14:52:17-0400
def task(r, unit, arr):
  n = len(arr)
  #We calculate the total amount of food that the rats can consume
  sum = r * unit
  sum_arr = 0
  #We calculate the total amount in the households
  for i in arr:
    sum_arr = sum_arr + i
  if len(arr) == 0:
    return -1
  elif sum > sum_arr:
    return 0
  #Where 1 represents that the household has surplus food for the rats 
  elif sum < sum_arr:
    return 1


task(7,2,[2, 8, 3, 5, 7, 4, 1, 2])
1

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS