First commit

This commit is contained in:
2019-01-28 10:15:59 -05:00
parent 339697124e
commit 4557ad07db
5 changed files with 144 additions and 42 deletions

4
.gitignore vendored Normal file
View File

@@ -0,0 +1,4 @@
#Ignore anything that Visual Studio Code produces
.vscode/*
#Ignore anything that python caches
__pycache__/*

63
Algorithms.py Normal file
View File

@@ -0,0 +1,63 @@
#Python/myClasses/Algorithms.py
#Matthew Ellison
# Created: 1-27-19
#Modified: 1-27-19
#This is a file that contains a few algorithms that I have used several times
import math
#This function returns a list with all the prime numbers <= goalNumber
def getPrimes(goalNumber: int):
primes = []
foundFactor = False
#If the number is 0 or negative return an empty list
if(goalNumber <= 1):
return primes
#If the number is even 2 is a factor and all other factors will be odd
if((goalNumber %2) == 0):
primes.append(2)
#We can now start at 3 and skip all even numbers, because they cannot be prime
for possiblePrime in range(3, goalNumber + 1, 2): #Need goalNumber + 1 to account for goalNumber being prime
#See if the current possible prime is divisible by any prime less than it. If not it is a prime itself
for primeNum in primes:
if((possiblePrime % primeNum) == 0):
foundFactor = True
break
#If you didn't find a factor then the current number must be prime
if(not foundFactor):
primes.append(possiblePrime)
else:
foundFactor = False
primes.sort()
return primes
#This is a function that returns all the factors of goalNumber
def getFactors(goalNumber: int):
#You need to get all the primes up to this number
primes = getPrimes(math.ceil(math.sqrt(goalNumber))) #If there is a prime it must be <= sqrt(num)
factors = []
#If you didn't get any primes the number itself must be a prime
factors.append(goalNumber)
#You need to step through each prime and see if it is a factor in the number
cnt = 0
while((cnt < len(primes)) and (goalNumber > 1)):
#If the prime is a factor you need to add it to the factor list
if((goalNumber % primes[cnt]) == 0):
factors.append(primes[cnt])
goalNumber /= primes[cnt]
#Otherwise advance the location in primes you are looking at
#By not advancing if the prime is a factor you allow for multiple of the same prime number as a factor
else:
cnt += 1
#If for some reason the goalNumber is not 0 print an error message
if(goalNumber > 1):
print("There was an error in getFactors(). A leftover of " + str(goalNumber))
#Return the list of factors
return factors

View File

@@ -1,45 +1,9 @@
**Edit a file, create a new file, and clone from Bitbucket in under 2 minutes** #pyClasses
This is a collection of classes and functions that I have found I use often enough that it is helpful to keep a record of them.
When you're done, you can delete the content in this README and update the file with details for others getting started with your repository. #Algorithms
This is a collection of functions that is not large enough to justify a class, but too helpful to not keep track of.
*We recommend that you open this README in another tab as you perform the tasks below. You can [watch our video](https://youtu.be/0ocf7u76WSo) for a full demo of all the steps in this tutorial. Open the video in a new tab to avoid leaving Bitbucket.*
--- #Stopwatch
This class is used for timing portions of your code.
## Edit a file
Youll start by editing this README file to learn how to edit a file in Bitbucket.
1. Click **Source** on the left side.
2. Click the README.md link from the list of files.
3. Click the **Edit** button.
4. Delete the following text: *Delete this line to make a change to the README from Bitbucket.*
5. After making your change, click **Commit** and then **Commit** again in the dialog. The commit page will open and youll see the change you just made.
6. Go back to the **Source** page.
---
## Create a file
Next, youll add a new file to this repository.
1. Click the **New file** button at the top of the **Source** page.
2. Give the file a filename of **contributors.txt**.
3. Enter your name in the empty file space.
4. Click **Commit** and then **Commit** again in the dialog.
5. Go back to the **Source** page.
Before you move on, go ahead and explore the repository. You've already seen the **Source** page, but check out the **Commits**, **Branches**, and **Settings** pages.
---
## Clone a repository
Use these steps to clone from SourceTree, our client for using the repository command-line free. Cloning allows you to work on your files locally. If you don't yet have SourceTree, [download and install first](https://www.sourcetreeapp.com/). If you prefer to clone from the command line, see [Clone a repository](https://confluence.atlassian.com/x/4whODQ).
1. Youll see the clone button under the **Source** heading. Click that button.
2. Now click **Check out in SourceTree**. You may need to create a SourceTree account or log in.
3. When you see the **Clone New** dialog in SourceTree, update the destination path and name if youd like to and then click **Clone**.
4. Open the directory you just created to see your repositorys files.
Now that you're more familiar with your Bitbucket repository, go ahead and add a new file locally. You can [push your change back to Bitbucket with SourceTree](https://confluence.atlassian.com/x/iqyBMg), or you can [add, commit,](https://confluence.atlassian.com/x/8QhODQ) and [push from the command line](https://confluence.atlassian.com/x/NQ0zDQ).

71
Stopwatch.py Normal file
View File

@@ -0,0 +1,71 @@
#Python/myClasses/Stopwatch.py
#Matthew Ellison
# Created: 1-27-19
#Modified: 1-27-19
#This is a class that is used to time program run times
import time
class Stopwatch:
#Initialize the class and all the variables you will need
def __init__(self):
self.timeStarted = 0.0 #This variable holds the time that the start function was called
self.timeStopped = 0.0 #This variable holds the time that the stop function was called
self.started = False #This variable holds true after the start function has been called
self.finished = False #This variable holds true after the start and stop functions have been called
#Save the time at this point. Simulates starting a stopwatch
def start(self):
self.started = True
self.finished = False
self.timeStarted = time.perf_counter_ns()
#Save the time at this point. Simulates stopping a stopwatch
def stop(self):
#If the stopwatch has been started then you record the stopping time
if(self.started):
self.timeStopped = time.perf_counter_ns()
self.finished = True
#Otherwise just ignore the function call
#Returns the difference between two times, usually start and stop times, but can be start time and now
#Simulates looking at the stopwatch; it can still be running when you look at it
def getTime(self):
#If the start and stop function have been called then calculate the time between the calls
if(self.finished):
return (self.timeStopped - self.timeStarted)
#If start was called but not stop calculate the time between start and now. Simulates looking at the watch while still running
elif(self.started):
return (time.perf_counter_ns() - self.timeStarted)
#If start was never called return a negative number. In this context time can never be negative
else:
return -1
#Return a specific resolution of time. This is done as a floating point number
#Returns the result of getTime() in terms of seconds
def getSeconds(self):
second = self.getTime()
if(second < 0):
return second
else:
return (second / 1000000000)
#Returns the result of getTime() in terms of milliseconds
def getMilliseconds(self):
milli = self.getTime()
if(milli < 0):
return milli
else:
return (milli / 1000000)
#Returns the result of getTime() in terms of microseconds
def getMicroseconds(self):
micro = self.getTime()
if(micro < 0):
return micro
else:
return (micro / 1000)
#Returns the result of getTime() in terms of nanoseconds
def getNanoseconds(self):
return self.getTime()

0
__init__.py Normal file
View File