Programming Assignment 4
-
(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)
returns9
(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 andpy -3 ch06_pr02.py
on Windows) user provides input:
Enter a number: 234
The digits of 234 sum to 9
- Name the file and program
-
(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 andpy -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 andpy -3 ch06_pr05.py
on Windows) user provides input:
Enter three numbers: 31, 12.4, 15
The sorted numbers are 12.4 15 31
- Name the file and program
-
(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 ashours:minutes:seconds
. For exampleconvertMillis(5500)
returns a string0:0:5
,convertMillis(100000)
returns a string0:1:40
, andconvertMillis(555550000)
returns a string154:19:10
. Your main method should take an amount of milliseconds from a user and display a string in the formathours:minutes:seconds
.- Name the file and program
ch06_pr23.py
. - Example execution
(
python3 ch06_pr23.py
on MAC andpy -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 andpy -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 andpy -3 ch06_pr23.py
on Windows) user provides input:
Enter a time in milliseconds: 555550000
Time in hours:minutes:seconds is 154:19:10
- Name the file and program
-
(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 helpfindClosestAngle
andsensePlanAct
.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. Usetime.sleep(0.05)
to allow the servo to move 5 degrees andtime.sleep(1)
to allow the servo to rotate back to degree 10. The functionsensePlanAct
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 invokesensePlanAct
.- Name the file and program
find_closest_obstacle.py
. - Example execution
(
cat find_closest_obstacle.py | ssh pi@gopigoXX python3 -u
on MAC andtype 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?
- Name the file and program
-
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 andpy -3 ch06_pr31.py
on Windows) user provides input:
Current date and time is Jan 29, 2017 16:41:5
- Name the file and program
General Instructions, Turning in assignments, and Grading
General Instructions
- Name each file and program as listed in the instructions.
- The top of each program should have a comment block where the first line
is your name and following lines are a description of your program.
# Name: Jory Denny
# This program debates Batman vs Superman!
# References:
# - Ref 1. - Any program solution that defines its own function or class must have a
main
function and properly specified entry point into the program, e.g.:
def main():
# Do something
if __name__ == '__main__':
main() - Use proper coding style (described more in Grading below)
- While I support extra research, use of online materials, and collaboration with other students, please ensure the work is your own and you understand all portions of the work you turn in.
- Follow turn-in instructions precisely.
- Failure to complete any of these steps will result in a significant loss of points.
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.
- Soft copy (Online submission)
- Accept the invitation to the classroom assignment, which creates your repository on GitHub.
- Clone this repository, which includes some starting files.
- Add assignment files (stage, commit, push).
- Modify the Coverpage.txt file to provide name, sources, signature of following the honor code, etc. Be sure to stage, commit, and push these changes as well. If you do not fill out the coverpage, I will not grade the assignment.
- After the due date, I will clone your repository and add a grade and comments as necessary. After which you can pull these changes for your records.
Additionally, robotics problems will need to be demonstrated to me. This must be demonstrated to me by 5pm on the due date.
Points
- Each assignment is graded out of 100 points (not including bonus).
- Criteria and point distribution
- If the code is not named precisely or does not execute correctly, -50% on a problem.
- Following instructions and algorithm used to solve the problem, 25% on a problem. Following instructions is extremely important in computer science, train yourself to think like a computer. There are many ways to solve a problem, some may be better or worse than others.
- Stylistic elements of written code,
25% of a problem. Style includes (but is not
limited to):
- Every class, method, and function must be documented with docstrings describing its purpose, formal arguments, and return value.
- Control flow should be documented with intent and purpose of code
- Naming should be descriptive and consistent
- Indenting properly
- Consistent spacing
- Avoiding code duplication
- While I support extra research, use of online materials, and collaboration with other students, please ensure the work is your own and you understand all portions of the work you turn in. If I suspect excessive use of online materials, I will invite you to walk me through your solution. If you are not able to explain the work sufficiently, I will not give credit for that problem.
- If there are any discrepancies in grades please see the instructor during his office hours or by appointment (do not discuss with the lab assistants or graders).