Skip to content

Instantly share code, notes, and snippets.

View PttCodingMan's full-sized avatar
🏠
正在支援下一代 PTT API

CodingMan PttCodingMan

🏠
正在支援下一代 PTT API
View GitHub Profile
@PttCodingMan
PttCodingMan / reversed_linked_list.py
Last active June 13, 2020 05:20
Reveres singly-linked list in Python 3
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
def get_reversed_linked_list(self, root):
current_node = root
next_node = root.next
pre_node = None
@PttCodingMan
PttCodingMan / get_aid_from_url.py
Last active November 5, 2020 17:50
PTT 文章網址轉換成看板與aid
def get_aid_from_url(url: str) -> (str, str):
# from get_aid_from_url in PyPtt
# 檢查是否符合 PTT BBS 文章網址格式
pattern = re.compile('https://www.ptt.cc/bbs/[-.\w]+/M.[\d]+.A[.\w]*.html')
r = pattern.search(url)
if r is None:
raise ValueError('url must be www.ptt.cc article url')
def quick_sort(data: list[int]) -> list[int]:
if (length := len(data)) <= 1:
return data
pivot = data.pop(length // 2)
smaller_list = [i for i in data if i < pivot]
bigger_list = [i for i in data if i >= pivot]
return quick_sort(smaller_list) + [pivot] + quick_sort(bigger_list)
@PttCodingMan
PttCodingMan / walk.py
Created November 10, 2020 03:40
List all files in the folder in Python 3, recursion is an option
import os
if __name__ == '__main__':
def walk_dir(current_path, recursive=True):
result = []
for (parent_path, folder_names, file_names) in os.walk(current_path):
# I don't like -> \ <-
parent_path = parent_path.replace('\\', '/')
if not recursive:
@PttCodingMan
PttCodingMan / run_cmd.py
Last active March 29, 2021 07:02
Run command in Python 3 and get the execute result
import subprocess
import locale
def run_cmd(cmd, use_cmd=False):
if use_cmd:
# windows
cmd = ['cmd', '/c'] + cmd
# Linux
cmd = ['/bin/sh', '-c'] + cmd
result = subprocess.run(cmd, stdout=subprocess.PIPE)
@PttCodingMan
PttCodingMan / no_bug.py
Created April 30, 2021 13:09
Bless the code is no bugs
# ____________
# | |
# | |
# | |
# | |
# | |
# | |
# | |
# _____________________| |_____________________
# | |
@PttCodingMan
PttCodingMan / init.sh
Last active June 21, 2021 06:05
init Minecraft server in debian or ubuntu
# set your java installer here, must with tar.gz
java_installer=https://cdn.azul.com/zulu/bin/zulu16.30.15-ca-jre16.0.1-linux_x64.tar.gz
installer_name_full="$(basename -- $java_installer)"
installer_name="${installer_name_full%.*}"
installer_name="${installer_name%.*}"
sudo apt-get update && sudo apt-get -y upgrade
sudo apt-get -y install git
def permutations(data, start_index, end_index):
if start_index == end_index:
print(data)
return
for i in range(start_index, end_index):
data[i], data[start_index] = data[start_index], data[i]
permutations(data, start_index + 1, end_index)
data[i], data[start_index] = data[start_index], data[i]
class Solution:
def threeSum(self, nums: List[int]) -> List[List[int]]:
if (length := len(nums)) < 3:
return []
result_map = {}
result = []
targets = []
for i in range(length):