Ask Question

Write the function onlyTwo (x, y, z) which takes in three positive integers x, y and z. The function returns an integer with the sum of the squares of the two largest numbers. Hint: the max () method could be useful Example: onlyTwo (1, 2, 3) will return 13 since 2*2 + 3*3 = 13 onlyTwo (3,3,2) will return 18 since 3*3 + 3*3 = 18

+5
Answers (1)
  1. 2 November, 18:09
    0
    The solution code is written in Python:

    def onlyTwo (x, y, z) : largest = max (x, y, z) smallest = min (x, y, z) second = (x + y + z) - (largest + smallest) sum = largest * largest + second * second return sum

    Explanation:

    Firstly, create a function named onlyTwo () that takes three parameters, x, y, z.

    Next, use the max function to find the largest number (Line 2).

    Next, use the min function to find the minimum number (Line 3).

    Since we are only expected to identify the two largest number from three numbers, we can get the second largest number by subtracting the sum of the three numbers with largest + smallest (Line 4).

    At last, we apply the equation to calculate the sum of the squares of the two largest numbers and return it as output (Line 6-7).
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “Write the function onlyTwo (x, y, z) which takes in three positive integers x, y and z. The function returns an integer with the sum of the ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers