## Question

2) The Monty Hall Paradox. Marilyn vos Savant described the following problem (loosely based on a game show hosted by Monty Hall) in a popular magazine: “Suppose you’re on a game show, and you’re given the choice of three doors: Behind one door is a car; behind the others, goats. You pick a door, say No. 1, and the host, who knows what’s behind the doors, opens another door, say No. 3, which has a goat. He then says to you, ‘Do you want to pick door No. 2?’ Is it to your advantage to switch?”

Ms. vos Savant proved that it is to your advantage, but many of her readers, including some mathematics professors, disagreed, arguing that the probability would not change because another door was opened.

Your task is to simulate this game show. In each iteration, randomly pick a door number between 1 and 3 for placing the car. Randomly have the player pick a door. Randomly have the game show host pick a door having a goat (but not the door that the player picked). Increment a counter for strategy 1 if the player wins by switching to the third door, and increment a counter for strategy 2 if the player wins by sticking with the original choice. Run 1,000 iterations and print both counters.

3) Credit Card Number Check. The last digit of a credit card number is the check digit, which protects against transcription errors such as an error in a single digit or switching two digits. The following method is used to verify actual credit card numbers but, for simplicity, we will describe it for numbers with 8 digits instead of 16:

• Starting from the rightmost digit, form the sum of every other digit. For Example, if the credit card number is 4358 9795, then you form the sum 5 + 7 + 8 + 3 = 23.

• Double each of the digits that was not included in the preceding step. Add all digits of the resulting numbers. For example, with the number given above, Doubling the digits, starting with the next-to-last one, yields 18 18 10 8. Adding all digits in these values yields 1 + 8 + 1 + 8 + 1 + 0 + 8 = 27.

• Add the sums of the two preceding steps. If the last digit of the result is 0, the number is valid. In our case, 23 + 27 = 50, so the number is valid. Write a program that implements this algorithm. The user should supply an 8-digit number, and you should print out whether the number is valid or not. If it is not valid, you should print the value of the check digit that would make it valid.

4) In a social networking service, a user has friends, the friends have other friends, and so on. We are interested in knowing how many people can be reached from a person by following a given number of friendship relations.

This number is called the “degree of separation”: one for friends, two for friends of friends, and so on. Because we do not have the data from an actual social network, we will simply use an average of the number of friends per user.

Write a recursive function def reachablePeople(degree, averageFriendsPerUser)

Use that function in a program that prompts the user for the desired degree and average, and then prints the number of reachable people. This number should include the original user.

5) Having a secure password is a very important practice, when much of our information is stored online. Write a program that validates a new password, following these rules:

• The password must be at least 8 characters long.

• The password must have at least one uppercase and one lowercase letter.

• The password must have at least one digit.

Write a program that asks for a password, then asks again to confirm it. If the passwords don’t match or the rules are not fulfilled, prompt again. Your program should include a function that checks whether a password is valid.

6) Write a function def sameElements(a, b) that checks whether two lists have the same elements in some order, with the same multiplicities.

For example,

1 4 9 16 9 7 4 9 11

and

11 1 4 9 16 9 7 4 9

would be considered identical, but

1 4 9 16 9 7 4 9 11

and

11 11 7 9 16 4 1 4 9

would not. You will probably need one or more helper functions.

7) Write a program that produces ten random permutations of the numbers 1 to 10. To generate a random permutation, you need to fill a list with the numbers 1 to 10 so that no two entries of the list have the same contents. You could do it by brute force, by generating random values until you have a value that is not yet in the list. But that is inefficient. Instead, follow this algorithm.

Make a second list and fill it with the numbers 1 to 10. Repeat 10 times Pick a random element from the second list. Remove it and append it to the permutation list.

## Solution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

'''The Monty Hall Paradox

'''

import random

stayResults = []

swapResults = []

for i in range(1000):

car = random.randint(1,3)

player = random.randint(1,3)

reveal = [1,2,3]

reveal.remove(car)

if player in reveal:

reveal.remove(player)

if len(reveal) == 1:

reveal = reveal[0]

else:

revealIndex = random.randint(0,1)

reveal = reveal[revealIndex]

swap = random.randint(1,2)

if swap == 2:

choices = [1,2,3]

choices.remove(player)

choices.remove(reveal)

player = choices[0]

if player == car:

result = True

else:

result = False

if swap == 1:

stayResults.append(result)

else:

swapResults.append...

By purchasing this solution you'll be able to access the following files:

Solution.zip.