# You must make sure to run all cells in sequence using shift + enter or you might encounter errors
from pykubegrader.initialize import initialize_assignment
responses = initialize_assignment("1_homework", "week_2", "homework", assignment_points = 57.75, assignment_tag = 'week2-homework')
# Initialize Otter
import otter
grader = otter.Notebook("1_homework.ipynb")
๐ Python Programming: Explore the Nutshell Studies ๐ต๏ธโโ๏ธ๐#
The Nutshell Studies of Unexplained Death were created by Frances Glessner Lee as miniature dioramas replicating real crime scenes. These models are used to train investigators in observing details, solving mysteries, and analyzing evidence.
In these Python exercises, youโll recreate parts of this fascinating process by solving problems involving data types, numerical precision, lists, dictionaries, and basic calculations.
Question 1: Scaling the Mystery ๐งฎ๐#
โEverything must be scaled to perfection!โ
The Nutshell Studies require precise scaling to recreate scenes. You are given:
The real room width is 15.5 feet.
The model room width is given as a string:
"4.25"
inches.
Tasks:#
Convert the room width and length to inches. Save the results in variables
room_width_inches
androom_length_inches
.Convert the variable
model_width
to a float, replace the original variable. To do this you should use thefloat()
function.Calculate the scale ratio by dividing the model width by the room width. Save the result in a variable
scale_ratio
.Calculate the real-world area:
Compute the real-world area of the rectangular room using the formula:
$\( \text{Real-world area} = \text{width (in feet)} \times \text{length (in feet)} \)$This will give the area in square feet.
Save the result in a variable
real_area_sqft
.
Calculate the scaled diorama area:
First, calculate the area of the real room in square inches by using the width and length in inches and multiplying them.
Scale this area down using the square of the scale ratio:
\[ \text{Scaled diorama area} = \frac{\text{Real area (in square inches)}}{(\text{scale ratio})^2} \]
Save the result in a variable
scaled_area_sqin
.
def question_1():
# Given information
# Room dimensions
room_width_feet = 15.5 # Real room width in feet
room_length_feet = 20 # Real room length in feet
model_width = "4.25" # Model width as a string
# Convert dimensions to inches for consistency
...
# Convert the model width to a float
...
# Calculate the scale ratio
...
# Real-world area (in square feet) and scaled diorama area (in square inches)
...
return room_width_inches, room_length_inches, model_width, scale_ratio, real_area_sqft, scaled_area_sqin
# We have provided this code for you to test your implementation
room_width_inches, room_length_inches, model_width, scale_ratio, real_area_sqft, scaled_area_sqin = question_1()
# Print results
print(f"Scale of the model: {scale_ratio:.6f}")
print(f"Real-world area: {real_area_sqft:.2f} square feet")
print(f"Scaled diorama area: {scaled_area_sqin:.2f} square inches")
grader.check("scaling-the-mystery")
Question 2: Cataloging Crime Scene Objects ๐๐ช#
โWhatโs in the crime scene?โ
A crime scene contains various objects: "knife"
, "glass"
, "rug"
, "chair"
, and "lamp"
.
To investigate, youโll need to create and modify a list.
Tasks:#
Build a list of object,
objects
, containing the objects mentioned above.make sure to add a comment saying
List of objects found at the crime scene
.
Add three more objects:
"hat"
,"pen"
,"rope"
.There are several ways to add items to a list. You can use the
append()
method or the+
operator.
Calculate the number of objects at the crime scene and save the result in a variable
total_objects
. To do this, use thelen()
function.Retrieve and print the 3rd, 5th, and last objects from the list.
Retrieve the third object and assign it to the variable
third_object
.Retrieve the fifth object and assign it to the variable
fifth_object
.Retrieve the last object and assign it to the variable
last_object
. Make sure this will always get the last object, regardless of the number of objects in the list, by using the index-1
.
Write the code to print information:
The updated list of objects, โUpdated list of objects: [list of objects]โ.
The total number of objects, โTotal number of objects: [total_objects]โ.
The third object, โ3rd object: [third_object]โ.
The fifth object, โ5th object: [fifth_object]โ.
The last object, โLast object: [last_object]โ.
def question_2():
# List of objects in the crime scene
# Build a list of object, `objects`, with the following objects: "knife", "glass", "rug", "chair", "lamp"
...
# Add three more objects: `"hat"`, `"pen"`, `"rope"`.
...
# Calculate the total number of objects using the `len()` function
...
# Retrieve specific objects
# Retrieve the third object and assign it to the variable `third_object`.
...
# Retrieve the fifth object and assign it to the variable `fifth_object`.
...
# Retrieve the last object and assign it to the variable `last_object`.
...
# The updated list of objects, "Updated list of objects: [list of objects]".
...
# The total number of objects, "Total number of objects: [total_objects]".
...
# The third object, "3rd object: [third_object]".
...
# The fifth object, "5th object: [fifth_object]".
...
# The last object, "Last object: [last_object]".
...
return objects, total_objects, third_object, fifth_object, last_object
objects, total_objects, third_object, fifth_object, last_object = question_2()
grader.check("cataloging-crime-scenes")
Question 3: Object Details Dictionary ๐๏ธ๐#
โTrack every detail!โ
Each object in the Nutshell diorama has attributes like its position, whether itโs a clue, and its priority for solving the case.
Tasks:#
Create a dictionary
objects_details
with the following details (this should be a nested dictionary):knife: position =
"near victim's hand"
, is_clue =True
, priority =8
glass: position =
"on the table"
, is_clue =False
, priority =3
rug: position =
"center of the room"
, is_clue =False
, priority =2
lamp: position =
"overturned near the corner"
, is_clue =True
, priority =6
Update the priority of
"glass"
to7
.Retrieve and print the position and priority of the
"knife"
and"rug"
. Save the knife position and priority in variablesknife_position
andknife_priority
, and the rug position and priority in variablesrug_position
andrug_priority
.
import copy
def question_3():
# Dictionary of objects with details
# 1. Create a dictionary with the following details (this should be a nested dictionary):
# - **knife**: position = `"near victim's hand"`, is_clue = `True`, priority = `8`
# - **glass**: position = `"on the table"`, is_clue = `False`, priority = `3`
# - **rug**: position = `"center of the room"`, is_clue = `False`, priority = `2`
# - **lamp**: position = `"overturned near the corner"`, is_clue = `True`, priority = `6`
...
# Do not modify this code, it is used for grading
original_details = copy.deepcopy(objects_details)
# Update the priority of the "glass"
# 2. Update the **priority** of `"glass"` to `7`.
...
# Retrieve details for "knife" and "rug"
# 3. Retrieve and print the **position** and **priority** of the `"knife"` and `"rug"`.
# Save the knife position and priority in variables `knife_position` and `knife_priority`,
# and the rug position and priority in variables `rug_position` and `rug_priority`.
...
return (
original_details,
objects_details,
knife_position,
knife_priority,
rug_position,
rug_priority,
)
original_details, objects_details, knife_position, knife_priority, rug_position, rug_priority = question_3()
# Print results
print("Updated dictionary:", objects_details)
print("\nDetails of knife:")
print("Position:", knife_position)
print("Priority:", knife_priority)
print("\nDetails of rug:")
print("Position:", rug_position)
print("Priority:", rug_priority)
grader.check("Object-Details-Dictionary")
Bonus Challenge: Handling Missing Objects ๐ต๏ธโโ๏ธโ#
The investigators occasionally encounter missing data for certain objects in the crime scene. Use dictionary methods like .get()
to safely handle cases where some objects might not exist in the dictionary.
Scenario:#
The following objects and their priority scores are provided:
knife: priority = 8
glass: priority = 7
rug: priority = 2
lamp: priority = 6
An investigator asks about the priority of the following objects:
knife
hat (not in the dictionary)
rug
book (not in the dictionary)
Tasks:#
Use the
.get()
method to retrieve the priorities of the requested objects.Assign a default priority of
0
for any objects not found in the dictionary.
def bonus():
# Dictionary of objects with their priority scores
priority_scores = {
"knife": 8,
"glass": 7,
"rug": 2,
"lamp": 6
}
# Use the `.get()` method to safely retrieve priorities, with a default value of 0 for missing objects
# Save the retrieved priorities in variables knife_priority, hat_priority, rug_priority, and book_priority
...
return knife_priority, hat_priority, rug_priority, book_priority
knife_priority, hat_priority, rug_priority, book_priority = bonus()
# Print the retrieved priorities
print("Retrieved priority scores:")
print(f"Knife: {knife_priority}")
print(f"Hat: {hat_priority}")
print(f"Rug: {rug_priority}")
print(f"Book: {book_priority}")
grader.check("handeling-missing-objects")
from IPython.display import IFrame
IFrame(
"https://americanart.si.edu/exhibitions/nutshells/inside",
width=800,
height=800,
)
Submitting Assignment#
Please run the following block of code using shift + enter
to submit your assignment, you should see your score.
from pykubegrader.submit.submit_assignment import submit_assignment
submit_assignment("week2-homework", "1_homework")