Skip to content

Instantly share code, notes, and snippets.

View GaryRevell's full-sized avatar
🙂

Gary Revell GaryRevell

🙂
View GitHub Profile
@GaryRevell
GaryRevell / spiral.py
Created April 6, 2018 20:25
Spiral class
from math import floor,sqrt
class Spiral():
"""
This class creates a spiral starting from 1..elements
"""
#
# Define up/down & left/right step functions
#
def move_right(self,x, y):
#
# Advent problem from day one - http://adventofcode.com/2017/day/1
#
def captcha(myStr):
return sum([int(myStr[c]) for c in range(-1,len(myStr)-1) if myStr[c]==myStr[c+1]])
myStrs = {'1122':3,'1111':4,'1234':0,'91212129':9}
if __name__ == '__main__':
for m in myStrs:
@GaryRevell
GaryRevell / varargs.py
Last active March 28, 2018 16:13
Simple demo of the use of the * and ** function parameters
#
# Toy function that has 1 required arg, then allows a simple open-ended list of args (tuple)
# and finally handles key=val list arguments stored as a dict
#
def varargs(req, *args, **kwargs):
print("req=",req)
if args:
print("args=",args)
if kwargs:
print("kwargs=",kwargs)
@GaryRevell
GaryRevell / fruit_classifier.py
Created March 28, 2018 13:44
Simple machine learning classifier using a decision tree, is it an apple or an orange?
#
# Very simple "Hello World" type machine learning intro program that uses a decision tree classifier
#
from sklearn import tree
#
print("Features = [Weight, skin-type] e.g.[140 gms, 'smooth' = 1] , [180 gms, 'bumpy' = 0 ]")
features = [[140,1],[130,1],[150,1],[170,0],[190,0]]
print("Raw data = ",features)
#
print("For each label => apple (0) , orange (1)")
@GaryRevell
GaryRevell / anagram.py
Created March 28, 2018 13:34
Print out the anagrams of the word supplied on the console
from itertools import *
def show(iterable):
first = None
for i, item in enumerate(iterable, 1):
if first != item[0]:
if first is not None:
print()
first = item[0]
print(''.join(item), end=' ')
@GaryRevell
GaryRevell / numberGuess.py
Created March 28, 2018 13:15
Number guessing game used for school education purposes
'''
The Number Guess Game
Guess the random number the computer has chosen.
After each try, if unsuccessful, the program will help by narrowing the bounds to guess in.
This helps to teach binary searches, where you chop midway between the ranges each time to
minimize the number of guesses required
Type 'q' to quit the program
'''
@GaryRevell
GaryRevell / nilakanth.py
Created March 28, 2018 13:12
Calculate pi using Nilakanth series
'''
Calculate pi using Nilakantha series
http://www.codeproject.com/Articles/813185/Calculating-the-Number-PI-Through-Infinite-Sequenc
π = 3 + (4/(2*3*4)) - (4/(4*5*6)) + (4/(6*7*8)) - (4/(8*9*10)) ...
Or another series
http://www-history.mcs.st-andrews.ac.uk/Biographies/Nilakantha.html