mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-05-11 09:01:44 -04:00
Co-authored-by: Kolade Chris <65571316+Ksound22@users.noreply.github.com> Co-authored-by: Huyen Nguyen <25715018+huyenltnguyen@users.noreply.github.com>
2.1 KiB
2.1 KiB
id, title, challengeType, dashedName
| id | title | challengeType | dashedName |
|---|---|---|---|
| 6821ecab237de8297eaee7a0 | Python Challenge 18: Second Best | 29 | python-challenge-18 |
--description--
Given an array of integers representing the price of different laptops, and an integer representing your budget, return:
- The second most expensive laptop if it is within your budget, or
- The most expensive laptop that is within your budget, or
0if no laptops are within your budget.
- Duplicate prices should be ignored.
--hints--
get_laptop_cost([1500, 2000, 1800, 1400], 1900) should return 1800
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_laptop_cost([1500, 2000, 1800, 1400], 1900), 1800)`)
}})
get_laptop_cost([1500, 2000, 2000, 1800, 1400], 1900) should return 1800
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_laptop_cost([1500, 2000, 2000, 1800, 1400], 1900), 1800)`)
}})
get_laptop_cost([2099, 1599, 1899, 1499], 2200) should return 1899
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_laptop_cost([2099, 1599, 1899, 1499], 2200), 1899)`)
}})
get_laptop_cost([2099, 1599, 1899, 1499], 1000) should return 0
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_laptop_cost([2099, 1599, 1899, 1499], 1000), 0)`)
}})
get_laptop_cost([1200, 1500, 1600, 1800, 1400, 2000], 1450) should return 1400
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(get_laptop_cost([1200, 1500, 1600, 1800, 1400, 2000], 1450), 1400)`)
}})
--seed--
--seed-contents--
def get_laptop_cost(laptops, budget):
return laptops
--solutions--
def get_laptop_cost(laptops, budget):
laptops = sorted(set(laptops), reverse=True)
if len(laptops) >= 2 and budget >= laptops[1]:
return laptops[1]
if not laptops or budget < laptops[-1]:
return 0
for price in laptops[2:]:
if budget >= price:
return price
return 0