OutFriday, September 18, 2026
DueFriday, September 25, 2026, 7:00 pm

Overview

This assignment practices reading, testing, and writing programs that use functions and conditionals. We also use it to build an understanding of what a “model” is, using the context of computing home energy usage. This familiar domain is designed to prepare us to explore the costs of generative AI a bit later in the semester.

Learning objectives

After this assignment, you should be able to:

AI and collaboration policy for this assignment

No AI usage: all parts of this assignment should be done manually. While these are small programs, practicing writing code out for yourself is one of the ways that you develop the “muscle memory” for coding that will help you on larger assignments later. Avoid the AI temptation and write these for yourself. If you're having trouble, come to office hours!

Setup

}

Do not put your name in your Pyret/.arr files, so we can grade it anonymously.

Part 1Variations on Programs that Ask Questions

The file conditionals.arr contains three programming problems, each of which comes with 3 or 4 proposed solutions. This exercise has you explore different ways to write programs with conditionals, as well as learn to think about mistakes that can arise when working with conditionals. For this part, we want you to do two things:

Task 1

For each problem, create a check: block that distinguishes the correct solutions from the incorrect solutions. Your check: block will contain a collection of tests that you believe capture the requirements of the problem. A program that is correct should pass all of your tests, while a program that has an error should fail at least one of your tests.

Focus on the different characteristics of valid inputs and include tests that capture the relevant ones. For example, numbers can be positive, zero, or negative; they could also be larger or smaller. These characteristics might be relevant for some problems but not others. Figure out which characteristics matter, then use that to guide writing your tests. You aren't aiming for a particular number of tests, but rather a systematic approach to checking characteristics. You shouldn't need more than a single-digit number of tests for any of these problems.

Task 2

For each incorrect solution, indicate where the error lies in the code. Specify which line or expression has the error and give a one-sentence description of what was incorrect. You are not editing the code: you are reading the code, diagnosing it, and explaining the problem to another human.

Part 2Developing a Model of Home Energy Usage

One of your roommates is concerned about the energy usage in your apartment. They complain about lights being left on and phone chargers being plugged into the wall. At the same time, they also drive to campus, own three game consoles, and replace their laptop every two years. Which behavior is worse? In this part, we’ll learn what it means to build a model of energy usage and apply it to comparing scenarios of home energy usage. This sets the foundation for us to look at the energy usage of LLMs in upcoming assignments.

Energy terminology

Discussions of energy consumption often reference kilowatt-hours (units of energy consumed) and CO2e (carbon-dioxide equivalent, a shorthand for greenhouse gas emissions). Many gasses other than carbon dioxide trap heat in the atmosphere; in practice, all emissions get converted into units of CO2 to ease comparisons.

The energy-lib.arr file mentioned in the Setup section has values for the wattage of various appliances and the CO2 emissions from various regions of the world. In practice, wattage is printed on each appliance. Emissions vary during the year, so these figures are annual averages. A more refined analysis would use a finer-grained model, but this suffices for our purposes; just don’t quote the CO2 figures as static facts.

Examples and Tests

As you write functions for the rest of this assignment, include a where: block on each one showing three examples of what the program does. These examples don't need to be comprehensive, but they should illustrate different input characteristics or scenarios.

Running one appliance

Task 3

Write a program device-kwh that computes how much energy an appliance uses over a period of time. The program takes the name of an appliance and a number of hours, and produces the kilowatt-hours used.

Task 4

Write a program device-carbon that gives the emissions of one appliance run. The program takes the name of an appliance, a number of hours, and the name of a region, and produces the grams of CO2e released by running that appliance for the given period of time in the given location.

For someone with limited experience with energy, hearing that their usage is “114 grams of CO2e” isn’t relatable or meaningful. In practice, energy messaging often reports usage relative to other usages that people understand, such as “less than driving a mile”.

Task 5

Write a program carbon-label that turns a carbon figure into a plain-language comparison. The program takes a number of grams of CO2e and produces one of the strings "less than charging a phone", "less than driving a mile", "less than a burger", or "more than a burger". Use the constants PHONE-CHARGE-G, MILE-DRIVEN-G, and BURGER-G from the energy library file.

Comparing choices

In practice, we compare the (potential) energy usage of different device and lifestyle choices.

Task 6

Write a program annual-savings-kg that measures the payoff of swapping appliances. The program takes the name of the current appliance, the name of a replacement, the hours per day both are run, and the name of a region, and produces the kilograms of CO2e saved over a year by making the swap. Assume 365 days and assume both appliances run the same hours.

Task 7

Write a program annual-carbon-kg that scales one day’s habit up to a year. The program takes the name of an appliance, the hours per day it runs, and the name of a region, and produces the kilograms of CO2e from running it that many hours every day for a year. Assume a year is 365 days.

Your program should also check that the provided number of hours is no more than 24. If it is more than 24, raise an error with the message "more than 24 hours indicated".

Task 8

Write a program greener-run that takes an appliance and the hours it is run, another appliance and the hours it is run, and a region. The program returns a string consisting of the name of whichever appliance emits less CO2. If there is a tie, return the string "no difference".

Task 9

Write a program needs-attention that screens appliances worth a second look. The program takes the name of an appliance and the hours per day it runs, and produces true when the appliance either draws more than HIGH-DRAW-WATTS while running or runs for more than LONG-RUNTIME-HOURS a day. Appliances that always need to be on should yield false, regardless of their wattage draw.

Task 10

So far, we’ve only looked at the cost of using an appliance. We haven’t considered the cost to build the appliance in the first place.

Write a program dominant-lifecycle that says which half of an appliance’s life dominates its footprint. The program takes the name of an appliance, the hours per day it runs, a number of years, and the name of a region, and produces "manufacturing", "use", or "comparable" — the last when neither figure is more than twice the other. Use embodied-kg from the library.

Settle the argument

Task 11

Time to settle the argument with your roommate. Use the functions you have written to put the hall light, the laptop, and one car trip your roommate takes regularly all on the same scale. Then answer two questions in your worksheet document:

  • What are the relative costs for each of these three uses of energy? Use a couple of sentences to report your findings.
  • Now, interpret the findings. Either write a couple of sentences of response to your roommate or briefly describe how the findings aligned (or not) with what you expected. There's no right or wrong answer; this is just communication practice.

Part 3Reflection

Task 12

In the appropriate spot in your worksheet, answer the Reflection questions about how this assignment went for you. This part isn't graded other than for completion, but your answers help us understand what is and isn't working for future assignments (and reflection helps you commit learning to memory).

Grading

For the first two tasks, we'll check whether your tests are adequate and your identified fixes accurate. For the remaining programming tasks, we will run a collection of our own tests against your functions to check their behavior.

Code Conversations

For the remaining problems, you will sign up to have a conversation with the TAs about your solutions. These conversations are designed to help you practice talking about programs, while also being a good way for us to give you feedback about what you're learning from the assignments. They're also a way for us to make sure that you understand the work that you are turning in.

To help everyone get comfortable with code conversations, on this assignment you'll hold them in pairs: two students and two TAs meeting together (this form of grading is new for our TAs as well). As the semester progresses, you'll do these one-on-one with TAs. Don't worry about doing well on this first one -- we know you don't know how to do this, so this will give you a sense of what to expect for later in the course when these start to count much more towards your grade.

Handin

Hand in these files, together, in one submission:

  • hw2-code.arr
  • conditionals.arr

Submit to Gradescope. You may submit as many times as you like before the deadline; we grade the last submission.