SAMPLE written problems in Python for midterm in PSCB57 _________________________________________________________________________ During preparation we encourage the use of computer, to check if your solution works correctly. In the exam the computer will not be used, but the good thing is that we will not subtract many points for trivial mistakes like typos, as opposed to essential algorithmic and coding mistakes, such as disregarding proper indentation, using meaningless lines of code such as: 0 < x < 1, or confused use of lists in places where only arrays can be used, for instance 3*array_name has a completely different result than 3*list_name, even if they initially hold the same numbers. _________________________________________________________________________ Remember that your program(s) need to be richly commented in actual exam. _________________________________________________________________________ Problem 1. Print a three-column table of conversion of meters to miles (statute US miles) and to nautical miles. The first column should be meters = 0,100,200,300,... 10000. The table should have a header stating what the columns mean in words: " meters mi nm". Problem 2. Write an interactive program that asks for the weight in ounces and returns the corresponding number of grams. The program needs to detect the wrong negative input, and ask again. It should not print irrelevant distant decimals - round the result to reasonable accuracy before printing. For more experienced Pythonistas: find how many digits the user has entered, limit the output to the same number of digits + 1. Problem 3. Input from keyboard a floating point value of V, prompted by a message that it is expected to be in units of m/s. Create a table of angles alpha in degrees (from 1 to 90) and the corresponding distance (range) that a stone will land at, on an planet identical to Earth, except for the fact that it has no atmosphere. Stone is thrown at initial speed V, and the angle w.r.t. horizon given in the table. In the loop creating the table, detect and store the maximum range and the angle, and print them with some explanation immediately under the table. Problem 4. Write a program that reads from the keyboard integers N and M, makes sure that 0 10**2) from the starting (0,0) point. Problem 12. Write a code to find by the simplest method possible the area under the curve y = sin(x)**2/(1 + (7/8)*cos(x) )**3 from x = 0 to x = pi = 3.1415..., by dividing the x range into N=100000 intervals. Problem 13. Input from a file the two-column data representing an arbitrary number N of (x,y) pairs. The program should treat each (x,y) pair as a vertex position of a planar polygon. Compute the area of an arbitrary N-lateral polygon using: A = (1/2) *( x1*y2-x2*y1 + x2*y3-x3*y2 + x3*y4-x4*y3 + ...+ x[N-1]*y[N]-x[N]*y[N-1] ) Test the program on some simple triangle and a simple rectangle of your choice, whose area is very easy to calculate, e.g. because they contain one right angle (90 degrees). Problem 14. The rules for determining whether or not a year is a leap year are: (i) Any year that is divisible by 400 is a leap year. (ii) Of the remaining years, any year that is divisible by 100 is not a leap year. (iii) Of the remaining years, any year that is divisible by 4 is a leap year. (iv) All other years are not leap years. Write a program that lists all the years in the current century and for each calls a function called leap(year) that returns the value 0 or 1, depending on whether year is a leap year (1) or not (0), and displays the result as two columns, the first column the years, the second containing either a blank for non-leap years, or the text "leap year" for leap years. Problem 15. The value of π can be approximated by the following infinite series: π ≈ 3+ 4*[ 1/(2×3×4) + 1/(4×5×6) + 1/(6×7×8) + 1/(8×9×10) + 1/(10×11×12) +...] Write a program that displays the first 16 consecutive approximations of π. If N is the number of terms in square brackets, what is the order of convergence, that is the asymptotic value of constant q in: |π_approx - π_true| = const./N**q without doing any graphics. Hint: Without actually doing the plot, *imagine* doing a log-log plot of the consecutive approximations for N=1,2,3,...15, and print the slope that would exist on such graph between the third or forth and the N-th point (for N>4). Use the numpy's np.log10() function for the calculation.