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.
  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.
  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.
  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.
  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.