Lists Are Passed by Reference
~10 mindebugging
Write with_bonus(scores, bonus) that returns a new list with bonus added to the end. The list you were given must be unchanged afterwards.
The tests check both the returned value and that the input survived.
Examples
The bonus is added to the end of the returned list
- Input
- with_bonus([70, 80], 100)
- Output
- [70, 80, 100]
Adding to an empty list gives a one-item list
- Input
- with_bonus([], 5)
- Output
- [5]
The original list is left untouched (this is what catches append-in-place)
- Input
- with_bonus([1, 2], 3)
- Output
- [1, 2]
Hints
Hint 1
Build the result up as you go, then return it.
Hint 2
Watch for this: mutated caller list.
Requirements
scores: a list of numbers. Must not be modified.bonus: the value to addReturn a new list: the original values followed by bonus.
Constraints
Standard library only, no imports required
Time limit: 200 ms, Memory: 64 MB
Where this shows up
~10 min
••••••
3 employers weight this skill
2 frontier labs, 1 AI product company. Top match scores 80.
Python
def with_bonus(scores, bonus):
"""
Return a new list with one extra value on the end.
Args:
scores: a list of numbers. Must not be modified.
bonus: the value to add
Returns:
A new list: the original values followed by bonus.
"""
# YOUR CODE HERE
pass
def _check_unchanged(scores, bonus):
"""Calls with_bonus and reports the caller's list afterwards."""
original = list(scores)
with_bonus(scores, bonus)
return list(scores)