|
Department of Computer Science |
University of San Francisco |
Computer Science 110-03
Introduction to Computer Science I
Fall 2011
MWF 2:15-3:20, HR 235
Professor: Peter Pacheco
Office: Harney 540
Phone: 422-6630
Email: domain: cs.usfca.edu, user: peter
Office Hours: M 4-5, W and F 10-11, and by appointment
TA: Haley Smith
Email: domain: usfca.edu, user: hrsmith
Office Hour: R 1:30-2:30
TA: Simon Piel
Email: domain: dons.usfca.edu, user: spiel
Office Hour: W 3:30-4:30
Class mailing list:
The earlier instructions for joining
the list were incorrect. Please send your preferred
email address to the instructor, and he'll add you to the list.
Once you're a member of the list you can also post messages by sending
email to user cs110-03 in the domain cs.usfca.edu.
Course Syllabus (Here's a
PDF Version.)
Programming Assignments:
Lab Assignments:
- Assignment 1
- Part A (Nothing to turn in.)
- Part B (Add lab1b.py to your SVN repository.)
- Assignment 2.
- Part A: modify the final average
program so that it takes as input all the scores except the final
exam score. It also takes as input the desired final average.
From this information it computes the score needed on the final
exam to get the desired grade. This should be submitted to the
l2 directory of your SVN repository as exam_score.py.
Here's a solution.
- Part B: modify your solution to Part A so that it uses
a function to compute the weighted average of the student's
scores prior to the final exam. Use the value
computed by this function to find the grade needed on the
final exam in order to get the desired grade.
Here's a partial solution
Here's a solution.
- Assignment 3. All parts are due on Monday, September 19.
- Part A: Write a Python program that reads three
lines of input. The first and third lines are
numbers. The second line is an arithmetic operator
(+, -, *, /). After reading the three lines, and
converting the first and third to floats, the program should
call a function that carries out the indicated operation
and returns the result. When the call returns, the program
should print the result. For example, if the user entered
17
/
2
The program should print something like this
The result is 8.5
Here's a solution.
- Part B: Modify your solution to Part A so that
it accepts input (number, operation, number) and prints
the result until the user enters an 'x' for the operation.
Here's a solution.
- Part C: Modify the average program
so that it also finds and prints the variance
and standard deviation of the
user-input numbers. The variance can be calculated using the
formula:
sum_sq = val1**2 + val2**2 + . . . + valN**2
variance = (sum_sq - N*average**2)/(N-1)
The standard deviation is the square root of the variance.
Be sure to check whether N-1 is 0 before doing the division.
Here's a solution.
- Assignment 4. All parts are due on Friday, September 23.
- Part A. Fix the bug in the program that
prints a triangle of asterisks. Then modify the program
so that it prints the triangle ``upside down'': longest row
first, then next longest row, etc. Only turn in the second
program. Here's a solution
- Part B. Write a program that uses the TurtleWorld module
to draw a regular polygon with n sides. The user will enter
the number of sides and the length of the sides.
Here's a solution
- Assignment 5. All parts are due on Friday, September 30.
- Part A. Use the random walk
program as the basis for a program that draws a
collection of randomly located circles with random radii.
Let the user enter the delay between moves, the number
of circles and the maximum radius (which you can assume is
less than or equal to 50). Here's a
solution.
- Part B. This program
is an attempt to implement Newton's method. However,
it doesn't check on the number of passes through the
while loop. So it could enter an infinite loop. Modify
it so that it gets from the user the maximum allowable
number of passes, and checks this in the while
statement. Here's a solution.
- Part C. Write a program that sorts the characters in
a string into increasing alphabetic order. For example,
if the string is "peter", then the program should create
the new string "eeprt". You should assume that the only
characters in the string are lowercase letters.
Here's a solution.
- Assignment 6. All parts are due on Friday, October 14.
- Part A. Write a Python function del_char
that deletes all occurrences of a character from a string.
The parameters should be the character to delete and the
string. The function should return a new string that's
identical to the string parameter except the character is
deleted. For example if the character is 'e' and the
string is "peter", then the function should return
the string "ptr". Use your function in a program
that gets a string and a character from the user and
prints the string with the character deleted.
Here's a solution.
- Part B. Modify the sort_list function
in the selection sort program
so that it uses bubble sort.
Here's a solution.
- Assignment 7. All parts are due on Friday, October 21.
- Part A. Modify the insertion
sort program so that it sorts the input list in
decreasing order. Here's a
solution.
- Part B. Write a Python program that uses a recursive
function to compute Fibonacci numbers. Input will be
a nonnegative int n, output should be the nth Fibonacci
number. Here's a
solution
- Part C. Write a Python program that uses a recursive
function to reverse a string that's input by the user.
For example, if the user inputs "recursive", the program
should print "evisrucer". Your program should not use
any for or while statements. Here's a
solution.
- Assignment 8. All parts are due on Monday, October 31.
- Part A. Write a recursive version of the
regular polygon program.
When the program ends the turtle should return to the same
position and have the same direction that it had when
the program started (center of window facing right).
Your program should not use any for or while statements.
Here's a solution.
- Part B. Write a Python program that stores information
on a musician or musical group as a dictionary. The program
should prompt the user for the name of the musician or
group. It should also prompt the user for a list of the
musician's songs. The information on each song should include the
song's title, year, and length (in seconds), and this
information should also be stored as a dictionary. When the user
has finished entering information, the program should print
the information he or she entered.
- Part C. Modify your solution to Part B so that after
creating the database, it gives the user four options:
Add a new song to the musician's song dictionary,
delete a song from the musician's song dictionary,
print information on a song -- its length and year --,
and quit. Your program should prompt the user for
a single character -- 'a', 'd', 'p', 'q'-- and it should
continue prompting for and executing the user's commands
until he or she types 'q'.
Here's a solution to
parts B and C.
- Assignment 9. All parts are due on Friday, November 4.
- Part A. Add the remaining reverse lookups to the
movie database program
(movie_d_use.py,
movie_db.py, and
movie_db1.py).
Your program should find all movies made in a user-specified
year, all movies made by a user-specified director,
and all movies with a user-specified rating.
Here's a solution.
- Part B. Use pickle and Python files to modify
the musician database program
so that it has options to write the database
to a file and to read the database from a file.
(See the movie database program
for an example.) Here's a
solution.
- Assignment 10. All parts are due on Friday, November 11.
- Part A. Add a depth-first search function to the
program that reads and prints a
square matrix. Your depth-first search function should
print the number of each vertex when the vertex is
"visited."
Here's a solution.
- Part B. Write a Python program that will build a
list of the ``words'' in an HTML file in this
directory. Input to the program will
be the name of the html file. Output should be a list
of all strings that are not part of an HTML tag. For example, the
words in virions.html
are
virions, righties, mongos, ashy, trowing, manioca
(Note that the title, virions, should be included in the list.)
Here's a solution.
- Part C. Write a Python program that takes as input a
``root'' URL (e.g., http://cs.usfca.edu/~peter/cs110/dir) and
the name of one of the files in this
directory. It should open the file
and create a list of absolute URLs for all of the links
in the file. For example, if the
root URL is http://cs.usfca.edu/~peter/cs110/dir and the
input file is
ariette.html then it
should build the list
http://cs.usfca.edu/~peter/cs110/dir/sorcerer.html
http://cs.usfca.edu/~peter/cs110/dir/manioca.html
Note: your program doesn't need to access the internet,
just read the input, open the file, create the list, and
print the list.
Here's a solution.
- Assignment 11. All parts are due on Friday, December 2.
- Part A. Write a driver program for the
sort_by_length function
taken from Section 12.7 of the text. Your program should
read in a list of words, one word per line. End of input
will be indicated by an empty word (enter a carriage
return). It should then sort the list by word length,
and print the resulting list, one word per line.
Here's a solution.
- Part B. Add functions deal_hands
and print_hands to
the code in the cards program.
The first function should deal one card at a time to each
player until each player has two cards. The second function
should print out the cards in each hand.
Here's a solution.
- Assignment 12. All parts are due on Monday, December 5.
- Part A. Write a gen_world function that
can be used with the Life program. Its arguments are
the number of rows and columns in the world, and a
probability p (a float between 0 and 1). Use the
random function from the random
module to generate a random float for each cell in
the world. If the random float is less than p,
the cell should be alive. Otherwise it should be vacant.
Include a driver for the function that gets the number of
rows and columns and the probability p from
the user. It should print the world returned by
gen_world.
You can
use the life code that
we've written so far in class. For a solution see the
complete life program.
- Part B. Write a somethings_alive function
that can be used in the Life program. Its arguments are
the world and the number of rows and columns in the world.
It should return True if there's at least one living cell.
Otherwise it should return False. Include a driver for
the function: it should get the number of rows and
columns and the initial configuration from the user.
It should print the world and the result of
the call to somethings_alive. You can
use the life code that
we've written so far in class.
For a solution see the
complete life program.
Code Examples:
- Convert Celsius to Fahrenheit
- Find final average in CS 110
- Find an amount invested at simple
interest
- Find an amount invested at compound
interest
- Some programs that convert a single character Roman numeral
into a Hindu-Arabic numeral
- Running sum programs
- Print patterns of asterisks
- TurtleWorld Graphics
- Solving Equations
- This program attempts
to implement Newton's method, but it could have problems
with an infinite loop.
- Strings
- This program counts
the number of times a character appears in a string.
- This program finds
the character that appears most often in a string.
- More Roman Numerals
- A buggy program for
converting a Roman Numeral to a Hindu-Arabic number.
- A program for
converting a Roman Numeral to a Hindu-Arabic number.
This version fixes the bug in the previous version.
- A program for
converting a Roman Numeral to a Hindu-Arabic number.
This version handles the final character outside the
loop.
- A program for
converting a Roman Numeral to a Hindu-Arabic number.
This version also converts the first character before
the loop.
- A program for
converting a Roman Numeral to a Hindu-Arabic number.
This version adjusts the subscripts.
- A program for
converting a Roman Numeral to a Hindu-Arabic number.
This version handles erroneous input characters.
- Selection sort
- A buggy program
that tries to sort a list of ints.
- A buggy program
that tries to sort a list of ints. This version
prints the list as it's changed by the program.
- A buggy program
that tries to sort a list of ints. This version
attempts to swap two elements in the list.
- A program
that sorts a list of ints.
- A program
that sorts a list of ints. This version eliminates
an unnecessary pass through the main for
loop.
- Insertion sort
- A buggy program
that tries to sort a list of ints using insertion sort.
- A program
that sorts a list of ints using insertion sort.
- A program that
looks at what happens when a function changes its
parameters.
- Recursion
- A buggy program
that attempts to use recursion to print hello repeatedly.
- A program
that uses recursion to print hello repeatedly.
- A buggy program
that attempts to use recursion to add the first n positive
ints.
- A program
that uses recursion to add the first n positive
ints.
- A program
that uses recursion to add the first n positive
ints. This version prints out where the program is
in the sequence of recursive calls.
- A program
that uses binary search to search a sorted list.
This version has a bug.
- A program
that uses binary search to search a sorted list.
- A program
that compares the performance of binary and linear
search.
- A program
that draws a Koch curve.
- A program
that draws a Koch curve that will extend across
the window.
- Dictionaries
- A program
that stores information on a movie as a list.
- A program
that stores information on a movie as a dictionary.
- A program
that stores information on a movie as a dictionary.
This version has create_movie_d function
that takes the title as a parameter.
- A program that
allows the user to create a database of movies.
- A program that
allows the user to create a database of movies,
modify the database, and get some information
from the database.
- A program that
allows the user to create a database of movies,
modify the database, and get some information
from the database. This version reads and writes
the database using basic file operations.
- A program that
allows the user to create a database of movies,
modify the database, and get some information
from the database. This version reads and writes
the database using the pickle module.
- A program that
compares the performance of binary and linear
searching and using a dictionary. This version
prints the list, the dictionary, the keys to
be searched, and the results of each search.
- A program that
compares the performance of binary and linear
searching and using a dictionary. This version
only prints run-times.
- Life
Other Information:
Peter Pacheco
2011-12-07