Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

(a)Define the remove_triplets() function which is passed a list of integers as a parameter. The function removes all triplets from the list (i.e., removes any

(a)Define the remove_triplets() function which is passed a list of integers as a parameter. The function removes all triplets from the list (i.e., removes any three elements in the list which are exactly the same and are in sequence). For example, the following code:

a_list = [6, 6, 6, 7, 6, 6, 6, 3, 3, 3, 8, 8, 8, 3] remove_triplets(a_list) print("1.", a_list)

a_list = [6, 6, 6, 7, 6, 6, 6, 6, 6] remove_triplets(a_list) print("2.", a_list)

a_list = [6, 6, 6, 7, 6, 6, 4, 3, 3, 3, 8, 8, 8, 3] remove_triplets(a_list) print("3.", a_list)

a_list = [1, 1, 1, 4, 4, 4, 1, 1, 1] remove_triplets(a_list) print("4.", a_list)

a_list = [1, 1, 2, 1, 2, 2] remove_triplets(a_list) print("5.", a_list)

prints:

1. [7, 3] 2. [7, 6, 6] 3. [7, 6, 6, 4, 3] 4. [] 5. [1, 1, 2, 1, 2, 2] """

def remove_triplets(a_list):

def test_remove_triplets():

a_list = [6, 6, 6, 7, 6, 6, 6, 3, 3, 3, 8, 8, 8, 3]

remove_triplets(a_list) print("1.", a_list)

a_list = [6, 6, 6, 7, 6, 6, 6, 6, 6]

remove_triplets(a_list)

print("2.", a_list)

a_list = [6, 6, 6, 7, 6, 6, 4, 3, 3, 3, 8, 8, 8, 3]

remove_triplets(a_list) print("3.", a_list)

a_list = [1, 1, 1, 4, 4, 4, 1, 1, 1]

remove_triplets(a_list)

print("4.", a_list)

a_list = [1, 1, 2, 1, 2, 2]

remove_triplets(a_list)

print("5.", a_list)

list1 = []

print("6. Before:", list1, end = " ")

remove_triplets(list1)

print("After:", list1)

list1 = [1, 1, 1, 3, 8, 4, 5, 5, 5]

print("7. Before:", list1, end = " ")

remove_triplets(list1)

print("After:", list1)

(b)

In a dice rolling game a hand is made up of any number of random dice throws and is valued in the following way:

In this game a run is a sequence of dice values starting from 1, e.g., 123, 12345, 1234, 1.

Each dice which is part of a run of dice starting from a 1 has a value which is equivalent to the dice number. The value of any dice which is part of a run is added to the hand score.

If there is no 1 in a hand of dice, the score for the whole hand is 0.

A hand of dice can contain more than one run.

Study the following five example hands of dice and their corresponding valuation. Make sure you understand how the hands are valued:

[5, 3, 2, 5, 4, 5, 6, 4, 3] has value 0 [3, 4, 1, 5, 3, 1, 4, 6] has value 2 (contains one run with just the dice [1] and a second run with just [1]) [5, 3, 2, 2, 6, 4, 5, 1, 4] has value 21 (contains one run with the dice [1, 2, 3, 4, 5, 6]) [2, 1, 1, 1, 2, 3, 3, 1, 3, 2] has value 19 (contains three separate runs with the dice [1, 2, 3] and a second run with the dice [1] [3, 4, 1, 5, 2, 1, 5, 1, 2, 3, 4, 6] has value 37 (contains one run with the dice [1, 2, 3, 4, 5, 6], a second run with [1, 2, 3, 4, 5] and a third run with the dice [1])

Complete the get_hand_score() function which is passed a list of dice throws and returns the value of the hand according to the rules described above.

def get_hand_score(list_of_dice): return 0

def test_get_hand_score(): print("1. score: ", get_hand_score([5, 3, 2, 5, 4, 5, 6, 4, 3])) print("2. score: ", get_hand_score([3, 4, 1, 5, 3, 1, 4, 6])) print("3. score: ", get_hand_score([5, 3, 2, 2, 6, 4, 5, 1, 4])) print("4. score: ", get_hand_score([2, 1, 1, 1, 2, 3, 3, 1, 3, 2])) print("5. score: ", get_hand_score([3, 4, 1, 5, 2, 1, 5, 1, 2, 3, 4, 6]))

list1 = [5, 3, 2, 5, 5, 6, 4, 3, 2, 1, 1, 5, 2, 5, 1] list1_copy = list1[::] list1_copy.sort() print("6. list: ", list1) score1 = get_hand_score(list1) print(" list_sorted: ", list1_copy) print(" score:", score1) print()

list1 = [5, 3, 2, 6, 4, 5, 1, 4, 1, 2, 6, 4] list1_copy = list1[::] list1_copy.sort() print("7. list: ", list1) score1 = get_hand_score(list1) print(" list_sorted: ", list1_copy) print(" score:", score1) print()

list1 = [2, 1, 1, 1, 2, 3, 3, 2, 3] list1_copy = list1[::] list1_copy.sort() print("8. list: ", list1) score1 = get_hand_score(list1) print(" list_sorted: ", list1_copy) print(" score:", score1) #-------------------------------------------------- #--------------------------------------------------

test_remove_triplets()

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Database Management

Authors: Jeffrey A. Hoffer Fred R. McFadden

9th Edition

B01JXPZ7AK, 9780805360479

More Books

Students also viewed these Databases questions

Question

Hi there, Can I get answers for questions 10-13? Thanks

Answered: 1 week ago