--- id: 6a22d77ddf034bc4e35b1d54 title: "Challenge 347: Game Theory" challengeType: 29 dashedName: challenge-347 --- # --description-- Given two equal length strings representing two players' strategies for a game, return the scores as an array `[player1, player2]`. - The given strings will only contain one of two letters: `"C"` (cooperate) or `"D"` (defect). - Each character represents one round, scored as follows: - If both players cooperate, each scores 3. - If both players defect, each scores 1. - If one player defects and the other cooperates, the defector scores 5 and the cooperator scores 0. # --hints-- `play_game("CCCC", "CCCC")` should return `[12, 12]`. ```js ({test: () => { runPython(` from unittest import TestCase TestCase().assertEqual(play_game("CCCC", "CCCC"), [12, 12])`) }}) ``` `play_game("DDDD", "DDDD")` should return `[4, 4]`. ```js ({test: () => { runPython(` from unittest import TestCase TestCase().assertEqual(play_game("DDDD", "DDDD"), [4, 4])`) }}) ``` `play_game("CCDD", "CDDD")` should return `[5, 10]`. ```js ({test: () => { runPython(` from unittest import TestCase TestCase().assertEqual(play_game("CCDD", "CDDD"), [5, 10])`) }}) ``` `play_game("CCCDCDCCCDDC", "CCDDCDCDDCCD")` should return `[24, 34]`. ```js ({test: () => { runPython(` from unittest import TestCase TestCase().assertEqual(play_game("CCCDCDCCCDDC", "CCDDCDCDDCCD"), [24, 34])`) }}) ``` `play_game("DDCCDDDDCDDCDDDCDD", "CCDCCCDCCCDCCCCDCC")` should return `[66, 21]`. ```js ({test: () => { runPython(` from unittest import TestCase TestCase().assertEqual(play_game("DDCCDDDDCDDCDDDCDD", "CCDCCCDCCCDCCCCDCC"), [66, 21])`) }}) ``` # --seed-- ## --seed-contents-- ```py def play_game(p1, p2): return p1 ``` # --solutions-- ```py def play_game(p1, p2): scores = [0, 0] for i in range(len(p1)): if p1[i] == 'C' and p2[i] == 'C': scores[0] += 3 scores[1] += 3 elif p1[i] == 'D' and p2[i] == 'D': scores[0] += 1 scores[1] += 1 elif p1[i] == 'D' and p2[i] == 'C': scores[0] += 5 else: scores[1] += 5 return scores ```