reference link to problem - https://www.hackerrank.com/challenges/electronics-shop/problem


Approach 1: Correct output but on some test cases gave Run time Error

#!/bin/python3

import os
import sys

#
# Complete the getMoneySpent function below.
#
def getMoneySpent(keyboards, drives, b):
    #
    # Write your code here.
    #
    #Part One: Finding sum of 2 items one 1 keyboard, 1 USB drive
    #Part One: Finding sum of 2 items one 1 keyboard, 1 USB drive
    keyboards_drives = []
    for position, value in enumerate(keyboards):
        for drive_pos, drive_val in enumerate(drives):
            sum = value + drive_val
            keyboards_drives.append(sum)
    print(keyboards_drives)


    for pos, item in enumerate(sorted(keyboards_drives)):
        print("item to be processed next ", item)
        if item > b:
            print("item removed ", item)
            keyboards_drives.remove(item)
            print(keyboards_drives)
        print(keyboards_drives)

    # If price is greater than budget print -1 else 1
    if len(keyboards_drives) !=0:
        print(max(keyboards_drives))
        output = max(keyboards_drives)
    else:
        print("-1")
        output = -1

    return output
        
if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    bnm = input().split()

    b = int(bnm[0])

    n = int(bnm[1])

    m = int(bnm[2])

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

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

    #
    # The maximum amount of money she can spend on a keyboard and USB drive, or -1 if she can't purchase both items
    #

    moneySpent = getMoneySpent(keyboards, drives, b)

    fptr.write(str(moneySpent) + '\n')

    fptr.close()


Test Case

374625 797 951
183477 732159 779867 598794 596985 156054 445934 156030 99998 58097 459353 866372 3337

Expected Output
374625


Resulted in a Run Time Error


Further Tinkering. Optimized the logic. Reducing couple of lines.

Approach 2:

#!/bin/python3

import os
import sys

#
# Complete the getMoneySpent function below.
#
def getMoneySpent(keyboards, drives, b):
    #
    # Write your code here.
    #

    #Part One: Finding sum of 2 items one 1 keyboard, 1 USB drive
    keyboards_drives = []
    for position, value in enumerate(keyboards):
        for drive_pos, drive_val in enumerate(drives):
            sum = value + drive_val
            if sum <= b:
                keyboards_drives.append(sum)
    print(keyboards_drives)

    #If price is greater than budget print -1 else 1
    if len(keyboards_drives) !=0:
        print(max(keyboards_drives))
        output = max(keyboards_drives)
    else:
        print("-1")
        output = -1

    return output
   


if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    bnm = input().split()

    b = int(bnm[0])

    n = int(bnm[1])

    m = int(bnm[2])

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

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

    #
    # The maximum amount of money she can spend on a keyboard and USB drive, or -1 if she can't purchase both items
    #

    moneySpent = getMoneySpent(keyboards, drives, b)

    fptr.write(str(moneySpent) + '\n')

    fptr.close()