Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
37
Task/Zig-zag-matrix/AppleScript/zig-zag-matrix-1.applescript
Normal file
37
Task/Zig-zag-matrix/AppleScript/zig-zag-matrix-1.applescript
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
set n to 5 -- Size of zig-zag matrix (n^2 cells).
|
||||
|
||||
-- Create an empty matrix.
|
||||
set m to {}
|
||||
repeat with i from 1 to n
|
||||
set R to {}
|
||||
repeat with j from 1 to n
|
||||
set end of R to 0
|
||||
end repeat
|
||||
set end of m to R
|
||||
end repeat
|
||||
|
||||
-- Populate the matrix in a zig-zag manner.
|
||||
set {x, y, v, d} to {1, 1, 0, 1}
|
||||
repeat while v < (n ^ 2)
|
||||
if 1 ≤ x and x ≤ n and 1 ≤ y and y ≤ n then
|
||||
set {m's item y's item x, x, y, v} to {v, x + d, y - d, v + 1}
|
||||
else if x > n then
|
||||
set {x, y, d} to {n, y + 2, -d}
|
||||
else if y > n then
|
||||
set {x, y, d} to {x + 2, n, -d}
|
||||
else if x < 1 then
|
||||
set {x, y, d} to {1, y, -d}
|
||||
else if y < 1 then
|
||||
set {x, y, d} to {x, 1, -d}
|
||||
end if
|
||||
end repeat
|
||||
--> R = {{0, 1, 5, 6, 14}, {2, 4, 7, 13, 15}, {3, 8, 12, 16, 21}, {9, 11, 17, 20, 22}, {10, 18, 19, 23, 24}}
|
||||
|
||||
-- Reformat the matrix into a table for viewing.
|
||||
repeat with i in m
|
||||
repeat with j in i
|
||||
set j's contents to (characters -(length of (n ^ 2 as string)) thru -1 of (" " & j)) as string
|
||||
end repeat
|
||||
set end of i to return
|
||||
end repeat
|
||||
return return & m as string
|
||||
34
Task/Zig-zag-matrix/AppleScript/zig-zag-matrix-2.applescript
Normal file
34
Task/Zig-zag-matrix/AppleScript/zig-zag-matrix-2.applescript
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
set n to 5
|
||||
|
||||
set m to {}
|
||||
repeat with i from 1 to n
|
||||
set end of m to {} -- Built a foundation for the matrix out of n empty lists.
|
||||
end repeat
|
||||
|
||||
set {v, d, i} to {0, -1, 1}
|
||||
repeat while v < n ^ 2
|
||||
if length of m's item i < n then
|
||||
set {end of m's item i, i, v} to {f(v, n), i + d, v + 1}
|
||||
if i < 1 then
|
||||
set {i, d} to {1, -d}
|
||||
else if i > n then
|
||||
set {i, d} to {n, -d}
|
||||
else if i > 1 and (count of m's item (i - 1)) = 1 then
|
||||
set d to -d
|
||||
end if
|
||||
else
|
||||
set {i, d} to {i + 1, 1}
|
||||
end if
|
||||
end repeat
|
||||
|
||||
-- Handler/function to format the cells on the fly.
|
||||
on f(v, n)
|
||||
return (characters -(length of (n ^ 2 as string)) thru -1 of (" " & v)) as string
|
||||
end f
|
||||
|
||||
-- Reformat the matrix into a table for viewing.
|
||||
set text item delimiters to ""
|
||||
repeat with i in m
|
||||
set i's contents to (i as string) & return
|
||||
end repeat
|
||||
return return & m as string
|
||||
158
Task/Zig-zag-matrix/AppleScript/zig-zag-matrix-3.applescript
Normal file
158
Task/Zig-zag-matrix/AppleScript/zig-zag-matrix-3.applescript
Normal file
|
|
@ -0,0 +1,158 @@
|
|||
-- zigzagMatrix
|
||||
on zigzagMatrix(n)
|
||||
|
||||
-- diagonals :: n -> [[n]]
|
||||
script diagonals
|
||||
on |λ|(n)
|
||||
script mf
|
||||
on diags(xs, iCol, iRow)
|
||||
if (iCol < length of xs) then
|
||||
if iRow < n then
|
||||
set iNext to iCol + 1
|
||||
else
|
||||
set iNext to iCol - 1
|
||||
end if
|
||||
|
||||
set {headList, tail} to splitAt(iCol, xs)
|
||||
{headList} & diags(tail, iNext, iRow + 1)
|
||||
else
|
||||
{xs}
|
||||
end if
|
||||
end diags
|
||||
end script
|
||||
|
||||
diags(enumFromTo(0, n * n - 1), 1, 1) of mf
|
||||
end |λ|
|
||||
end script
|
||||
|
||||
-- oddReversed :: [a] -> Int -> [a]
|
||||
script oddReversed
|
||||
on |λ|(lst, i)
|
||||
if i mod 2 = 0 then
|
||||
lst
|
||||
else
|
||||
reverse of lst
|
||||
end if
|
||||
end |λ|
|
||||
end script
|
||||
|
||||
rowsFromDiagonals(n, map(oddReversed, |λ|(n) of diagonals))
|
||||
|
||||
end zigzagMatrix
|
||||
|
||||
-- Rows of given length from list of diagonals
|
||||
-- rowsFromDiagonals :: Int -> [[a]] -> [[a]]
|
||||
on rowsFromDiagonals(n, lst)
|
||||
if length of lst > 0 then
|
||||
|
||||
-- lengthOverOne :: [a] -> Bool
|
||||
script lengthOverOne
|
||||
on |λ|(lst)
|
||||
length of lst > 1
|
||||
end |λ|
|
||||
end script
|
||||
|
||||
set {edge, residue} to splitAt(n, lst)
|
||||
|
||||
{map(my head, edge)} & ¬
|
||||
rowsFromDiagonals(n, ¬
|
||||
map(my tail, ¬
|
||||
filter(lengthOverOne, edge)) & residue)
|
||||
else
|
||||
{}
|
||||
end if
|
||||
end rowsFromDiagonals
|
||||
|
||||
|
||||
-- TEST -----------------------------------------------------------------------
|
||||
on run
|
||||
|
||||
zigzagMatrix(5)
|
||||
|
||||
end run
|
||||
|
||||
|
||||
-- GENERIC FUNCTIONS ----------------------------------------------------------
|
||||
|
||||
-- enumFromTo :: Int -> Int -> [Int]
|
||||
on enumFromTo(m, n)
|
||||
if n < m then
|
||||
set d to -1
|
||||
else
|
||||
set d to 1
|
||||
end if
|
||||
set lst to {}
|
||||
repeat with i from m to n by d
|
||||
set end of lst to i
|
||||
end repeat
|
||||
return lst
|
||||
end enumFromTo
|
||||
|
||||
-- filter :: (a -> Bool) -> [a] -> [a]
|
||||
on filter(f, xs)
|
||||
tell mReturn(f)
|
||||
set lst to {}
|
||||
set lng to length of xs
|
||||
repeat with i from 1 to lng
|
||||
set v to item i of xs
|
||||
if |λ|(v, i, xs) then set end of lst to v
|
||||
end repeat
|
||||
return lst
|
||||
end tell
|
||||
end filter
|
||||
|
||||
-- head :: [a] -> a
|
||||
on head(xs)
|
||||
if length of xs > 0 then
|
||||
item 1 of xs
|
||||
else
|
||||
missing value
|
||||
end if
|
||||
end head
|
||||
|
||||
-- map :: (a -> b) -> [a] -> [b]
|
||||
on map(f, xs)
|
||||
tell mReturn(f)
|
||||
set lng to length of xs
|
||||
set lst to {}
|
||||
repeat with i from 1 to lng
|
||||
set end of lst to |λ|(item i of xs, i, xs)
|
||||
end repeat
|
||||
return lst
|
||||
end tell
|
||||
end map
|
||||
|
||||
-- Lift 2nd class handler function into 1st class script wrapper
|
||||
-- mReturn :: Handler -> Script
|
||||
on mReturn(f)
|
||||
if class of f is script then
|
||||
f
|
||||
else
|
||||
script
|
||||
property |λ| : f
|
||||
end script
|
||||
end if
|
||||
end mReturn
|
||||
|
||||
-- splitAt:: n -> list -> {n items from start of list, rest of list}
|
||||
-- splitAt :: Int -> [a] -> ([a], [a])
|
||||
on splitAt(n, xs)
|
||||
if n > 0 and n < length of xs then
|
||||
{items 1 thru n of xs, items (n + 1) thru -1 of xs}
|
||||
else
|
||||
if n < 1 then
|
||||
{{}, xs}
|
||||
else
|
||||
{xs, {}}
|
||||
end if
|
||||
end if
|
||||
end splitAt
|
||||
|
||||
-- tail :: [a] -> [a]
|
||||
on tail(xs)
|
||||
if length of xs > 1 then
|
||||
items 2 thru -1 of xs
|
||||
else
|
||||
{}
|
||||
end if
|
||||
end tail
|
||||
65
Task/Zig-zag-matrix/AppleScript/zig-zag-matrix-4.applescript
Normal file
65
Task/Zig-zag-matrix/AppleScript/zig-zag-matrix-4.applescript
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
on zigzagMatrix(n)
|
||||
script o
|
||||
property matrix : {} -- Matrix list.
|
||||
property row : missing value -- Row sublist.
|
||||
end script
|
||||
|
||||
repeat n times
|
||||
set end of o's matrix to {} -- Build a foundation for the matrix out of n empty lists.
|
||||
end repeat
|
||||
|
||||
set {r, d} to {1, -1} -- Row index and direction to next insertion row (negative = row above).
|
||||
repeat with v from 0 to (n ^ 2) - 1 -- Values to insert.
|
||||
set o's row to o's matrix's item r
|
||||
repeat while ((count o's row) = n)
|
||||
set r to r + 1
|
||||
set d to 1
|
||||
set o's row to o's matrix's item r
|
||||
end repeat
|
||||
set end of o's row to v
|
||||
set r to r + d
|
||||
if (r < 1) then
|
||||
set r to 1
|
||||
set d to -d
|
||||
else if (r > n) then
|
||||
set r to n
|
||||
set d to -d
|
||||
else if ((r > 1) and ((count o's matrix's item (r - 1)) = 1)) then
|
||||
set d to -d
|
||||
end if
|
||||
end repeat
|
||||
|
||||
return o's matrix
|
||||
end zigzagMatrix
|
||||
|
||||
-- Demo:
|
||||
on matrixToText(matrix, w)
|
||||
script o
|
||||
property matrix : missing value
|
||||
property row : missing value
|
||||
end script
|
||||
|
||||
set o's matrix to matrix
|
||||
set padding to " "
|
||||
repeat with r from 1 to (count o's matrix)
|
||||
set o's row to o's matrix's item r
|
||||
repeat with i from 1 to (count o's row)
|
||||
set o's row's item i to text -w thru end of (padding & o's row's item i)
|
||||
end repeat
|
||||
set o's matrix's item r to join(o's row, "")
|
||||
end repeat
|
||||
|
||||
return join(o's matrix, linefeed)
|
||||
end matrixToText
|
||||
|
||||
on join(lst, delim)
|
||||
set astid to AppleScript's text item delimiters
|
||||
set AppleScript's text item delimiters to delim
|
||||
set txt to lst as text
|
||||
set AppleScript's text item delimiters to astid
|
||||
return txt
|
||||
end join
|
||||
|
||||
set n to 5
|
||||
set matrix to zigzagMatrix(n)
|
||||
linefeed & matrixToText(matrix, (count (n ^ 2 - 1 as integer as text)) + 2) & linefeed
|
||||
Loading…
Add table
Add a link
Reference in a new issue