This file contains hidden or 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
package life.yanfeng.study.algorithm; | |
import edu.princeton.cs.algs4.StdOut; | |
import java.util.Arrays; | |
enum StackActionEnum { | |
pop, push | |
} |
This file contains hidden or 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
package life.yanfeng.study.algorithm; | |
import edu.princeton.cs.algs4.StdOut; | |
// for 1.2.16 | |
public class Rational { | |
// 分子. | |
private long numerator; | |
// 计算最大公约数. ref: https://zh.wikipedia.org/wiki/%E8%BC%BE%E8%BD%89%E7%9B%B8%E9%99%A4%E6%B3%95 |
This file contains hidden or 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
package life.yanfeng.study.algorithm; | |
import edu.princeton.cs.algs4.StdOut; | |
public class SmartDate { | |
private final int month; | |
private final int day; | |
private final int year; | |
public SmartDate(int m, int d, int y) throws RuntimeException | |
{ |
This file contains hidden or 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
; 基础方法 | |
(define (same-parity x . l) | |
(same-parity-adpater x l) | |
) | |
(define (same-parity-adpater x l) | |
(if (null? l) | |
(cons x l) | |
(let ((car-item (car l))) | |
(if (even? (+ car-item x)) |
This file contains hidden or 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
package life.yanfeng.study.algorithm; | |
import edu.princeton.cs.algs4.StdOut; | |
public class SmartDate { | |
private final int month; | |
private final int day; | |
private final int year; | |
public SmartDate(int m, int d, int y) throws RuntimeException | |
{ |
This file contains hidden or 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
; 源码 1.14.scm 中有一部分. | |
; 既有实现的输出: 4. | |
(count-change 10) | |
; 新的实现... | |
(define us-coins (list 50 25 10 5 1)) | |
(define us-coins-0 (list 1 5 10 25 50)) | |
(define us-coins-1 (list 50 5 10 1 25)) | |
(define uk-coins (list 100 50 20 10 5 2 1 0.5)) |
This file contains hidden or 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
; (define (mul-interval x y) | |
; (let ((p1 (* (lower-bound x) (lower-bound y))) | |
; (p2 (* (lower-bound x) (upper-bound y))) | |
; (p3 (* (upper-bound x) (lower-bound y))) | |
; (p4 (* (upper-bound x) (upper-bound y))) | |
; ) | |
; (make-interval (min p1 p2 p3 p4) (max p1 p2 p3 p4)) | |
; ) | |
; ) |
This file contains hidden or 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
package life.yanfeng.study.algorithm; | |
import edu.princeton.cs.algs4.In; | |
import edu.princeton.cs.algs4.StdOut; | |
import java.util.Arrays; | |
import java.util.Random; | |
import java.util.concurrent.ThreadLocalRandom; | |
// for 1.1.39 |
This file contains hidden or 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
(define zero (lambda (f) (lambda (x) x))) | |
(define one (lambda (f) (lambda (x) (f x)))) | |
(define two (lambda (f) (lambda (x) (f (f x))))) | |
(define three (lambda (f) (lambda (x) (f (f (f x)))))) | |
(define (add m n) | |
(lambda (f) (lambda (x) ((m f) ((n f) x)))) |
This file contains hidden or 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
(define (my-cons x y) | |
(lambda (m) | |
(cond ((= m 0) x) | |
((= m 1) y))) | |
) | |
(define (my-car z) (z 0)) | |
(define (my-cdr z) (z 1)) |