1. Achilles and the Turtle (10 Points)
Write a program that simulates a race between Achilles and the turtle. Here is an example run (the
user input is underlined):
Where does Achilles start? 0
How many feet is the turtle ahead? 100
How many feet does Achilles run per second? 25
How many feet does the turtle crawl per second? 1
Time = 0 secs: Achilles at 0 , turtle at 100
Time = 1 secs: Achilles at 25 , turtle at 101
Time = 2 secs: Achilles at 50 , turtle at 102
Time = 3 secs: Achilles at 75 , turtle at 103
Time = 4 secs: Achilles at 100 , turtle at 104
Race over! Time = 5 secs: Achilles at 125 , turtle at 105
Hints: First, the user inputs the start position of Achilles and the turtle (in the example above, the turtle is 100 feet ahead), and the number of feet per second for Achilles (here: 25) and the turtle (here: 1). Then a loop is used that advances (simulated) time by 1 second per iteration. The loop continues as long as the turtle is still ahead and terminates once Achilles has passed the turtle (i.e.,you need to formulate an appropriate condition). Also, (1) use appropriate statements to make sure that the user enters only non-negative numbers and (2) do not forget to comment your program!

2. List Processing and Strings (12 Points) Write a program that asks the user to enter a sentence,which is just a string. The program then calls a user-defined function that counts the non-whitespace characters in the sentence and returns that number to the calling statement in the main program. The
main part of your program looks exactly like this:
in_line = raw_input("Please enter a sentence: ")
chars = count_chars(in_line)
print "The sentence has", chars, "non-whitespace characters."
count chars is a user-defined function that gets a string as parameter. On the assignments Web page, you will find an example program that shows how a user defined function is programmed and called from the main program. You simply have to adjust this program template to have the function count chars return a number that is then assigned the variable chars in the main program above.
Here is an example run of the program (input and output are underlined):
Please enter a sentence: I start my homework early
The sentence has 21 non-whitespace characters.
! Continue reading on the next page!!
• In the user-defined function count chars: In order to obtain a list of individual words from
the input string (i.e., the string passed on as parameter to the function), you have to use the
function split(). Recall that this function returns a list of strings (see also textbook page 92 for the function split() and Section 4.4.4). Then use a for-loop to iterate over the words of that list,counting for each word the characters using an accumulator variable (you know how to get the number of characters in a string. . . ). The value of that variable is then returned to the calling statement in the main program.
• This function split() is available once you import the library string. An example program that uses this function is given on the assignments Web page.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.