Electronics Shop Algorithm

ยท

4 min read

Howdy fellow programmers! ๐Ÿ‘‹๐Ÿฝ

In this article, I'll be describing how I solved the Electronics Shop Challenge on HackerRank using Python 3.

This challenge is part of the Implementation challenges in the Algorithm section on HackerRank.

If this is your first time reading my data structures and algorithms article, please consider reading more in my Algos in Plain English series.๐Ÿ˜ƒ

Problem ๐ŸŽฒ

A person wants to determine the most expensive computer keyboard and USB drive that can be purchased with a given budget. Given price lists for keyboards and USB drives and a budget, find the cost to buy them. If it is not possible to buy both items, return -1.

Example

b = 60
keyboards = [40, 50, 60]
drives = [5, 8, 12]

The person can buy a

40 keyboard + 12 USB drive = 52, or a

50 keyboard + 8 USB drive = 58.

choose the latter as the more expensive option and return 58.

Function Description

Complete the getMoneySpent function in the editor below.

getMoneySpent has the following parameter(s):

  • int keyboards[n]: the keyboard prices
  • int drives[m]: the drive prices
  • int b: the budget

Returns

int: the maximum that can be spent, or -1 if it is not possible to buy both items

Input Format

The first line contains three space-separated integers b, n, and m, the budget, the number of keyboard models and the number of USB drive models.

The second line contains n space-separated integers keyboard[i], the prices of each keyboard model.

The third line contains m space-separated integers drives, the prices of the USB drives.

Sample Input 0:

image.png

Sample Output 0:

image.png

Explanation:

Buy the 2nd keyboard and the 3rd USB drive for a total cost of 8 + 1 = 9.

I'll get right into the algorithm. Please feel free to read the full question on HackerRank.

Solution ๐Ÿ”Ž

In plain English, you go to an electronics shop with a budget, and you need to buy the most expensive keyboard and mouse under that budget. Once you buy them, you should return the sum of their prices. if it isn't possible to buy them within the budget, return -1.

SuperstoreAmySosaGIF.gif

To test our solution, let's consider the same sample input from above.

b = 10
keyboards = [3, 1]
drives = [5, 2, 8]

Step 1๏ธโƒฃ: Sum all keyboards and mice prices. โŒจ๏ธโž•๐Ÿ–ฑ๏ธ

The first step in solving this problem is to get the sum of all combinations of keyboards and mice prices.

We'll start by returning a list of the element-wise addition of items in both the keyboards and the drives lists in a variable prices.

    prices = [k + d for k in keyboards for d in drives]
    # [8, 5, 11, 6, 3, 9]

Step 2๏ธโƒฃ: Get all price sums within the budget.

We'll return all prices that are below or equal to the budget in a below_budget list.

    below_budget = [price for price in prices if price <= b]
    # [8, 5, 6, 3, 9]

Notice 11 is not part of this list.

Step 3๏ธโƒฃ: Develop the 'money spent` logic.

We are told to return -1 if the price of the keyboard and mouse is above the budget b or return the highest price within the budget.

  1. Using a simple if-else statement will be sufficient.
  2. We'll store -1 in a variable, not_possible.
  3. Recall that in Step 2, we returned a list of all prices below the budget.
  4. This means, that if below_budget is an empty list, the prices are above the budget, hence we return -1.
  5. If below_budget is not empty, we'll return its highest price.
    not_possible = -1

     if below_budget == []:
        return not_possible
    else:
        return max(below_budget )

     # 9

Step 4๏ธโƒฃ: Putting it all together

Full code.

def getMoneySpent(keyboards, drives, b):
    #
    # Write your code here.
    #
    not_possible = -1

    prices = [k + d for k in keyboards for d in drives]

    below_budget = [price for price in prices if price <= b]

    if below_budget == []:
        return not_possible
    else:
        return max(below_budget)

getMoneySpent([3, 1], [5, 2, 8], 10)
# 9

Thanks for reading!

Hope this article helped with understanding this challenge. Happy Coding!๐Ÿ‘จ๐Ÿฝโ€๐Ÿ’ป

Buy me a coffee.png

ย