Implementing several simple programs using Python to get familiar with it.
- Taylor Series computation example.
- Bouncing ball.
- Monte Carlo Simulation example.
- Text document analyzer.
- Simple interactive data visualization example.
Compute the value of cosine(a), with a is an angle given in radians, using Taylor Series formula. Compare the result computed with the actual value computed by math.cos()
- Required modules:
- math.py
- matplotlib.py
- TaylorSeries.py
- Run the program by calling the following in python shell:
plotRealCosVersusComputed(low, high, number, taylorN)
where:- low: lower bound of angle to graph
- high: upper bound of angle to graph
- number: number of points to compute and graph (recommended more than 1000 to have a good curve)
- taylorN: the number of terms in the calculated Taylor Series.
For range -6 to 6:
Taylor Series with 6 terms | Taylor series with 10 terms |
---|---|
![]() |
![]() |
For range -10 to 10:
Taylor Series with 10 terms | Taylor series with 15 terms |
---|---|
![]() |
![]() |
Implement a simple interactive bouncing ball program, where the user can choose the start point and velocity (including direction) of the ball.
- Required modules:
- graphics.py
- BoucingBall.py
- Run the program by calling the following in python shell:
bounce(resolution)
A simulation based on random numbers to drive a computational model is called a Monte Carlo Simulation. The simulation program "plays" the computational model over and over again, each time assigning random values to the input variables. It then builds up a probabilistic characterization of the system that it is trying to model.
This specific program simulates the game of Craps, plots the probabilities of winning and losing.
Game of Craps rule: In one game of Craps, a player initially rolls a pair of normal, six-sided dice: - If this initial roll is 7 or 11, the player wins. - If this initial roll is 2, 3, or 12, the player loses. - With any other value for the initial roll, the player “rolls for point.” That is, the player repeatedly rolls the dice until either a 7 or the value of the initial roll appears. If the value of the initial roll appears before the 7, the player wins. However, if a 7 appears before the value of the initial roll, the player loses.
- Required modules:
- GameOfCrap.py: responsible for playing one game of Craps. Record the result and turns.
- StatsGenerator.py: keep track of the games' statistics.
- GraphSimResult.py: graph the simulation result.
- Run the program by typing the following in Python shell:
graphResult(#games)
where #games is the number of games to simulate (the more the better)
Simulation with 10000 games | Simulations with 100000 games |
---|---|
![]() |
![]() |
Implement a simple program that opens and reads one or more files containing normal English text, scans each one and accumulate a list of individual words appearing in that file, and then, when each input file has been scanned, writes to another file the list of unique words appearing in the entire set of input files — in alphabetical order — along with the number of occurrences of each word.
- Required modules:
- dataProcessor.py
- outputGenerator.py
- main.py
- Run the program straight from the command-line:
python main.py [file1] [file2] [...]
where: [fileN] is the name of the text file provided - Open the ouput text: search for output.txt file in directory.
- Enjoy!
Run with:
python main.py test.txt
Output:
Test.txt content | output.txt content |
---|---|
Implement a program that reads a set of public data (about real estate prices, in csv format) from the region around Sacramento, California. Plot the locations of properties of the real estate transactions on a map of the region.
- Required modules:
- graphics.py
- Map.py
- Filter.py
- Map.py
- Plot.py
- ReportGenerator.py
- Data files:
- SacramentoRealEstateTransactions.csv
- SacramentoMap.gif
- Run the program from command-line:
python main.py
- A window with the map of Sacramento Area will pop up. Click anywhere on the map to choose the center point.
- Click anywhere around the center point to specify the radius of data wanted.
- Check out report: search for report.txt
- Enjoy!
Data plotted on map | Sample report |
---|---|
These programs are implemented by Dung (Kevin) Nguyen, the owner of this repository, with the general instructions provided by Professor Lauer at Worcester Polytechnic Institute.