Skip to content

Instantly share code, notes, and snippets.

View wiccy46's full-sized avatar

Jiajun wiccy46

  • Holoplot
  • Berlin
View GitHub Profile
@wiccy46
wiccy46 / lcm.py
Created April 17, 2020 14:49
[lcm] Lowest Common Multiple #python #math
from math import gcd # Python versions 3.5 and above
#from fractions import gcd # Python versions below 3.5
from functools import reduce # Python version 3.x
def lcm(denominators):
return reduce(lambda a,b: a*b // gcd(a,b), denominators)
@wiccy46
wiccy46 / pickle.md
Created April 3, 2020 09:17
[pickle] what is pickle for #python #markdown
  1. saving a program's state data to disk so that it can carry on where it left off when restarted (persistence)

  2. sending python data over a TCP connection in a multi-core or distributed system (marshalling)

  3. storing python objects in a database

  4. converting an arbitrary python object to a string so that it can be used as a dictionary key (e.g. for caching & memoization).

@wiccy46
wiccy46 / binarytree_sum.py
Last active March 31, 2020 12:13
[binarytree_sum] Binary tree sum #python
def search_path(root):
if root is None:
return []
if root.left is None and root.right is None:
return [root.value]
return [root.value + val for val in search_path(root.right)
+ search_path(root.left)]
@wiccy46
wiccy46 / mlp.py
Created March 28, 2020 15:47
[MLP] Simple MLP example #python #tensorflow
model = Sequential()
model.add(Dense(256, input_shape=(40, )))
model.add(Acitivation('relu'))
model.add(Dropout(0.5))
model.add(Dense(256))
model.add(Activation('relu'))
model.add(Dropout(0.5))
@wiccy46
wiccy46 / batch_generator.py
Created March 26, 2020 10:18
[batch] Batch generator #python
def batch_generator(items, batch_size):
batch = []
i = 0
for item in items:
batch.append(item)
if len(batch) == batch_size:
yield batch
batch = []
yield batch
@wiccy46
wiccy46 / dockerrun.txt
Created March 24, 2020 09:10
[dockerrun] Docker data scicence #docker
$ docker run -it --rm --name ds -p 8888:8888 jupyter/datescience-notebook
@wiccy46
wiccy46 / project_root.py
Created March 20, 2020 11:50
[project_root] Get cwd #python
import os
project_root = os.path.dirname(os.path.abspath(__file__))
@wiccy46
wiccy46 / dsstoreremove.txt
Created February 21, 2020 16:22
[dsstoreRemove] Remove DS_Store from git, #git
Step 1: In order to remove existing files from the repository:
find . -name .DS_Store -print0 | xargs -0 git rm -f --ignore-unmatch
Step 2: Add this line '.DS_Store' to your .gitignore file, which can be found at the top level of your repository (or create if it isn't there already. Use the following command to do so:
echo .DS_Store >> .gitignore
Step 3: Then run the following command:
@wiccy46
wiccy46 / counter_defaultdict.py
Created February 10, 2020 14:42
[counter] Counting items with defaultdict #python
from collections import defaultdict
item_list = 'spam spam egg egg spam'.split()
count = defaultdict(int)
for item in item_list:
count[item] += 1
@wiccy46
wiccy46 / reverse_int.py
Created February 7, 2020 16:29
[reverse_number] Reverse a number #python
n = 1234
rev=0
while(n>0):
dig=n%10
rev=rev*10+dig
n=n//10