Processing math: 0%

CPSC120A
Fundamentals of Computer Science I

Lab 8

Modules

Practice Problem #1

Write a Python program that will print the result of rolling 12 six sided dice to the terminal window.

Example Output

$ python3 practice1.py
2
1
4
3
2
6
5
4
1
2
1
1
Practice Problem #2

Write a Python program that can compute the number of pairs that can be created from our CPSC 120 class. You should use the input function to determine how many students are in your class.

The number of combinations of size k that can be created from a set of size n can be computed using the binomial equation:

\binom nk = \frac{n!}{k! \cdot (n - k)!}

Example

$ python3 practice2.py
How many students are in your class? 20
There are 190 pairs.
Sine Function

Hopefully you are familiar with the sine and cosine functions from back in your trigonometry class. As you can imagine, these are incredibly useful functions not only for mathematics, but for drawing in general.

Details

In Emacs, create a Python program in a file called sine.py that uses the turtle module to draw a function plot of the math.sin function. The program should prompt the user for the amplitude of the sin function, in turtle window units.

Test your program by running it multiple times with different input values. Make sure your code follows the courses code conventions.

Example

$ python3 sine.py
Enter the amplitude: 100
Turtle Graphics Sine Plot

Hint

Challenge

The sine and cosine functions are interesting, but I think the tangent function is a much more visually appealing function. Change the color of the turtle's pen, and also plot the tangent function on top of the sine function.

Screen Saver

In old CRT monitors if the same image was displayed for too long, a ghost of the image would get 'burned' into the screen. To prevent this from occuring a screensaver would run whenever the computer was inactive for a period. Modern LCD monitors no longer need screen savers, but they can still be fun to watch.

Details

In Emacs, create a Python program in a file called screen_saver.py that uses the turtle module to draw many dots on the screen. The dots should be in random locations, centers located anywhere in the window, have random sizes, in the range [100, 300], and should be random colors.

Make sure your code follows the courses code conventions.

Example

$ python3 screen_saver.py
Turtle Graphics Sine Plot

Hint

Challenge

The random dots screen saver is good, but is pretty silly. Instead, you already have a program written that can draw polygons with an arbitrary number of sides. Write a new program called screen_saver_poly.py, which generates a large number of polygons with a random number of sides in the range [3, 20]. These shapes should also be filled in a random color.

Submission

Please show your source code and run your programs for the instructor or lab assistant. Only a programs that have perfect style and flawless functionality will be accepted as complete.