Programming Assignment 4


  1. (25 points) Problem 6.2 of the book. Sum the digits in an integer. Write a function that computes the sum of the digits in an integer. Use the following header:
    def sumDigits(n)
    For example, sumDigits(234) returns 9 (2+3+4). Write a main method that prompts the user to enter an integer and displays the sum of all its digits.
    • Name the file and program ch06_pr02.py.
    • Example execution (python3 ch06_pr02.py on MAC and py -3 ch06_pr02.py on Windows) user provides input:
      Enter a number: 234
      The digits of 234 sum to 9
  2. (25 points) Problem 6.5 of the book. Sort three numbers. Write the following function to display three numbers in increasing order:
    def displaySortedNumbers(num1, num2, num3)
    Write a main method that prompts the user to enter three numbers and invokes your function.
    • Name the file and program ch06_pr05.py.
    • Example execution (python3 ch06_pr05.py on MAC and py -3 ch06_pr05.py on Windows) user provides input:
      Enter three numbers: 3, 2.4, 5
      The sorted numbers are 2.4 3 5
    • Example execution (python3 ch06_pr05.py on MAC and py -3 ch06_pr05.py on Windows) user provides input:
      Enter three numbers: 31, 12.4, 15
      The sorted numbers are 12.4 15 31
  3. (25 points) Problem 6.23 of the book. Convert milliseconds to hours, minutes, and seconds. Write a function that converts milliseconds to hours, minutes, and seconds using the following header:
    def convertMillis(millis)
    The method returns a string as hours:minutes:seconds. For example convertMillis(5500) returns a string 0:0:5, convertMillis(100000) returns a string 0:1:40, and convertMillis(555550000) returns a string 154:19:10. Your main method should take an amount of milliseconds from a user and display a string in the format hours:minutes:seconds.
    • Name the file and program ch06_pr23.py.
    • Example execution (python3 ch06_pr23.py on MAC and py -3 ch06_pr23.py on Windows) user provides input:
      Enter a time in milliseconds: 5500
      Time in hours:minutes:seconds is 0:0:5
    • Example execution (python3 ch06_pr23.py on MAC and py -3 ch06_pr23.py on Windows) user provides input:
      Enter a time in milliseconds: 100000
      Time in hours:minutes:seconds is 0:1:40
    • Example execution (python3 ch06_pr23.py on MAC and py -3 ch06_pr23.py on Windows) user provides input:
      Enter a time in milliseconds: 555550000
      Time in hours:minutes:seconds is 154:19:10
  4. (25 points) Find closest obstacle. Implement a program on the GoPiGo that allows the robot to find the closest object and move towards it by 10cm. The program will do this 5 times.

    Create two functions to help findClosestAngle and sensePlanAct. findClosestAngle will use the servo to scan angles from 10 to 170 at an increment of 5 degrees and the function will return the angle at which the closest obstacle was present. Use time.sleep(0.05) to allow the servo to move 5 degrees and time.sleep(1) to allow the servo to rotate back to degree 10. The function sensePlanAct will find the closest angle (by invoking the other function), turn to face the closest obstacle, and then drive forward 10cm. Further, the method will do this 5 times and stop the robot. main will invoke sensePlanAct.
    • Name the file and program find_closest_obstacle.py.
    • Example execution (cat find_closest_obstacle.py | ssh pi@gopigoXX python3 -u on MAC and type find_closest_obstacle.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_obstacle.txt. In this file, please answer the following questions:
      • What could this basic functionality be useful for?
  5. Bonus. (20 points) Problem 6.31 of the book. Current date and time. Invoking time.time() returns the elapsed time in seconds since midnight of January 1, 1970. Write a program that displays the date and time. You must account for leap years. Months should be displayed as three letter abbreviations. The time should not account for time zones, i.e., the time displayed will be in UTC+0 or GMT. Hint: use functions liberally.
    • Name the file and program ch06_pr31.py.
    • Example execution (python3 ch06_pr31.py on MAC and py -3 ch06_pr31.py on Windows) user provides input:
      Current date and time is Jan 29, 2017 16:41:5


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