Skip to content

Instantly share code, notes, and snippets.

@timcsy
timcsy / long_list.py
Created November 19, 2024 00:27
很長的 List
long_list = [
703, 513, 444, 201, 562, 992, 214, 895, 847, 651,
134, 978, 238, 119, 820, 449, 94, 51, 374, 670,
923, 158, 614, 771, 877, 82, 346, 687, 304, 530,
"apple", "banana", "cherry", "date", "elderberry",
"fig", "grape", "honeydew", "kiwi", "lemon",
"mango", "nectarine", "orange", "papaya", "quince",
"raspberry", "strawberry", "tangerine", "ugli fruit", "watermelon",
1831, 4827, 9021, 5209, 6554,
"dog", "cat", "bird", "fish", "hamster",
@timcsy
timcsy / grade_hint.py
Created November 15, 2024 03:43
grade.py 練習提示
# 定義一個計算成績等第的 function
def show_grade(student_name, student_score):
# 完成 show_grade 函數
# 使用 function 處理每位學生
students = [
{"name": "Alice", "score": 85},
{"name": "Bob", "score": 72},
{"name": "Charlie", "score": 67},
{"name": "Diana", "score": 93}
@timcsy
timcsy / grade.py
Last active November 15, 2024 03:14
練習:把以下程式用函式重寫
# 學生成績
student1_name = "Alice"
student1_score = 85
if student1_score >= 90:
student1_grade = "A"
elif student1_score >= 80:
student1_grade = "B"
elif student1_score >= 70:
student1_grade = "C"
elif student1_score >= 60:
@timcsy
timcsy / imgur.py
Last active May 7, 2024 09:09
備份 HackMD 上面之前上傳到 imgur 的圖片。請到 HackMD 的設定頁面把所有的筆記下載下來,放在 Notes 資料夾裡面,然後把這個 imgur.py 檔放在外面,執行 python imgur.py 之後就可以備份囉!備份後的圖將存到 imgur 資料夾
import os
from glob import glob
import re
import urllib.request
paths = glob('Notes/*.md')
results = []
for path in paths:
with open(path, 'r', encoding='UTF-8') as fin:
data = fin.read()
@timcsy
timcsy / newton.cpp
Last active March 20, 2021 13:31
牛頓法範例
// 比較可以重複利用牛頓法的寫法(使用 function pointer)
#include <iostream>
using namespace std;
typedef double (*Equation)(double x); // 把 function pointer 命名為 Equation
double diff(Equation f, double x, double h=1e-6) {
return (f(x + h) - f(x)) / h;
}
@timcsy
timcsy / list.h
Created January 16, 2018 02:41 — forked from ctlaltlaltc/list.h
Simple doubly linked list implementation, modify from linux kernel list.h, so we can use it in application.
#ifndef _LIST_H
#define _LIST_H
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
#define container_of(ptr, type, member) ({ \
const typeof( ((type *)0)->member ) *__mptr = (ptr); \
(type *)( (char *)__mptr - offsetof(type,member) );})
struct list_head {
struct list_head *next, *prev;