Appendix C — Assignment 3

STAT 201

Author

YOUR NAME

Instructions

  1. Write your name on the assignment.

  2. Write your code in the Code cells of the template provided to write solutions for the assignment. Do not open a new notebook, and work from scratch. Ensure that the solution is written neatly enough to understand and grade.

  3. Use Quarto to print the .ipynb file as HTML. You will need to open the command prompt, navigate to the directory containing the file, and use the command: quarto render filename.ipynb --to html. Submit the HTML file.

  4. You may talk to a friend, discuss the questions and potential directions for solving them. However, you need to write your own solutions and code separately, and not as a group activity. Do not use AI to solve the problems.

  5. If your document is not clean and organized, you can lose up to 2 points:

    • Must be an HTML file rendered using Quarto.
    • There aren’t excessively long outputs of extraneous information (e.g. no printouts of unnecessary results without good reason, there aren’t long printouts of which iteration a loop is on, there aren’t long sections of commented-out code, etc.). There is no piece of unnecessary / redundant code, and no unnecessary / redundant text
    • The code follows the python style guide for naming variables, spaces, indentation, etc.
    • The code should be commented and clearly written with intuitive variable names. For example, use variable names such as number_input, factor, hours, instead of a,b,xyz, etc.

C.1 Question 1 (5 points)

  1. Create a tuple called my_data that contains the following elements: 'dog', 9, True, 2, 8, 'cat', 3

  2. Unpack only the string elements in my_data. You can store these elements by any variable name of your choice. Print the variables.

  3. Convert my_data to a list object. Then use indexing in 2 different ways to print the second object in your list. Note: that means you will have the same object (9) being printed out twice.

  4. Use a loop to remove any elements in my_data that are NOT integers. Print my_data to prove you were successful.

Note: This needs to be a loop that would work even if you changed the elements in my_data.

  1. Write a function that takes a list of integers as an input and returns both the minimum and maximum value. Store the output of the function when it is run with my_data from part d. Print your stored variables in a sentence to prove you were successful. This sentence should be the only thing printed.

C.2 Question 2 (2 points)

Create a shopping list by asking the user for a grocery item to add to the list object.

Continue asking the user for items until the user types “done”.

Only once the user is “done”, print the grocery list.

Run your program with at least 3 items added to your list.

C.3 Question 3 (2 points)

Ask the user for a grocery item and the price of the item (2 inputs).

Create a dictionary where the grocery item is the key and the price is the value.

Continue asking the user for items and prices until the user types “done”.

Only once the user is “done”, print the object.

Run your program with at least 3 entries.

C.4 Question 4 (2 points)

Take a space-separated list of numbers as input from the user and calculate the sum of all the numbers

C.5 Question 5 (4 points)

The score list below contains the credit score for an individual at the end of the year and the year list contains the corresponding year. ie: at the end of 2010 the individual had a credit score of 680.

score = [680, 685, 695, 690, 715, 720, 710]
year = [2010, 2011, 2012, 2014, 2015, 2016, 2017]

C.5.1 Part a (1 point)

The information for 2013 is missing. In year, insert the year 2013 between 2012 and 2014. In score, insert the credit score of 2013 as the average of the credit score of 2012 and 2014 (use indexing - never use fixed values if it can be avoided). Print year and score to prove you were successful.

C.5.2 Part b (3 points)

Determine which year had the largest percentage increase in credit score. Round your percent to 2 decimal places (0.00%).

For example, the percent change in 2011 is equal to 100*(685 - 680)/680 = 0.74%.

There are MANY ways to accomplish this and you may do so using any techniques we have learned so long as you are using code.

Print only the sentence:

                "The individual had the largest increase in {year} of {change} percent."
                

For example if the largest increase happened to be in 2011 (it didn’t):

                "The individual had the largest increase in 2011 of 0.74 percent."

C.6 Question 6 (4 points)

Suppose 4 individuals are trying to decide which toppings to put on a shared pizza.

The pizza dictionary below contains the names of the individuals and list of corresponding topping preferences.

pizza = {
    'Ava': ["sausage", "mushroom", "bacon"],
    'Billy': ["pepperoni", "bacon", "sausage", "olives"],
    'Carol': ["ham", "pineapple", "olives", "mushroom"],
    'David': ["pepperoni", "bacon", "mushroom", "green pepper", "mushroom"]
}

If at least 3 individuals listed a topping it will be included on the shared pizza.

Create a list of the toppings that at least 3 individuals included as a preference. Print only this list object.

There are MANY ways to accomplish this and you may do so using any techniques we have learned so long as you are using code.

C.7 Question 7 (11 points)

C.7.1 Part a (1 point)

Create a list called suits that contains the following elements: “hearts”, “spades”, “diamonds”, “clubs”

Create a list called numbers that contain the following elements: 2, 3, 4, 5, 6, 7, 8, 9, 10, “J”, “Q”, “K”, “A”

No need to print any output here.

C.7.2 Part b (3 points)

Use a nested loop to create a nested data structure called deck that is a list of dictionaries.

This data structure should contain every combination of suits and numbers created in Part a.

Clarification:

  • This list should contain 52 dictionaries each representing a card.
  • Each dictionary will have a key for suit and value.
  • Example: The first object in the list is {‘suit’: ‘hearts’, ‘value’: 2}

Print only your deck object.

C.7.3 Part c (1 point)

Import the random module and name the alias rm. Use the sample() function from this module to sample 2 cards from the deck you created in Part b. Assign the first object to player1 and the second object to player2.

Print player1 and player2’s cards to prove you were successful.

C.7.4 Part d (4 points)

Write a function that takes 2 input values (each input will be a dictionary containing keys: “suit” and “value”). This function should only return the card (dictionary) that is the highest number. If both numbers are the same then the function should return the string “Tie”.

Store the output of the function as winning_card when the function is run using player1 and player2 created in Part c.

Print winning_card.

Clarification:

  • 2 < … < 10 < J < Q < K < A
  • All suits are the same: 2 of spades = 2 of diamonds
  • The word input in a function does NOT refer to “user input”. Recall, a function takes any number of input objects and returns any number of output objects.
  • This function should not print or return anything other than what was specified above (ie: the winning card or the word “Tie”).

C.7.5 Part e (2 points)

Write a conditional statement (using the objects created above) to print one of the following statements:

- "Player 1 wins."
- "Player 2 wins."
- "Player 1 and Player 2 tie."