Skip to content

Instantly share code, notes, and snippets.

View jamiebullock's full-sized avatar

Jamie Bullock jamiebullock

View GitHub Profile
@jamiebullock
jamiebullock / counter_class.py
Created July 2, 2020 09:56
Counter class example
class Counter:
count = 0 # class attribute count
def increment(): # define method to increment count
count += 1
Counter.count # count is 0
Counter.increment() # call the method increment()
Counter.count # count is 1
counter = Counter() # create instance of Counter
counter.count # class attribute... count is still 1
@jamiebullock
jamiebullock / counter.py
Last active July 2, 2020 09:55
Counter example
class Counter:
def __init__(self): # define __init__ method passing self
self.count = 0 # set instance attribute count to 0
def increment(self): # define increment method passing self
self.count += 1 # add 1 to instance attribute count
c1 = Counter() # create Counter instance
c2 = Counter() # create another instance
c1.increment() # increment counter for c1
c1.count # count is 1
@jamiebullock
jamiebullock / fruit_classifier.py
Last active June 22, 2020 13:42
Simple Fruit Classifier
import pandas as pd
from sklearn.model_selection import train_test_split
data = pd.read_csv('fruit_data.csv')
types = pd.read_csv('fruit_types.csv', index_col=0)
y = data['Label']
X = data[['Mass', 'Width', 'Height']]
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)
Label Mass Width Height
1 192 8.4 7.3
1 180 8 6.8
1 176 7.4 7.2
2 86 6.2 4.7
2 84 6 4.6
2 80 5.8 4.3
2 80 5.9 4.3
2 76 5.8 4
3 178 7.1 7.8
Sound Average Frequency Average Loudness Duration Distance
A -0.53 -1.65 -0.65 0
C 0.05 -0.15 -0.96 1.64
B -0.62 0.60 0.17 2.40
E 1.91 -0.15 -0.41 2.87
D -0.81 1.35 1.85 3.91
Average Frequency Average Loudness Duration
-0.53 -1.65 -0.65
-0.62 0.60 0.17
0.05 -0.15 -0.96
-0.81 1.35 1.85
1.91 -0.15 -0.41
Average Frequency Average Loudness Duration
440 -6 3.4
352 -3 9.2
1053 -4 1.2
153 -2 20.9
3002 -4 5.1
@jamiebullock
jamiebullock / calculator3.py
Created February 16, 2020 12:58
Calculator using dictionary lookup and recursion
from operator import pow, truediv, mul, add, sub
operators = {
'+': add,
'-': sub,
'*': mul,
'/': truediv
}
def calculate(s):
@jamiebullock
jamiebullock / calculator1.py
Created February 15, 2020 18:37
Calculator with conditional branching
num1 = input("First Number:\n")
operator = input("Operator (+, -, *, /):\n")
num2 = input("Second Number:\n")
num1 = float(num1)
num2 = float(num2)
out = None
if operator == "+":
@jamiebullock
jamiebullock / abstraction.java
Created January 23, 2020 20:23
Separating levels of abstraction
public String render() throws Exception
{
HtmlTag hr = new HtmlTag("hr");
if (extraDashes > 0)
hr.addAttribute("size", hrSize(extraDahses));
return hr.html();
}
private String hrSize(int height)
{