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

 

peter@norvig.com > risk_death
prev  |  next  |  chance

The most famous [of the classic blunders] is "never get involved in a land war in Asia" - but only slightly less well-known is this: "Never go against a Sicilian when death is on the line"! In this exercise, a follow-up to the previous Risk Game exercise, you will simulate a game of Risk to the death. The function risk_death takes four arguments. The first is the number of armies in a single territory of the attacker. The second is a list of the number of armies in a sequence of territories that the attacker is attempting to conquer, in order. The third is a list of dice rolls for the attacker's dice and the fourth for the defender's dice. The attacker rolls the minimum of 3 and the number of attackers minus one; the defender rolls the minimum of 2, the number of defenders in the current territory, and the number of attackers. Then the dice are sorted, highest first, and compared in respective order. Ties go to defender. Armies are subtracted from attacker or defender, depending on results. If attacker gets down to 1, the attack is over; if defender's first territory gets down to 0 then it is conquered: the attacker has to leave one of its armies to occupy the territory, and then can go on to attack the next territory. If all defender territories are gone, the attack is over. risk_death should return a list of two things: numA and listD, as modified by the results of battles. (If numA is 0 or 1, the defender has won; if listD is [], the attacker has won.) For example, risk_death(27, [12, 1, 1, 1, 1, 5], ..., ...) means that the attacker has 27 armies and wants to conquer first a territory defended by 12 armies, then 4 territories with a single army each, and finally a territory with 5 armies.


risk_death(4, [2], [4, 3, 1, 6, 6, 6], [2, 5, 3]) → [2, []]
risk_death(7, [1, 1, 1], [6, 6, 6, 6, 6, 6, 6, 6, 6], [1, 1, 1]) → [4, []]
risk_death(7, [3, 2], [1, 4, 2, 5, 1, 3, 6, 5, 4, 3, 3, 4, 2, 6], [6, 4, 2, 3, 3, 2, 5, 1]) → [2, []]

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

def risk_death(numA, listD, diceA, diceD):

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: 557 Post-solution available

Copyright Nick Parlante 2017 - privacy