Programming Assignment 2


  1. (25 points) Problem 4.1 of the book. Algebra: solve quadratic equations. The two roots of a quadratic equation, for example, ax2+bx+c=0, can be obtained through the classic quadratic formula. In this, b2-4ac is called the discriminant of the quadratic equation. If it is positive, the equation has two real roots. If it is zero, the equation has one real root. If it is negative, the equation has no real roots.
    Write a program that prompts the user to enter values for a, b, and c and displays the result based on the discriminant. If the discriminant is positive, display two roots. If the discriminant is 0, display one root. Otherwise, display The equation has no real roots.
    You may need to import math in order to use sqrt().
    • Name the file and program ch04_pr01.py.
    • Example execution (python3 ch04_pr01.py on MAC and py -3 ch04_pr01.py on Windows) user provides input:
      Enter a, b, c: 1.0, 3, 1
      The roots are -0.3819660112501051 and -2.618033988749895
    • Example execution (python3 ch04_pr01.py on MAC and py -3 ch04_pr01.py on Windows) user provides input:
      Enter a, b, c: 1, 2.0, 1
      The root is -1.0
    • Example execution (python3 ch04_pr01.py on MAC and py -3 ch04_pr01.py on Windows) user provides input:
      Enter a, b, c: 1, 2, 3
      The equation has no real roots
  2. (25 points) Problem 4.19 of the book. Compute the perimeter of a triangle. Write a program that reads three edges for a triangle and computes the perimeter if the input is valid. Otherwise, display that the input is invalid. The input is valid if the sum of every pair of two edges is greater than the remaining edge.
    • Name the file and program ch04_pr19.py.
    • Example execution (python3 ch04_pr19.py on MAC and py -3 ch04_pr19.py on Windows) user provides input:
      Enter three edges: 1, 1, 1
      The perimeter is 3
    • Example execution (python3 ch04_pr19.py on MAC and py -3 ch04_pr19.py on Windows) user provides input:
      Enter three edges: 1, 3, 1
      The input is invalid
  3. (25 points) Problem 4.23 of the book. Geometry: point in a rectangle. Write a program that prompts the user to enter a point (x, y) and checks whether the point is within the rectangle centered at (0, 0) with width 10 and height 5. For example, (2, 2) is inside the rectangle and (6, 4) is outside the rectangle.
    • Name the file and program ch04_pr23.py.
    • Example execution (python3 ch04_pr23.py on MAC and py -3 ch04_pr23.py on Windows) user provides input:
      Enter a point with two coordinates: 2, 2
      Point (2.0, 2.0) is in the rectangle
    • Example execution (python3 ch04_pr23.py on MAC and py -3 ch04_pr23.py on Windows) user provides input:
      Enter a point with two coordinates: 6, 4
      Point (6.0, 4.0) is not in the rectangle
  4. (25 points) Find Closest Obstacle. Implement a program on the GoPiGo that reads three different distance values, one with the servo at 45°, one with the servo at 90°, and one with the servo at 135°. In order to read the successive values, be sure to give the robot time (using time.sleep(0.5)) to rotate the servo. Set the appropriate LED on for the closest reading, i.e., turn on the left LED if the closest obstacle is toward the left, turn the LED on the Light and Color sensor if the closest obstacle is in front of the robot, and turn the right LED on if the closes obstacle to toward the right. After 2 seconds of time, turn the LED off. Note, that if one of the LEDs does not turn on, the robot itself may be broken, not your code.
    • Name the file and program find_closest.py.
    • Example execution (cat find_closest.py | ssh pi@gopigoXX python3 -u on MAC and type find_closest.py | ssh pi@gopigoXX python3 -u on Windows, remember to use the correct hostname) user provides input (output may vary):
      No textual output.
    • Create a file find_closest.txt. In this file, please answer the following questions:
      • What happens in your program if two distance readings were identical?
      • Do you think this is a common occurance? Why or why not?
  5. Bonus. (10 points) Problem 4.17 of the book. Game: scissor, rock, paper. Write a program that plays the popular scissor-rock-paper game. (A scissor can cut paper, a rock can knock a scissor, and a paper can wrap a rock.) The program randomly generates a number 0, 1, or 2 representing scissor, rock, and paper. The program prompts the user to enter a number 0, 1, or 2 and displays a message indicating whether the user or the computer wins, loses, or draws. You need to handle invalid input by the user.
    • Name the file and program ch04_pr17.py.
    • Example execution (python3 ch04_pr17.py on MAC and py -3 ch04_pr17.py on Windows) user provides input:
      scissor (0), rock (1), paper(2): 1
      The computer is scissor. You are rock. You won
    • Example execution (python3 ch04_pr17.py on MAC and py -3 ch04_pr17.py on Windows) user provides input:
      scissor (0), rock (1), paper(2): 2
      The computer is paper. You are paper too. It is a draw


General Instructions, Turning in assignments, and Grading

General Instructions

Turn in Instructions

Each assignment will be turned in through GitHub classroom. Please find the link to create a repository at the top of this page, or through this link. Robot problems must be demonstrated by 5pm and assignments must be submitted to GitHub by 11:59pm of the due date. I do not accept any late assignments.

Additionally, robotics problems will need to be demonstrated to me. This must be demonstrated to me by 5pm on the due date.

Points