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
#!perl | |
# This program is for keeping the linear-time | |
# algorithm for the maximum segment sum problem. | |
use strict; | |
use 5.012; | |
use List::Util qw/sum/; | |
sub mss { # This is the O(n) algorithm | |
my ($sum, $mss) = (0, 0); |
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
(defun insert (item lst &optional (key #'<)) | |
(if (null lst) | |
(list item) | |
(if (funcall key item (car lst)) | |
(cons item lst) | |
(cons (car lst) (insert item (cdr lst) key))))) | |
(defun insertion-sort (lst &optional (key #'<)) | |
(if (null lst) | |
lst |
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
sub dist_square_sum { | |
my @array = @_; | |
my ($sum, $sum_sq, $r); | |
$sum = $sum_sq = $r = 0; | |
for (@array) { # O(N) | |
$sum += $_; # O(1) | |
$sum_sq += $_**2; # O(1) | |
} | |
for (0..$#array-1) { # O(N) | |
$sum -= $array[$_]; # O(1) |
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
(defun insert (cmp target list) | |
"Insert the target into a sorted list." | |
(if (null list) | |
(cons target '()) | |
(if (funcall cmp target (first list)) | |
(cons target list) | |
(cons (first list) (insert cmp target (rest list)))))) | |
(defun insertionSort (cmp list) | |
"Insertion Sort, a recursive version." |
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
def chinese_time(time=Time.now) | |
chinese_hour = "子丑寅卯辰巳午未申酉戊亥".split(//u) | |
chinese_num = "零壹貳參肆伍陸柒捌玖".split(//u) | |
if time.hour.even? | |
hour = time.hour | |
min = time.min+60 | |
else | |
hour = time.hour+1 | |
min = time.min | |
end |
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
asInt :: String -> Int | |
asInt xs = loop 0 xs | |
loop acc [] = acc | |
loop acc (x:xs) = let acc' = acc * 10 + digitToInt x | |
in loop acc' xs | |
loopF :: Double -> String -> Double | |
loopF acc [] = acc | |
loopF acc (x:xs) = let acc' = (acc + fromIntegral (digitToInt x))*0.1 |
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
#menubar { | |
position: fixed; | |
bottom: 0; | |
left: 10px; | |
right: 10px; | |
height: 20px; | |
} |
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
#!/usr/bin/gosh | |
(define (move n from to space) | |
(cond ((= n 1) (display (string-append "move #1 from " from " to " to "\n"))) | |
(else | |
(move (- n 1) from space to) | |
(display (string-append "move #" (number->string n) " from " from " to " to "\n")) | |
(move (- n 1) space to from)))) | |
(move 3 "A" "B" "C") |
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
#!/usr/bin/perl | |
use strict; | |
use 5.010; | |
hanoi(3, "A", "B", "C"); | |
sub hanoi { | |
my ($N, $FROM, $TO, $SPACE) = @_; | |
if ($N == 1) { | |
say "Move #$N from $FROM to $TO"; |
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
#!/usr/bin/guile -s | |
!# | |
(define (sqrt x) | |
(define (good-enough? guess) | |
(< (abs (- guess (/ x guess))) .001)) | |
(define (improve guess) | |
(/ (+ guess (/ x guess)) 2)) | |
(define (try guess) | |
(if (good-enough? guess) |
NewerOlder