#!/usr/bin/env python # -*- coding: utf-8 -*- """ Python solutions for Prof. Dhande's homework (week 2). We use functions on that example, but they still dont know functions. TODO: create 5 files, one by problems. @date: on Sat Jan 17 19:41:30 2015. @author: Lilian Besson. """ # Homework Problem #1 def problem1(xmin=-100, xmax=100, ymin=-100, ymax=100): """ Solve the first problem. """ print "\n\n# Problem #1 : check if a point is in a square work space." print "The work-space [{xmin},{xmax}] × [{ymin},{ymax}].".format(xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax) x = float(raw_input("Give me the x coordinate for P : x = ")) y = float(raw_input("Give me the y coordinate for P : y = ")) print "Alright, I will tell you if P(x={x}, y={y}) is in the work-space [{xmin},{xmax}] × [{ymin},{ymax}].".format(x=x, y=y, xmin=xmin, xmax=xmax, ymin=ymin, ymax=ymax) # So it is quite easy: # if ((xmin <= x) and (x <= xmax)) and ((ymin <= y) and (y <= ymax)): # This syntax is also possible if (xmin <= x <= xmax) and (ymin <= y <= ymax): print "Yes, the point P is indeed inside!" return True else: print "No, the point P is not inside..." return False # Precision of the checks for the next exercise epsilon = 0.0001 # Homework Problem #2 # INFO: this problem is simpler is you use the vectorial product def isLeftOrRight(p=(0, 0), q=(0, 1), a=(1, 1)): """ Check if the point is on the line, on its left or on its right, by computing the z coordinate of the vector product. Return 0 if on the line, < 0 if on the left, > 0 if on the right.""" return ((q[1] - p[1]) * (a[0] - p[0])) - ((q[0] - p[0]) * (a[1] - p[1])) def problem2(): """ Solve the second problem. """ print "\n\n# Problem #2 : take two points P, Q, and take a third point A, then check if it is on the left, on or on the right of the line (P,Q)." # Input point P print "\nFor the first point P:" p_x = float(raw_input("Give me the x coordinate for P : p_x = ")) p_y = float(raw_input("Give me the y coordinate for P : p_y = ")) # Input point Q print "\nFor the second point Q:" q_x = float(raw_input("Give me the x coordinate for Q : q_x = ")) q_y = float(raw_input("Give me the y coordinate for Q : q_y = ")) # Input point A print "\nFor the third point A:" a_x = float(raw_input("Give me the x coordinate for A : a_x = ")) a_y = float(raw_input("Give me the y coordinate for A : a_y = ")) # Compute the coordinates of the vectors (P, Q) and (P, A) in order to do the vector product # And now check if the point is on the line or not # By computing the z coordinate of the vector product check_for_A = isLeftOrRight(p=(p_x, p_y), q=(q_x, q_y), a=(a_x, a_y)) if check_for_A == 0: print "The point A is on the line!" elif abs(check_for_A) < epsilon: print "The point A is very close to the line! (precision is not perfect here)..." elif check_for_A > 0: print "The point A is on the 'left' of the line (direction from P to Q)." elif check_for_A < 0: print "The point A is on the 'right' of the line (direction from P to Q)." else: print "Some weird thing is here!" return False return True # Homework Problem #3 def problem3(): """ Solve the third problem. """ print "\n\n# Problem #3 : take a point O and a radius r, and take a second point A, then check if it is inside, on or outside the circle C(O, r)." # Input point P print "\nFor the first point O:" o_x = float(raw_input("Give me the x coordinate for O : o_x = ")) o_y = float(raw_input("Give me the y coordinate for O : o_y = ")) # Input radius r print "\nFor the radius r:" r = float(raw_input("Give me the radius : r = ")) assert r >= 0 # Input point A print "\nFor the second point A:" a_x = float(raw_input("Give me the x coordinate for A : a_x = ")) a_y = float(raw_input("Give me the y coordinate for A : a_y = ")) # And now check if the point is on the circle or not from math import sqrt # This is durty, usually we import modules in the beginning of the program distance = sqrt((a_x - o_x)**2 + (a_y - o_y)**2) if distance < r - epsilon: print "The point A is inside the circle C(O, r) !" elif r - epsilon <= distance <= r + epsilon: print "The point A is on the circle C(O, r) !" elif r + epsilon < distance: print "The point A is outside the circle C(O, r) !" else: print "Some weird thing is here!" return False return True # Homework Problem #4 def problem4(): """ Solve the forth problem. """ print "\n\n# Problem #4 : ask x,y, then define three points A(-x,-y), B(x, -y), and C(0, y), and take a second point P(x1, y1), then check if it is inside, on or outside the triangle (A, B, C)." # Input point P print "\nFor the two values x, y:" x = float(raw_input("Give me the x value, with x > 0 please: x = ")) assert x > 0 y = float(raw_input("Give me the y value, with y > 0 please: y = ")) assert y > 0 a_x, a_y = -x, -y b_x, b_y = x, -y c_x, c_y = 0, y # Input point A print "\nFor the second point P:" x1 = float(raw_input("Give me the x1 coordinate for P : x1 = ")) y1 = float(raw_input("Give me the y1 coordinate for P : y1 = ")) # And now check if the point is on the triangle or not check_ab = isLeftOrRight(p=(a_x, a_y), q=(b_x, b_y), a=(x1, y1)) check_bc = isLeftOrRight(p=(b_x, b_y), q=(c_x, c_y), a=(x1, y1)) check_ca = isLeftOrRight(p=(c_x, c_y), q=(a_x, a_y), a=(x1, y1)) if check_ab == 0: print "The point P is on [A, B] !" elif check_bc == 0: print "The point P is on [B, C] !" elif check_ca == 0: print "The point P is on [C, A] !" elif check_ab < 0 and check_bc < 0 and check_ca < 0: print "The point P is inside the triangle [A, B, C]." else: print "The point P is outside the triangle [A, B, C]." return True # Homework Problem #5 def problem5(easy=False): """ Solve the fifth problem. """ print "\n\n# Problem #5 : print the first K odd number." K = int(raw_input("Give me the value of K : K = ")) if easy: # Quick, easy solution: for i in range(1, 2*K, 2): print i else: # # Pretty solution: # s = " ".join(map(str, xrange(1, 2*K, 2))) # # For more help, see str.join?, map? and xrange? # # Another solution, with a comprehension list: # s = " ".join([str(i) for i in xrange(1, 2*K, 2)]) # # For more help, see str.join? and xrange? # The simplest solution: s = "" for i in range(1, 2*K, 2): s = s + str(i) + " " # We add that integer to the string print s # Launch all the tests. if __name__ == '__main__': problem1() problem2() problem3() problem4() problem5()