HW 1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Northeastern University

DS2000 – Programming with Data


Fall 2022, Profs. Muzny and Rachlin

Homework 1: Input/Output

Introduction
The most basic function of any piece of software is to transform input into output. When thinking about
the requirements of a program, software developers often speak of the ”Input/Output (I/O) Specification”:
What form of input will the program receive, and what is the target output that the program must
produce. A program is written or implemented to meet these I/O requirements. It’s up to you, as the
coder, to figure out how to perform this transformation. For example, with electronic medical records,
a doctor might enter a patient’s age and weight and the name of a medication into a computer. A
program then computes the correct dosage - the output. In this first homework you’ll write several small
programs that transform input into output. We’ll specify the required user-inputs and you will write
a small program to generate the expected outputs. You’ll use some built in python functions in your
implementation including input, print, and round along with arithmetic operators (+, -, *, /, //, %,
**).

Expectations
Write a separate program (program.py) to answer each question. Be sure to document your code with
a descriptive header and concise comments. It isn’t necessary to comment every line, particularly those
lines whose purpose is obvious. Instead, use comments to outline the main steps of your program. Use
variable names that are descriptive, concise, and not excessively verbose. Practice good style: x = 5, not
x=5 and separate sections of your program by blank lines. For your homework submission, submit these
files:

• running.py: your program for question 1

• mortgage.py: your program for question 2

• light.py: your program for question 3


4

• output.pdf or output.txt: The output for each of your programs combined into a single file.
This can be either a simple text file, or you can paste screenshots into a word document and save
the resulting file to a .PDF. (Do not submit a .DOC or .DOCX file. Please convert the document to
a PDF-formatted file so that the graders can see your output in gradescope without
with having to download
your output file.)

You are welcome to upload a file multiple times, but only your last submission will be graded. Verify
that all files have been submitted.
DS2000, Fall 2022, Profs. Muzny and Rachlin – Homework 1: Input/Output 2

Question 1 (running.py)
Write a program where a runner inputs their running distance (kilometers - float) and time (minutes
and seconds - integers). The program should output the average pace measured in minutes per mile.
There are 1.61 kilometers per mile. Note that the program should accept the distance as a floating point
(decimal) number, where as the total running time is entered as two integers (minutes and seconds).

Enter distance [km]: 5.2


Enter time [minutes]: 29
Enter time [seconds]: 18
Your pace was 9 minutes 4.3 seconds per mile.

Question 2 (mortgage.py)
Suppose you are buying a house. Write a program to calculate your monthly mortage payment. The
inputs are:

1. P - The principal loan amount

2. i - The annual interest rate (5% = 0.05)

3. y - The number of years over which to pay back the loan.


i
Your monthly interest rate is therefore: r = 12 and the number of payments is n = y ∗ 12 (because you
are making 12 monthly payments per year.) The formula for the monthly mortgage payment is then:
P r (1+r)n
payment = (1+r)n −1

Principal loan amount: 300000


Annual interest rate : 0.05
Loan term (years) : 30
Monthly payment : 1610.46

Question 3 (light.py)
Light travels at 299,792,458 meters per second. Input an astronomical object’s distance in miles - an
integer. Report the number of hours, minutes, and seconds it takes for light from that object to reach the
Earth. Seconds should be rounded to one decimal place. The examples below are for the approximate
distances to the Moon, the Sun, and Pluto in miles. Assume that 1.61 kilometers = 1 mile and 1000
meters = 1 kilometer.

Enter distance [miles]: 240000


Light travel time: 0h 0m 1.3s

Enter distance [miles]: 93000000


Light travel time: 0h 8m 19.4s

Enter distance [miles]: 3000000000


Light travel time: 4h 28m 31.1s

You might also like