This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 嘗試用recursive的方式,來寫bubble sort | |
class BubbleSort: | |
def __init__(self, array): | |
self.arr = array | |
self.length = len(self.arr) | |
def recur_sort(self, left_round=None): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Node: | |
def __init__(self, val): | |
self.val = val | |
self.next = None # 這個next是一個pointer,用來指向後來放的Node | |
class MyLinked: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"}) \ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
): |
NewerOlder