% Created by Ben Cassell
% Created 2006/01/25
% Last Updated 2006/03/01
% Useful assortment of procedures
% Show all preset colours
proc colourShow
var window : int := Window.Open ("graphics:564;500")
var font : int := Font.New ("timesnewroman:10")
for i : 0 .. 16
for j : 0 .. 14
Draw.FillBox (i * 32, j * 32, i * 32 + 32 - 1, j * 32 + 32 - 1, (i + 1) + (j * 17))
Draw.Text (intstr (i + 1), i * 32 + 10, 485, font, 7)
Draw.Text (intstr (j + 1), 547, j * 32 + 10, font, 7)
end for
end for
end colourShow
% Find a Fibonacci number at a position: Must use 0, 1, 1, userNum
proc fibFind (numBefore, numCurrent, numPosition, numStop : int)
if numPosition ~= numStop then
fibFind (numCurrent, numCurrent + numBefore, numPosition + 1, numStop)
else
put numCurrent
end if
end fibFind
% Find the hypoteneuse of a triangle
fcn hypFind (side1, side2 : real) : real
result sqrt (side1 ** 2 + side2 ** 2)
end hypFind
% Reverse an array of integers
proc intReverse (var list : array 0 .. * of int)
var tempList : array 0 .. upper (list) of int
for i : 0 .. upper (list)
tempList (i) := list (i)
end for
for i : 0 .. upper (list)
list (i) := tempList (upper (tempList) - i)
end for
end intReverse
% Swap two integers
proc intSwap (var list : array 0 .. * of int, pos1, pos2 : int)
var tempNum : int
tempNum := list (pos1)
list (pos1) := list (pos2)
list (pos2) := tempNum
end intSwap
% Quick sort a set of integers: Must use list, lower index, upper index
proc intSort (var list : array 0 .. * of int, left, right : int)
var pivot : int
intSwap (list, left, (left + right) div 2)
var small := left
for i : left + 1 .. right
if list (i) <= list (left) then
small += 1
intSwap (list, small, i)
end if
end for
intSwap (list, left, small)
pivot := small
if left < pivot - 1 then
intSort (list, left, pivot - 1)
end if
if pivot + 1 < right then
intSort (list, pivot + 1, right)
end if
end intSort
% Reverse an array of reals
proc realReverse (var list : array 0 .. * of real)
var tempList : array 0 .. upper (list) of real
for i : 0 .. upper (list)
tempList (i) := list (i)
end for
for i : 0 .. upper (list)
list (i) := tempList (upper (tempList) - i)
end for
end realReverse
% Swap two reals
proc realSwap (var list : array 0 .. * of real, pos1, pos2 : int)
var tempNum : real
tempNum := list (pos1)
list (pos1) := list (pos2)
list (pos2) := tempNum
end realSwap
% Quick sort a set of reals: Must use list, lower index, upper index
proc realSort (var list : array 0 .. * of real, left, right : int)
var pivot : int
realSwap (list, left, (left + right) div 2)
var small := left
for i : left + 1 .. right
if list (i) <= list (left) then
small += 1
realSwap (list, small, i)
end if
end for
realSwap (list, left, small)
pivot := small
if left < pivot - 1 then
realSort (list, left, pivot - 1)
end if
if pivot + 1 < right then
realSort (list, pivot + 1, right)
end if
end realSort
% Find the slope of a line
fcn slopeFind (x1, y1, x2, y2 : real) : real
result (y2 - y1) / (x2 - x1)
end slopeFind
% Mirror a string
fcn strMirror (word : string) : string
var resultWord : string := ""
for decreasing i : length (word) .. 1
resultWord += word (i)
end for
result resultWord
end strMirror
% Reverse an array of reals
proc strReverse (var list : array 0 .. * of string)
var tempList : array 0 .. upper (list) of string
for i : 0 .. upper (list)
tempList (i) := list (i)
end for
for i : 0 .. upper (list)
list (i) := tempList (upper (tempList) - i)
end for
end strReverse
% Remove all blank spaces from a string
fcn strSpaceRemove (var word : string) : string
var resultWord : string := ""
for i : 1 .. length (word)
if word (i) ~= " " then
resultWord += word (i)
end if
end for
result resultWord
end strSpaceRemove
% Test a word to check for a palindrome
fcn palindromeCheck (var word : string, spaces : boolean) : boolean
var reverseWord : string
word := Str.Lower (word)
if ~spaces then
word := strSpaceRemove (word)
end if
reverseWord := strMirror (word)
if word = reverseWord then
result true
end if
result false
end palindromeCheck
% Swap two strings
proc strSwap (var list : array 0 .. * of string, pos1, pos2 : int)
var tempNum : string
tempNum := list (pos1)
list (pos1) := list (pos2)
list (pos2) := tempNum
end strSwap
% Quick sort a set of strings: Must use list, lower index, upper index
proc strSort (var list : array 0 .. * of string, left, right : int)
var pivot : int
strSwap (list, left, (left + right) div 2)
var small := left
for i : left + 1 .. right
if list (i) <= list (left) then
small += 1
strSwap (list, small, i)
end if
end for
strSwap (list, left, small)
pivot := small
if left < pivot - 1 then
strSort (list, left, pivot - 1)
end if
if pivot + 1 < right then
strSort (list, pivot + 1, right)
end if
end strSort
|