CSC3217 Artificial Intelligence and Expert System - 2
CSC3217 Artificial Intelligence and Expert System - 2
Assigned: 02/10/2022
Due: Sunday, 09/10/2022
Assignment Objectives:
The goal of this assignment is to help you solve problems and implement algorithms in Python
using the tools discussed in class: values, datatypes, and variables, operators, and conditional
"if/else" statements.
Getting Started:
This assignment requires you to write Python code to solve computational problems. To help you
get started on the assignment, we will give you a basic starter file for each problem. These files
contain function stubs ('function stubs' are functions that have no bodies; the function has a name
and a list of parameters, but it is up to you to complete the body of the function) and a few tests
you can try out to see if your code seems to be correct (note that the test cases we give you in the
starter files are just examples; we will use different test inputs to grade your work!). You need to
complete (fill in the bodies of) these functions for the assignments. Do not, under any
circumstances, change the names of the functions or their parameter lists.
Directions:
• Each starter file has a comment block at the top with various important information. Be
sure to add your information (name, ID number, and NetID) to the first three comment
lines, so that we can easily identify your work.
• Each of your functions must use the names and parameter lists indicated in the starter
code file.
• Each of your functions must explicitly return its final answer; the grading program will
ignore anything that your code prints out. Along those lines, do NOT use input()
anywhere within your functions; your functions should get all of their input from their
parameters.
• Be sure to submit your final work as directed by the indicated due date and time. Late
work will not be accepted for grading.
• Programs that crash will likely lose a lot of credit, so make sure you thoroughly test your
work before submitting it.
• Submit all of your completed hw1problemX.py files on Blackboard for Programming
Homework 01.
• Do not change the names of the files.
Problem 1: Estimating the U.S. Population (10 points)
The webcomic xkcd ( https://xkcd.com/1047/ )gives the following formula to approximate the
United States population for a given year (from 2000 onward):
1. Obtain the last two digits of the year (for example, 2017 would give you 17). One way to
do this is to calculate the year modulo 100.
2. Subtract 10 from this value
3. Multiply the result by 3
4. Add 310 to the product from the previous step
This gives the approximate U.S. population in millions of people for the given year.
For example, consider the year 2006. The last two digits of 2006 are 06, or just 6. Subtracting 10
gives us -4. Multiplying -4 by 3 gives us -12. Finally, we add 310 to -12 to get our final answer:
298 (million people).
Examples:
After many years, you have developed a system for determining exactly how much to tip when
eating out at a restaurant:
• If the final bill is $25.00 or less, you will leave a tip of exactly $6.00 (regardless of the
quality of service)
• If the final bill was greater than $25.00 and the service was “good” (according to some
criteria that we won’t bother describing here), you will leave a tip of 27% of the final bill
• Otherwise (meaning that the final bill was over $25.00 but the service was “bad”), you
will leave a tip equal to 13% of the final bill.
The “hw1problem2.py” file contains a Python function named tip_amount() that can
help you decide how much to leave as a tip. It takes two arguments, in the following order: the
amount of the final bill (a positive floating-point number) called bill and a boolean value
categorizing the quality of the service called good_service (True indicates “good” service
and False indicates “bad” service). The function returns a floating-point number corresponding
to the amount you will leave as a tip.
For example, tip_amount(12.56, False) returns 6.0, representing $6 (even though the
service was bad, your system still requires you to leave a $6 tip). tip_amount(48.93,
True) returns 13.2111, representing a 27% tip (for this problem, don’t worry about rounding
the tip amounts to two decimal places).
Examples:
For example, the time value "02:18:49" (2 hours, 18 minutes, and 49 seconds) corresponds to a
total of 8329 seconds: there are 3600 seconds in an hour and 60 seconds in a minute, so we have
(2 * 3600) + (18 * 60) + 49, which adds up to 8329.
You may assume that the number of hours always falls in the range 00-99 (inclusive), the
number of minutes always falls in the range 00-59 (inclusive), and the number of seconds always
falls in the range 00-59 (inclusive). Take note, the split function separates out the numbers in the
time string, but it does not convert them into integers.
Examples:
There are two primary scales for measuring temperature, Fahrenheit and Celsius. I watch a lot of
European TV and I am always confused about how hot it is supposed to be in the story because
they use Celsius rather than Fahrenheit. I want you to build a program to help me (and my
European counterparts who face the opposite problem).
Examples:
In a vacuum light travels at approximately 186,000 miles per second, which seems fast until you
begin to reckon with just how far apart most objects in the universe are. If the speed of light
represents a hard limit on the rate at which we can travel, then it seems unlikely we are ever
going to travel any significant distance from the Earth. In fact, the fastest that a crewed space
vessel has ever traveled (Apollo 10) is less than 1% of the speed of light. I want you to complete
the implementation of a program that will help you to share the existential apathy I experience
whenever I consider these facts about the world.
The file "hw1problem5.py" contains a single function called how_long(). This function
takes three arguments called distance, fraction, and scale.
• distance is a floating-point value representing the distance in miles between the Earth
and some other object in the universe.
• fraction is a floating-point value representing the speed at which humans can travel
as a fraction of the speed of light.
• scale is a single character string that determines whether the result is in minutes ("M"),
hours ("H"), days ("D"), or years ("Y").
The function includes a variable called c that represents the speed of light measured in miles per
second. c is initialized to 186,000 and should not be changed. The function returns a float
representing the amount of time it would take to travel the specified distance given the speed
humans can travel as specified by fraction, in the units of time specified by scale.
Notice that if we could travel at 1% of the speed of light (which is at least twice as fast the fastest
a human-crewed space vessel has ever travelled), we could tool around the solar system pretty
comfortably—about 2 minutes to the moon, about 7 hours to Mars, about 56 days to the outer
edge of our solar system, but that's it. At 1% of light speed it would take us about 426 years to
reach the nearest star system at Alpha Centauri. We're at the bottom of a gravitational well, and,
unless something truly remarkable happens, we will be stuck here forever.