Starting from:

$25

CTA200 -Problem Set 1 Solved

1.   (Dan Green.) Write a script that finds and replaces a given word in all the .txt files in the working directory, with the following properties:

a   The script name is called with “name find replace” where find and replace are words the user chooses when running the program. b A new directory named replace is created within the working directory.

c A copy of the .txt files with find is put in the replace directory, but with all occurrences of ’find’ replaced by ’replace.’ If a file does not contain find, it is ignored.

2.   (Adapted from Newman.) The binomial coefficient is given by

 (1)

for k ≥ 1 and 1 for

a   Write your own routine to generate the binomial coefficients for a givenn and k. Make sure the result is an integer (not a float) and gives the (correct) value 1 for k = 0. (Hint; you can do a cancellation to make it more tractable.)

b   Use your routine to write out the first 20 lines of Pascal’s triangle. Thisgives the coefficients for expanding binomials of the form (x + y)n.

c    Consider a biased (e.g., bent) coin with probability p of coming up heads, and probability 1 − p of coming up tails. The probability of obtaining heads k times when flipping the coin n times is given by

                                                                    .                                            (2)

Consider an experiment in which you flip the coin n times and hope to obtain at least k values of heads. For values of p, n, and k of your choice, use your routine to calculate the probability of obtaining heads at least k times in n flips. (Example application: a baseball player with batting average p = 0.250 has n = 4 attempts to obtain a hit, or at-bats, during a given game. What is the chance that s/he will obtain at least one hit during the game, assuming that all at-bats are statistically the same and uncorrelated?)

d   Simulate the experiment N times for N ∈ {10,100,1000}, or a similar range of your choice. For each value of N, which fraction of experiments were successful? Consider showing the results in a plot.

3.   (Adapted from Newman.) The Bessel function of the first kind of order m is given by

                                                  .                              (3)

a   Write a routine to calculate this integral directly, using the methodof integration of your choice, using only the library sine and cosine functions (you may use the library Bessel functions to check the results of your integral). Plot the results for a few values of m.

b   Suppose that light passing through a circular telescope aperture of ra-dius a is focussed on the telescope’s focal plane. If the source is effectively a point on the sky, then the pattern of intensity seen in the focal plane at a distance q from the optical axis is given by

                                                                                                          (4)

where I0 is the intensity at the center, and x =  2λRπaq. Here λ is the wavelength of the light and R is the distance from the aperture to the focal plane. This is known as the “point spread function” in optical astronomy, the “beam” in radio astronomy, and the “Airy disk” in general. (The geometrical ratio R/(2a) is known as the “f-number” of the telescope and typically has values of several.) For a given set of parameters, make an image of the two-dimensional point spread function, using a monochrome color scheme. Indicate the x and y axes with real distance units. (It might be hard to see the detail away from the main beam; if so, try increasing the contrast of your image in order to see the features on the edge.)

c    Download a high-resolution astronomical (or other) image of your choice.If this image were viewed through a telescope with a finite aperture, each pixel’s value would get smeared out to neighboring pixels, degrading the image. This is known as convolution. To simulate how this image would look when viewed through a telescope, use a canned convolution routine (e.g., scipy.ndimage.filters.convolve(), scipy.signal.convolve2d()) to convolve the image by your point-spread function. Don’t worry about being quantitative in this sub-section; rather play around to see what happens to your image when you convolve it.

4.   (George) Scene: Its 3pm and your supervisor has just handed you their old Fortran77 code that they swear “solved this problem in the 80s” and “is really smartly written and easy to follow”. After the meeting you take one look at the code and realize it doesn’t. And it’s not.

But, you said you’d have results by the group meeting the following day, so lets see if we can understand what it is doing, fix it up, and get some results! Also, this code has somehow already been converted into python 3. who knows

The code can be found in the form of a jupyter notebook in problemset1/question6/question6.ipynb . Go through the examples near the top, and fix up the code in the final cell.

5.   For each point in the complex plane c = x + iy, with −2 < x < 2 and −2 < y < 2, set z0 = 0 and iterate the equation zi+1 = zi2 + c. Note what happens to the zi’s: some points will remain bounded in absolute value |z|2 = ℜ(z)2 + ℑ(z)2, while others will run off to infinity. Make an image in which your points c that diverge are given one color and those that stay bounded are given another. (Once you have done this, you can try coloring the points that diverge using a colorscale that indicates the iteration number at which the given point diverged.) Try zooming in on a portion of the image and trying again.

6.   Write your own program simulating a space battle. It should have 3 typesof spaceships: standard spaceships, warships, and speeders. Each ship has lasers, shields and hull strength (and most importantly, a name!). Warships additionally have high powered missiles which they fire 30% of the time, while speeders have a 50% chance to dodge incoming shots. When a spaceship is shot by another ship, it first depletes its shields equal to the strength of the shot. When it runs out of shields, it takes hull damage at 50% of the shot strength. Once the hull is breached, the ship is destroyed!

Your program should include a class Ship that defines the standard spaceship, and two classes which inherit from Ship called Warship and Speeder. The classes should store the ship’s shield strength, hull strength, laser power and name. There should be, at a minimum, methods to deal with when a ship shoots, is shot at, whether the ship is destroyed or not, and printing a diagnostic summary of the ship’s status.

Your program should instantiate 3 regular ships, 1 warship and 1 speeder.

The spaceships should shoot randomly at each other until only one remains (targets cannot be themselves nor ships that are already destroyed). Print a log of the battle as it progresses, and declare a final victor.

More products