Skip to content

Instantly share code, notes, and snippets.

View TomLin's full-sized avatar

Tom Lin TomLin

View GitHub Profile
@TomLin
TomLin / sort_list.py
Created March 22, 2022 05:47
LeetCode 148 Sort List (linked list)
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def sortList(self, head: Optional[ListNode]) -> Optional[ListNode]:
if not head or not head.next:
@TomLin
TomLin / merge_two_sorted_lists.py
Created March 6, 2022 13:07
LeetCode 21 Merge two sorted linked list.
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
"""
- 在操作linked list上頭,一定要把head保留住,每一次要提取資料,都只能從head,從新開始往下一個個節點找值。
- 這是linked list的特色,在寫leetcode的時候,一定要注意。
- 另一個常用的,就是用current node,來指涉現在要進行處理的node。
@TomLin
TomLin / merge_sort_array.py
Created March 6, 2022 12:52
This is an implementation of merge sort on array.
from typing import List
class Solution:
def mergeSort(self, nums: List[int]) -> List[int]:
return self.divide(nums)
def divide(self, nums):
if len(nums) <= 1:
@TomLin
TomLin / bubble_sort_recursion.py
Created October 15, 2021 10:38
adopt recursion in bubble sort.
# 嘗試用recursive的方式,來寫bubble sort
class BubbleSort:
def __init__(self, array):
self.arr = array
self.length = len(self.arr)
def recur_sort(self, left_round=None):
@TomLin
TomLin / bubble_sort.py
Created October 15, 2021 09:42
bubble sort algorithm.
class BubbleSort:
def __init__(self, array):
self.arr = array
self.length = len(self.arr)
def bubble_sort(self):
for i in range(self.length):
left_round = self.length - i
@TomLin
TomLin / build_linked_list.py
Created October 15, 2021 08:52
build a linked list using python.
class Node:
def __init__(self, val):
self.val = val
self.next = None # 這個next是一個pointer,用來指向後來放的Node
class MyLinked:
@TomLin
TomLin / build_array.py
Created October 4, 2021 08:26
Build an array using list and OOP.
class MyArray:
def __init__(self, size):
self.arr = [None] * size # 學習如其它正規語言,需要設定list的長度
self.i_end = -1 # idx, flag for indicating 值已經塞滿了list的空間
def expand_space(self):
# 當array的長度不夠時,直接創造一個兩倍長度的array,並且將值複製過去
arr_new = [None] * (len(self.arr) * 2)
for i in range(len(self.arr)):
@TomLin
TomLin / trx_transform.py
Created May 30, 2020 06:54
Data preprocess for transaction data, borrowed from moorissa.
def trx_transform(df):
# REF: https://github.com/moorissa/medium/blob/master/items-recommender/notebooks/recommendation-MT.ipynb
s = time.time()
data = pd.melt(frame=df.set_index("customerId")["products"].apply(
pd.Series).reset_index(),
id_vars=["customerId"],
value_name="products") \
.dropna().drop(["variable"], axis=1) \
.groupby(["customerId", "products"]) \
.agg({"products": "count"}) \
@TomLin
TomLin / get_ncf_model.py
Last active May 19, 2020 09:39
Construct NCF model.
def get_ncf_model():
# REF: https://github.com/khuangaf/tibame_recommender_system/blob/master/NCF.ipynb
user_inp = Input((1,))
user_hidden = Embedding(input_dim=n_users, output_dim=64)(user_inp)
user_hidden = Flatten()(user_hidden)
item_inp = Input((1,))
item_hidden = Embedding(input_dim=n_items, output_dim=64)(item_inp)
item_hidden = Flatten()(item_hidden)
@TomLin
TomLin / train_test_split.py
Last active May 16, 2020 07:37
Train/Test dataset split for recommender model training and testing.
def train_test_split(
ratings,
split_count,
fraction=None,
dir_path=None,
output_file_long=None,
user_col: str = None,
item_col: str = None,
item_name: str = None
):