Skip to content

Instantly share code, notes, and snippets.

View JohnnyFang's full-sized avatar
👨‍💻
working on improvin' my skills!

Johnny Fang JohnnyFang

👨‍💻
working on improvin' my skills!
View GitHub Profile
@JohnnyFang
JohnnyFang / linkedlist.py
Created April 23, 2018 22:47
Python's Linked List implementation using recursion
class Node:
def __init__(self,data):
self.data = data
self.next = None
class Solution:
def display(self,head):
current = head
while current:
@JohnnyFang
JohnnyFang / command.py
Created October 9, 2018 02:40
Command Pattern example
class Command:
def execute(self):
pass
class Copy(Command):
def execute(self):
print("Copying...")
@JohnnyFang
JohnnyFang / interpreter.py
Created October 9, 2018 03:16
Interpreter pattern example
# Python 3 has a built in feature that allows us to designate a certain method as an abstract method.
# When a method is decorated as an abstract method it should be overrriden by a subclass.
from abc import ABC, abstractmethod
class AbstractExpression():
@abstractmethod
def interpret(self):
pass
class NonterminalExpression(AbstractExpression):
@JohnnyFang
JohnnyFang / memento.py
Created October 9, 2018 03:54
memento pattern example
import pickle
class Originator:
def __init__(self):
self.__state = None
def create_memento(self):
return pickle.dumps(vars(self))
def set_memento(slef, memento):
selfprevious_state = pickle.loads(memento)
@JohnnyFang
JohnnyFang / random thoughts
Created November 2, 2018 02:49
compare each item in a list with the rest
# compare each item in a list with the rest, only once?
for index, this in enumerate(mylist):
for that in mylist[index+1:]:
compare(this, that)
@JohnnyFang
JohnnyFang / arrayChange.py
Created November 5, 2018 16:32
array of integers. On each move you are allowed to increase exactly one of its element by one. Find the minimal number of moves required to obtain a strictly increasing sequence from the input.
def arrayChange(inputArray):
count = 0
adam = inputArray[0]
for el in inputArray[1:]:
if el <= adam:
count += adam-el+1
adam +=1
else:
adam = el
return coutn
@JohnnyFang
JohnnyFang / palindromeRearranging.py
Created November 5, 2018 16:51
Given a string, find out if its characters can be rearranged to form a palindrome.
# not so Pythonic way
def palindromeRearranging(inputString):
letter_counts_dict = dict()
for letter in inputString:
if letter in letter_counts_dict:
letter_counts_dict[letter] += 1
else:
letter_counts_dict[letter] = 1
threshold = 0
for k,v in letter_counts_dict.items():
@JohnnyFang
JohnnyFang / config.py
Created December 28, 2018 23:49
Flask configurations options - development and production.
class Config(object):
"""
Common configurations
"""
class DevelopmentConfig(Config):
"""
Development configurations
"""
@JohnnyFang
JohnnyFang / hello.py
Created December 28, 2018 23:53
flask sample hello
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello World!"
if __name__ == '__main__':
app.debug = True
app.run()
@JohnnyFang
JohnnyFang / config.py
Created December 29, 2018 00:01
flask instance config file - contains configuration variables for our app and database.
# instance/config.py
SECRET_KEY = 'b=3=!0^96*8jhkvkfbqf*1!yn-9'
SQLALCHEMY_DATABASE_URI = 'postgresql+psycopg2://USER:PASSWORD@localhost/db_name'