about | help | code help+videos | done | prefs |
In this exercise, we ask you to play a game of whack-a-mole. You will use the strategy: always whack the tallest mole (the one with the most-positive height; if there are several ties with the same height, whack the one that comes first in the list). Keep whacking until there are no more moles with a positive height; then the game is over and you should return a list of two things: the state of the moles after all the whacking, and the list of holes whacked. For example, whack_a_mole([-2, 1, -2]) returns [[-1,-1,-1], [1]], because moles[1] is the highest (at 1), and when you whack it, the result is that all the moles are negative. One complication: if the sum of the mole heights is positive, then the game can never end. (Why?) So in that case, just return the original list of mole heights, followed by None, to indicate that no sequence of holes can finish the game. whack_a_mole([-2, 1, -2]) → [[-1, -1, -1], [1]] whack_a_mole([2, -1, -2, 1, -1]) → [[0, -1, 0, 0, 0], [0, 1, 3, 4, 0, 3, 2, 1]] whack_a_mole([5, 4, 3, 2, 1, -20]) → [[0, 0, -1, 0, -2, -2], [0, 1, 2, 3, 4, 0, 1, 2, 3, 5, 0, 1, 2, 5, 0, 1, 4, 5, 0, 3, 4, 5, 3, 4, 3, 2]] ...Save, Compile, Run (ctrl-enter) |
Progress graphs:
Your progress graph for this problem
Random user progress graph for this problem
Random Epic Progress Graph
Difficulty: 223 Post-solution available
Copyright Nick Parlante 2017 - privacy