quest1 = """ Compute the growth of money ---------------------------- Ask the user the initial amount, the rate of interest and the number of years the money will be in the bank. Then ask if the money grows with simple intererest or compound interest. Depending upon the answer compute the amount of money there will be after the given number of years. Useful formulas. P = principal amount R = rate of interest T = time. Amt of money after T yrs w.r.t simple ineterest : P + PRT / 100 Amt of money after T yrs w.r.t compound ineterest : P(1 + R /100)^T """ quest2 = """ Computing volumes. -------------------- Give the user to choose a type of geometrical object, which can be a square, a rectangle, a circle or a cone (you can add more objects if you want.) Depending upon the answer enter the dimensions (like side for square, both the sides for rectangle, radius for circle, etc.) Then compute the area (in case of 2d objects) or volume (in case of 3d objects.) """ quest3 = """ Compute the Gaussian function. Input the mean, m and the standard deviation, s. Then input x and find the value of Gaussian function at x. The formula for the Gaussian function. f(x)= 1 / -1 / x - m \ 2 \ ----------- exp | --- |-------| | sqrt(2pi)s \ 2 \ s / / Can you modify the program so that if the user input s=0 or s negative, the program will print an error saying s cannot be 0 or negative? """ quest4 = """ Create a list. lst = ['a', 'b', 'c', 'e'] a. Insert the character 'd' between 'c' and 'e'. b. insert 'f' at the end. c. insert 'h' at the end. d. Insert 'g' at the end. e. delete 'h' from the list. f. insert 'h' at the end. g. print the first 3 entries of the list. h. print the last 3 entries of the list. i. print the entries of the list between position 2 and postion 5. """ quest5 = """ Test all the conditions. Input two numbers. If they are equal print that they are equal. If the first is less than the second, print that "first is smaller". If the first is greater than the second, print that "second is smaller." """ # Open an interpreter by typing # python on the terminal. quest6 = """ Experiment to see when two strings are equal, less or greater. Can you compare any two strings in python? What kind of ordering do the strings have. I expect you to do a lot of experiments on the python interpreter before answeering. Guessing is okay as long as you experiment and verify your guess. """ quest7 = """ Is the imaginary number j equal to itself. What happens when you type j == j in the interpreter. Can you explain the behaviour. If something is wrong with this, can you fix it? As we saw in class complex numbers are not ordered. If you have forgotten, try writing 1 + j > 2 + j or something like that. However we know real numbers are ordered. Does 2 + 0j > 1 + 0j return True? Experiment and see. """ quest8 = """ Enter the coefficients a, b and c of a quadratic equation a x^2 + bx + c = 0. If the discriminant is < 0 use cmath to find the complex roots. Otherwise use math.sqrt to find the two (may be identical) real roots. """ quest9 = """ Enter a, b, c, d, e and f defining the set of linear equations a x + b y = e and c x + d y = f. Find the determinant of the matrix a b c d If the determinant is zero print that the solution either does not exist, or there are too many solutions. Otherwise find x and y. """ # Answers import math import cmath print quest1 # Solution : P = input("Principal Amount : ") R = input("Rate of interest : ") R = float(R) T = input("Time : ") int_type = input("Interest is simple or compound ? ") if int_type == "simple" : print "Final amount = %g" % (P + P*R*T/100) else : print "Final amount = %g" % (P * (1 + R/100)**T) raw_input("Press enter") print quest2 # Solution # Solutions using elif are appreciated. Since I didn't do that in class # we give the following solution. print "Select the type of object from the following." print "1. Square" print "2. Rectangle" print "3. Circle" print "4. Cone." ans = input("Enter 1, 2, 3 or 4 : ") if ans == 1 : side = input("length of any one side : ") print "Area = %g" % (side * side) else : if ans == 2 : length = input("length of the rectangle : ") breadth = input("breadth of the rectangle : ") print "Area = %g" % (length * breadth) else : if ans == 3 : radius = input("radius of the circle : ") print "Area = %g" % (math.pi * radius * radius) else : if ans == 4 : radius = input("Radius of the base : ") height = input("Height of the cone : ") print "Volume = %g" % ((1.0 / 3) * math.pi * height * \ radius * radius) else : print "I do not recognize such an object." raw_input("Press enter") print quest3 # Solution m = input(" Mean : ") s = input("Standard deviation : ") x = input("Evaluation should be at x = ") x = float(x) if s <= 0 : print "Standard deviation cannot be negative or 0." else : normal_x = (x - m) / s const = 1.0 / (math.sqrt(2 * math.pi)) first_part = const / s value_f_at_x = first_part * math.exp(-0.5 * normal_x**2) print "Value of the Gaussian function with m = %g, s = %g at %g is %g" \ % (m, s, x, value_f_at_x) raw_input("Press enter") print quest4 # Solution lst = ['a', 'b', 'c', 'e'] print lst lst.insert(3, 'd') print lst lst.append('f') print lst lst.append('h') print lst lst.append('g') print lst del lst[6] print lst lst.append('h') print lst print lst[:3] print lst[5:] print lst[2:6] raw_input("Press enter") print quest5 # Solution a = input(" First number : ") b = input("Second number : ") if a == b : print ("The numbers are equal.") else : if a > b : print ("The second number is smaller.") else : print ("The first number is smaller.") raw_input("Press enter") print quest6 # The answer should be dictionary order. raw_input("Press enter for the answer.") print "Answer : Dictionary order." raw_input("Press enter") print quest7 raw_input("Press enter for the answer.") print "Answer : comparing 2 + 0j, and 1 + 0j is an error. The interpreter \ does not recognize these as real numbers." raw_input("Press enter") print quest8 # Solution print "Solving a x^2 + bx + c = 0. Input a, b, c" a = input("a = ") b = input("b = ") c = input("c = ") a = float(a) disc = b*b - 4 * a * c if disc >= 0 : sol1 = (-b + math.sqrt(disc)) / (2 * a) # The brackets in the den are # for clarity. They are not # needed. sol2 = (-b - math.sqrt(disc)) / (2 * a) else : sol1 = (-b + cmath.sqrt(disc)) / (2 * a) sol2 = (-b - cmath.sqrt(disc)) / (2 * a) print "The roots are " + str(sol1) + " and " + str(sol2) + "." raw_input("Press enter") print quest9 #soln print "Solving ax + by = e and." print "cx + dy = f." a = input("a = ") b = input("b = ") e = input("e = ") c = input("c = ") d = input("d = ") f = input("f = ") det = a * d - b * c det = float(det) if det == 0 : print "The system is either none or too many solutions." else : x = (e * d - b * f) / det y = (a * f - c * e) / det print "The solution is x = %g and y = %g" % (x, y)