CSCI 1100 Computer Science 1 Homework 1 Calculations and Functions Overview This homework is a combination of what was supposed to be Homeworks 1 and 2, and is worth 70 points toward your overall homework grade. There are three distinct parts to the assignment. The initial part explores how to submit homeworks for grading, including the types of error messages to expect. The other two parts practice material on Python as a calculator from Lecture 2 and the use of functions, as discussed in Lecture 3. All parts of this homework must be submitted on-line using the submission server by 11:59 pm on Wednesday, September 12 in order to receive full credit. Remember, time is measured according to the submission server s clock, not yours, and anything that is a minute or more late is considered a day late. All submissions for the homework will be going through a Department of Computer Science web server. You will log into this server, upload your Python programs, and view the results. Later in the semester we may automatically grade part of your homework, but not now while we are just getting started. You will either soon be receiving or have already received an email from root@cs.rpi.edu informing you that you have an account on the RPI CS department machines where you can submit homework assignments. This will include a password. You are welcome to use this password throughout the semester (it is pretty secure, but hard to remember), but you are encouraged to change it. The email contains instructions on how to use ssh to log onto a department server and make the change. The email also has a pointer to the website http://www.cs.rpi.edu/twiki/view/labstaffweb/labstaffssh with more detailed instructions. Note that you are only using this account for homework submissions, and it will disappear at the end of the semester. If you do not receive an email from labstaff@cs.rpi.edu by Thursday night (9/6), please send email to this address. Once you have established the account, you are ready to submit homework solutions. However, you can work on Part 1 and Part 2 of the homework even before you work on the submission server. Also, there will be time available in lab next week to help students with submission issues. Part 0: Using the Submission Server Start by creating a folder for CS 1 homework inside your dropbox and subfolder for HW 1. From the course Piazza site under Homework 1 on the Resources page, download the file part0.py. This simple Python program to compute the surface area of a rectangular solid (the sum of the areas of the six rectangular faces) contains both syntax errors (two) and a semantic error. Before fixing these errors we want you to start by just submitting the incorrect program to the on-line server to see what happens. By the end of this part of the homework, you will fix both the syntax errors and the semantic error. Please follow these steps: 1. Look at part0.py in the Wing IDE (on your virtual machine) and, without making any changes, attempt to run it (click the green triangle/arrow). It will complain of syntax error(s). Do NOT fix these errors yet. Instead, submit this to the homework server to see what happens.
2. To do this, using a web browser log onto the site https://submit.cs.rpi.edu/submit.php?course=csci1100 You will see three rows of radio buttons in the table for HW 1, one for each part of the homework. Click on the button for Part 0, then click on the Choose File button, browse your file system to part0.py, and click on Send File. The homework server will attempt to run this program using Python. It will fail, of course, because of the syntax errors, and the server will generate output that looks like this This is what the server shows you when your program has syntax errors. Remember, Python only shows you the first error! 3. Please find and fix the two syntax errors, making sure that the program runs on your virtual machine using the Wing IDE. It would be best if you do not fix the semantic error yet. 2
4. Submit the syntactically-correct version of part0.py to the homework server, again using the Part 0 button. (This illustrates the fact that you can resubmit the same homework multiple times we will grade your last submission!) The server will now be able to run your program. It will display your results side-by-side with the expected results, and you will be able to see any discrepancies. Here is an example 5. Finally, fix the semantic error in the program and resubmit, doing so until your answer matches ours. Once you do so, you will earn the full 10 points for part 0. Congratulations! Part 1 This part is worth 30 points. Before getting started, practice writing Python expressions and assignment statements using the Python interpreter! If nothing else, repeat some of the problems we worked on during lecture to see if you can solve them yourself... For this part of the homework you are going to write one Python (.py) file that outputs the results of several calculations. Your code should use variables, expressions, assignment statements and print statements. You may write a function to make your code shorter, as discussed in Lecture 3, but while it is desirable it is not necessary in order to earn full credit. To make your code easier to read, your.py file should have blank lines separating the groups of calculations. The distances (in miles) of the planets to the sun and the diameters of the five planets closest to the sun are as follows: Planet Diameter Distance Mercury 3,032 35,983,610 Venus 7,531 67,232,360 Earth 7,926 92,957,100 Mars 4,222 141,635,300 Jupiter 88,846 483,632,000 Write Python code that outputs the name Mercury and then outputs on each successive line its distance to the sun relative to the distance of the earth from the sun, its diameter relative to the earth s diameter, 3
the time, in minutes, that light takes to travel from the sun to Mercury (use 186,000 miles per second as the speed of light), and Mercury s volume relative to that of the earth. When you run the program (in the Wing IDE) the output should look something like Mercury relative distance 0.387099102704 relative diameter 0.382538480949 light time (minutes) 3.22433781362 relative volume 0.0559790323342 Repeat the calculations for Venus, Mars and Jupiter, outputting a blank line after the output for each planet. At this point, you can introduce a function if you wish to perform the calculations for a single planet and then call this function once for each of the four planets. The final output from your program should be the average distance of the five planets from the sun and the average diameter of the planets. You may use integer arithmetic to complete these calculations. The complete output from your program should look very close to Mercury relative distance 0.387099102704 relative diameter 0.382538480949 light time (minutes) 3.22433781362 relative volume 0.0559790323342 Venus relative distance 0.723262236021 relative diameter 0.950164017159 light time (minutes) 6.02440501792 relative volume 0.857477483265 Mars relative distance 1.52366306608 relative diameter 0.532677264698 light time (minutes) 12.6913351254 relative volume 0.151144546868 Jupiter relative distance 5.20274406151 relative diameter 11.209437295 light time (minutes) 43.3362007168 relative volume 1408.48243619 Average planetary distance in miles is 164288074 Average planetary diameter in miles is 22311 In order to complete Part 1 of the HW 1, please submit the file (e.g. part1.py) containing your solution to the submission server. Be sure to submit for Part 1, which is the second submission line on the website. Do not submit anything other than your single Python file! 4
Final note: You could write a Python program that simply prints the output provided above. It would start with something like print "Mercury" print " relative distance 0.387099102704" print " relative diameter 0.382538480949" print " light time (minutes) 3.22433781362" print " relative volume 0.0559790323342" print "" etc. If you do this, you will earn 0 points for the assignment. You must let Python do all of the calculations. To illustrate, if we suddenly realized that we had mistyped Earth s distance from the sun, you should have to change only one line of your code and the rest of your program should work correctly! The same is true of Part 2. Part 2 This part is worth 30 points. Before getting started, practice writing Python functions using the Python interpreter! If nothing else, repeat some of the problems we worked on during lecture to see if you can solve them yourself... The (fictional) Acme computing and social networks company has large data centers distributed throughout the world and these centers consume a large amount of energy each year. Environmental organizations would like Acme to reduce its carbon footprint, and pay in carbon credits for the pollution it produces. Each data center is formed by a sequence of standard shipping containers, each containing 1,160 servers, and each requiring about 250 kilowatts of energy. A common measurement of energy usage is to multiple the kilowatts times a unit of time, typically an hour, yielding the kilowatt-hour. As a sanity check to make this clear, these numbers imply that a container uses 250 24 365 2 million kilowatt-hours per year. To give a sense of the scale of this number, an efficient refrigerator uses about 500 kilowatt-hours of energy per year. Each data center uses about 200 containers, and Acme has about 15 data centers. The unnamed environmental organization wants Acme to pay $12 per ton of carbon used, and Acme consumes about 0.0006 tons of carbon per kilowatt-hour. Using this information, write a Python program to calculate and output how much enery Acme uses for the year 2012 and how much Acme must pay in carbon credits. Assuming Acme adds 4 containers to each center in each year and adds 2 data centers in 2013, 5 in 2014, and 3 in 2015, how much must Acme pay in carbon credits each year? The central part of your program is that you must write a function that takes the number of centers that Acme has and the number of containers per center and calculates the amount of money Acme must pay. The output from your program should be In 2012 Acme should pay $ 47304000.0 In 2013 Acme should pay $ 54683424.0 In 2014 Acme should pay $ 72154368.0 In 2015 Acme should pay $ 83570400.0 5
Please submit your program using the submission server, being sure to select Part 2, which is the third row of submissions. Do not submit anything other than your single Python file! A Note on Grading Most of the points for this assignment will be given just for getting the calculations and output correct. As we progress through the semester, however, the quality and clarity of your programs will become increasingly important. To practice this, please try to choose variable names that have meaning in regards to the calculations your code is performing and make your code as readable as possible by introducing blank lines between important sections of code. 6