This is your 1st homework assignment that requires coding!  Please read ALL the instructions carefully, especially those on the main homework page.

Read the whole assignment before you begin.


IMPORTANT:  For this assignment I want you to write program (A .py python code file that can be run at the command line).  No JupyterLab Notebooks for this HW!

(0) Completion of Labs and Reading

  • If you did not yet complete the in-class work or the weekly reading, then you may want to finish that first. 

(1)  Checkout the HW03 repository:

Login to Rivanna

Use this link to accept the assignment and create your repository on GitHub:   https://classroom.github.com/a/h7qCeV9S

After you accept the assignment and the repository and it exists in your GitHub, as we did in class02, clone the repository into your working area on Rivanna. 

(2) String Manipulation  (3 points)

      Write a python program (myname.py - starter code in the repository) that uses a string of your full name, "the name game". 


For me, that would by

MYNAME="Robert Craig Group".


Split this string into words, get the initials, and count how many letters there are.  Depending on how many names you have (I have three - use the "len" function to count yours), have your program print out each name.  Your program should work for names with up to 4.  You will need to use the "if or else/if" conditional to only print the names that were given if less and 4.  


Your program output should look like this:


Your first name is: Robert

Your second name is: Craig

Your third name is: Group

Your initials are: R.C.G.

Number of letters in your name is:  16

(3) Handling user input  (3 points)

User input with the input function. 

Copy the name program from above to "name_input.py".  Rather than use a string defined in the program, this version should request the user's name, and then print the output for the name that is given.  Hint:  use the built-in "input" function.  Make sure that your program works for names with up to 4 sub-names.  Think about what input validation is necessary so that your program won't crash if a user gives poor input?   Implement error handling with the "try/except" technique discussed in class.  


(2) Calculate π  Using Monte Carlo Integration (4 points)

PI can be easily calculated using Monte Carlo techniques. Write a program to do this called mc_pi.py.  There is starter code in the repository.  Your starter code allows the user to input how many random coordinates (N_MC) to throw at the command line  (remember sys.argv?).  Do you understand the starter code?  What happens if you put in bad data?  


Let's say that you know that the area of a circle is πr2. Assume a unit circle centered in a square of area 4 units. Generate N_MC uniform random pairs of numbers inside the area of the square for the x and y coordinates, and check to see if the point is inside of the circle (x2+y2<1). Hint: be careful with where you place your origin and how you calculate the radial distance from the center of the circle.  By counting how many points fall inside of the circle you can estimate the area of the circle.  From that value you can calculate pi.

Try 10,000 random points. How close are you to the true value of π given by the Python math module (import math, math.pi)?  Compare in your program and print the comparison to the screen. 

Use the time module to measure how long your program took to run. That is, in your program use code something like this:


import time
start_time = time.time()
main()
print("--- %s seconds ---" % (time.time() - start_time))

(4) Push to GitHub

Add all of the ".py" files to your repository and push it to GitHub. 

Note: if you use "git add -A" it will add all of the files in your directory to the repository so that you don't have to 'get add' each file separately.

  • No labels