Skip to content

Instantly share code, notes, and snippets.

@zallarak
zallarak / insertion_sort.py
Created January 8, 2012 17:35
Insertion Sort
def InsertionSort(A):
"""
Takes a list, returns a list.
"""
for j in range(len(A)):
key = A[j]
i = j-1
while (i>=0) and (A[i]>key):
A[i+1] = A[i]
i = i-1
@zallarak
zallarak / insertion_sort.rb
Created January 8, 2012 20:19
Insertion Sort (Ruby)
def InsertionSort(a)
a.each_with_index do |item, index|
i = index - 1
while i>=0
break if item >= a[i]
a[i+1] = a[i]
i -= 1
end
@zallarak
zallarak / linked_list_1.scm
Created January 9, 2012 00:38
Singly linked lists in Scheme (Lisp)
; One may form pairs in Scheme with (cons ITEM1 ITEM2)
; For example, (cons 1 2) represents the pair 1 and 2
; Let's define a the pair of 1 and 2 as one-and-two:
(define one-and-two (cons 1 2))
; Now one can invoke the pair with one-and-two
;1 ]=> one-and-two
; Value 11: (1 . 2)
@zallarak
zallarak / linked_list_2.scm
Created January 9, 2012 01:51
Mapping a linked list in Scheme (Lisp)
; Mapping a singly linked list in Scheme.
; map-1 - the name of our mapping function
; proc - the tranformation
; items - the list
(define (map-1 proc items)
(if (null? items)
()
(cons (proc (car items))
@zallarak
zallarak / hierarchical_data_structures_1.scm
Created January 9, 2012 16:38
Creating a tree in Scheme (Lisp)
; This creates a simple tree
(define tree-1 (cons (list 1 2) (list 3 4)))
; Quick linked list review
(list 1 2 3 4) ;is equivalent to:
(cons 1 (cons 2 (cons 3 (cons 4 ()))))
; And here is some interpreter action
; just for practice:
@zallarak
zallarak / leaves_in_a_tree.scm
Created January 9, 2012 18:35
Counting the leaves in a tree in Scheme (Lisp)
(define (count-leaves t)
(cond ((null? t) 0)
((not (pair? t)) 1)
(else (+ (count-leaves (car t))
(count-leaves (cdr t))))))
; The general logic of this function is:
; If the tree is null, return 0
; elif: return 1 if at a leaf. We know
; That we're at a leaf if the position is not null and not a pair
@zallarak
zallarak / merge_sort_1.py
Created January 12, 2012 01:58
Merge sort part 1
def MergeSort(a):
"""
Takes a list, returns a list.
"""
if (len(a) < 2):
return a
else:
mid = (len(a)) / 2
left = a[:mid]
right = a[mid:]
@zallarak
zallarak / merge_sort_2.py
Created January 12, 2012 02:05
Merge sort part 2
def Merge(left, right):
output = []
x, y = 0, 0
while x < len(left) and y < len(right):
if left[x] <= right[y]:
output.append(left[x])
x += 1
else:
output.append(right[y])
y += 1
@zallarak
zallarak / sym_diff_1.scm
Created January 14, 2012 23:12
Symbolic differentiation
; Symbolic differentiation
(define (deriv exp var)
(cond ((number? exp) 0)
((variable? exp)
(if (same-variable? exp var) 1 0))
((sum? exp)
(make-sum (deriv (addend exp) var)
(deriv (augend exp) var)))
((product? exp)
(make-sum
@zallarak
zallarak / binary_tree_1.scm
Created January 16, 2012 12:39
Binary trees in Scheme
; sets as unordered lists:
; a set for now is defined as as being able
; to undergo the following operations
; 1) element-of-set? checks if x is in set
(define (element-of-set? x set)
(cond ((null? set) false)
((equal? x (car set)) true)
(else (element-of-set? x (cdr set)))))