Skip to content

Instantly share code, notes, and snippets.

View yanfeng42's full-sized avatar
💌
I may be slow to respond.

Macro yanfeng42

💌
I may be slow to respond.
View GitHub Profile
@yanfeng42
yanfeng42 / QueueFromSevenStack.java
Created January 5, 2024 06:35
1.3.49栈与队列。用有限个栈实现一个队列,保证每个队列操作(在最坏的情况下)都只需要常数次的栈操作。警告:非常难!
package study.algorithm;
import edu.princeton.cs.algs4.StdOut;
public class QueueFromSevenStack<Item> {
private boolean isRevert = false; // 是否在反转 Stack.
private boolean isConcat = false; // 是否在拼接 Stack.
private Stack<Item> T = new Stack<>(); // 队列尾部,用于 enqueue 入队操作.
private Stack<Item> H = new Stack<>(); // 队列头部, 用于 dequeue 出队操作.
@yanfeng42
yanfeng42 / 2.22.scm
Created March 31, 2022 05:53
sicp2.22 An iterative version of the answer that is easier to understand
; My answer.
#|
keypoints:
* cache tail node.
* call set-cdr!
ref:
* tips: cons build pairs, not lists. cons build pairs, not lists. ref: https://stackoverflow.com/a/5741668
* https://medium.com/@aleksandrasays/my-other-car-is-a-cdr-3058e6743c15
@yanfeng42
yanfeng42 / batch_convert.py
Created March 16, 2022 04:42
Batch Convert XCode String files, from utf-16 to utf-8
import codecs
import os
import shutil
import glob
def convert(file_path: str, from_encoding: str, to_encoding: str):
utf16_path = file_path
utf8_tmp_path = utf16_path + ".tmp"
try:
@yanfeng42
yanfeng42 / shell_code_for_bot_pomodoro.sh
Created January 11, 2022 01:58
shell code for bot pomodoro
# step3.1
# check timedatectl
timedatectl
# change server timezone
sudo timedatectl set-timezone $timezone_you_need
# step3.2
@yanfeng42
yanfeng42 / bot_pomodoro.py
Created January 11, 2022 01:13
python telegram pomodoro robot
@task(alias='bot_p')
def bot_pomodoro():
token = $your_robot_token
chat_id = $robot_chat_id_with_you
now = datetime.datetime.now()
minute = now.minute
tip = 'unknown error'
if minute == 0:
@yanfeng42
yanfeng42 / InfixToPrefix.java
Created December 3, 2021 06:08
algs1.3.10 简单衍生 中缀转前缀
package life.yanfeng.study.algorithm;
import edu.princeton.cs.algs4.StdOut;
// 1.3.10 衍生 中缀转前缀
public class InfixToPrefix {
/// 自动补全 左括号 . 反向推导表达式的原始形式.
/// 核心思路, 也即: 双栈表达式的精髓在于: 使用同一个栈, 同时存储 操作数 和 中间运算结果.
static String transform(String exp) {
Stack<String> ops = new Stack<>();
@yanfeng42
yanfeng42 / InfixToPostfix.java
Created December 3, 2021 06:07
algs 1.3.10 中缀转后缀
package life.yanfeng.study.algorithm;
import edu.princeton.cs.algs4.StdOut;
/// for 1.3.10 中缀 转 后缀. 和 1.3.9 类似的思路
public class InfixToPostfix {
/// 自动补全 左括号 . 反向推导表达式的原始形式.
/// 核心思路, 也即: 双栈表达式的精髓在于: 使用同一个栈, 同时存储 操作数 和 中间运算结果.
static String transform(String exp) {
Stack<String> ops = new Stack<>();
@yanfeng42
yanfeng42 / AutoCompleteParentheses.java
Last active December 3, 2021 05:50
algs4 1.3.9 一道直击 双栈算法 精髓的练习题
package life.yanfeng.study.algorithm;
import edu.princeton.cs.algs4.StdOut;
// for 1.3.9
public class AutoCompleteParentheses {
/// from 1.3.1.7 Dijkstra 的双栈表达式求值算法
static double evaluate(String exp) {
Stack<String> ops = new Stack<>();
Stack<Double> vals = new Stack<>();
@yanfeng42
yanfeng42 / StackPrintCase.java
Last active November 11, 2021 01:34
algs4 1.3.5 简易的 数字进制转换 算法
package life.yanfeng.study.algorithm;
import edu.princeton.cs.algs4.StdOut;
// for 1.3.5
public class StackPrintCase {
public static void printCase(Integer N, int base) {
Stack<Integer> stack = new Stack<>();
while (N > 0) {
stack.push(N % base);
@yanfeng42
yanfeng42 / Parentheses.java
Created November 8, 2021 06:34
algs4 1.3.4 基于泛型的任意 "括号" 匹配校验算法
package life.yanfeng.study.algorithm;
import edu.princeton.cs.algs4.StdOut;
// for 1.3.4
public class Parentheses<Item> {
private Bracket<Item>[] brackets;
Parentheses(Bracket<Item>[] brackets) {
this.brackets = brackets;