Home > database >  Why do I have a RunTime error for my Python code?
Why do I have a RunTime error for my Python code?

Time:03-06

I am doing the Hackerrank basic Python programming challenges. This one is called Mini-Max Sum* and the link for the problem is: https://www.hackerrank.com/challenges/one-week-preparation-kit-mini-max-sum/problem?isFullScreen=true&h_l=interview&playlist_slugs[]=preparation-kits&playlist_slugs[]=one-week-preparation-kit&playlist_slugs[]=one-week-day-one

I was able to have my code compile for the sample cases, but it does not work for any of the other cases. I am very confused as to why I receive a Run Time Error. Please help!

#!/bin/python3

import math
import os
import random
import re
import sys

#
# Complete the 'miniMaxSum' function below.
#
# The function accepts INTEGER_ARRAY arr as parameter.
#

def miniMaxSum(arr):
    
    # Find the minimum number:   
    minimum = 10^9
    for integer in arr:
        if integer < minimum:
            minimum = integer

            
    # Search the list to find the minimum and remove it:
    arr.remove(minimum)
    
    
    # Sum all of the entries in the array; this gives the maximum sum for four integers:
    max_sum = 0
    for number in arr:
        max_sum  = number
# --------------------------------------------------------
    arr.append(minimum)
        
    # Find the maximum number:
    
    maximum = 1
    for integer in arr:
        if integer > maximum:
            maximum = integer
             
    
    # Search the list to find the maximum and remove it:
    arr.remove(maximum)
    
    # Sum all of the entries in the array; this gives the maximum sum for four integers:
    min_sum = 0
    for number in arr:
        min_sum  = number
        
    print(min_sum, max_sum)
    
    
           

if __name__ == '__main__':

    arr = list(map(int, input().rstrip().split()))

    miniMaxSum(arr)

CodePudding user response:

Problem lies in first line of function miniMaxSum().

def miniMaxSum(arr):
    
    # Find the minimum number:   
    minimum = 10^9

^ denotes the xor operation not power/exponentiation. Use ** in python instead.

10^9 = 3 (in binary 1010 ^ 1001 = 0011). Hence you initialize minimum with 3 instead of 1000,000,000

Why do you get Runtime error then?

arr.remove(minimum)

minimum = 3 may not exist in arr.


Your link to problem has expired, prefer including the statement. Current link to problem: https://www.hackerrank.com/challenges/mini-max-sum/problem

Statement:

Given five positive integers, find the minimum and maximum values that can be calculated by summing exactly four of the five integers. Then print the respective minimum and maximum values as a single line of two space-separated long integers.

  • Related