id/email
password
forgot password | create account
about | help | code help+videos | done | prefs
CodingBat code practice

 

david.white@denison.edu cs112fall2015 > whacks
prev  |  next  |  chance

In the game of Whack-a-Mole, there are a bunch of mole holes arranged in a circle. The player whacks one ersatz mole on the head with a mallet, sending it back into its hole. This repeats until the end of the game. In this exercise, you will implement the function whacks(moles, holes), where moles is a list of heights of moles, and a hole number. For example whack([2, -1, -2, 1, -1], [0, 1, 3, 4]) means that there are 5 moles, and two of them are sticking their heads out of their hole: moles[0] is at height 2 above, and moles[3] is at height 1. The moles will be whacked in the order [0, 1, 3, 4]: first mole number 0, then mole number 1, and so on. A whack changes a mole's height from h to -h, and transfers h units of height to each of the immediate neighbors. So when we first whack moles[0], it goes from 2 to -2, and each of the neighbors has its height increased by 2; the neighbor moles[1] would go from -1 to +1, and since the moles are in a circle, the other neighbor is the one at the other end of the list; moles[4] (also known as moles[-1]) goes from -1 to +1 as well. If you whack a mole of zero or negative height, there is no change to the moles. You are allowed (but not required) to mutate the argument moles, as long as you return a list which is the correct outcome of the whacks. (Note: no moles were harmed in the creation of this exercise.)


whacks([2, -1, -2, 1, -1], [0]) → [-2, 1, -2, 1, 1]
whacks([2, -1, -2, 1, -1], [0, 1]) → [-1, -1, -1, 1, 1]
whacks([2, -1, -2, 1, -1], [0, 1, 3, 4]) → [1, -1, 0, 1, -2]

...Save, Compile, Run (ctrl-enter)

def whacks(moles, holes):

Editor font size %:
Shorter output


Forget It! -- delete my code for this problem

Progress graphs:
 Your progress graph for this problem
 Random user progress graph for this problem
 Random Epic Progress Graph

Python Help

Difficulty: 222 Post-solution available

Copyright Nick Parlante 2017 - privacy