Skip to content

Instantly share code, notes, and snippets.

#!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);
@zard1989
zard1989 / insertion_sort.lisp
Created May 30, 2011 18:40
insertion sort in common lisp
(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
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)
(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."
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
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
#menubar {
position: fixed;
bottom: 0;
left: 10px;
right: 10px;
height: 20px;
}
#!/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")
#!/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";
#!/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)