This commit is contained in:
Ingy döt Net 2013-04-10 21:29:02 -07:00
parent 764da6cbbb
commit db842d013d
19005 changed files with 197040 additions and 7 deletions

View file

@ -0,0 +1,7 @@
{ 1 2 3 }
{
[ "The initial array: " write . ]
[ [ 42 1 ] dip set-nth ]
[ "Modified array: " write . ]
[ "The element we modified: " write [ 1 ] dip nth . ]
} cleave

View file

@ -0,0 +1,6 @@
V{ 1 2 3 }
{
[ "The initial vector: " write . ]
[ [ 42 ] dip push ]
[ "Modified vector: " write . ]
} cleave

View file

@ -0,0 +1,45 @@
# Arrays are better called lists in GAP. Lists may have elements of mixed types, e$
v := [ 10, 7, "bob", true, [ "inner", 5 ] ];
# [ 10, 7, "bob", true, [ "inner", 5 ] ]
# List index runs from 1 to Size(v)
v[1];
# 10
v[0];
# error
v[5];
# [ "inner", 5 ]
v[6];
# error
# One can assign a value to an undefined element
v[6] := 100;
# Even if it's not after the last: a list may have undefined elements
v[10] := 1000;
v;
# [ 10, 7, "bob", true, [ "inner", 5 ], 100,,,, 1000 ]
# And one can check for defined values
IsBound(v[10]);
# true
IsBound(v[9]);
# false
# Size of the list
Size(v);
# 10
# Appending a list to the end of another
Append(v, [ 8, 9]);
v;
# [ 10, 7, "bob", true, [ "inner", 5 ], 100,,,, 1000, 8, 9 ]
# Adding an element at the end
Add(v, "added");
v;
# [ 10, 7, "bob", true, [ "inner", 5 ], 100,,,, 1000, 8, 9, "added" ]

View file

@ -0,0 +1,4 @@
array[0] = ' '
array[1] = 'A'
array[2] = 'B'
array[3] = 'C'

View file

@ -0,0 +1,2 @@
for(i = 0; i < k; i += 1)
array[i] = i + 1

View file

@ -0,0 +1,12 @@
array[1,1] = 1
array[1,2] = 2
array[1,3] = 3
array[1,4] = 4
array[2,1] = 2
array[2,2] = 4
array[2,3] = 6
array[2,4] = 8
array[3,1] = 3
array[3,2] = 6
array[3,3] = 9
array[3,4] = 12

View file

@ -0,0 +1,3 @@
for(i = 1; i <= k; i += 1)
for(j = 1; j <= h; j += 1)
array[i,j] = i * j

View file

@ -0,0 +1 @@
Start,Programs,Lotus 123,Type:Bob[downarrow],Kat[downarrow],Sarah[downarrow]

View file

@ -0,0 +1,8 @@
DIM mynumbers AS INTEGER[]
myfruits AS STRING[]
mynumbers[0] = 1.5
mynumbers[1] = 2.3
myfruits[0] = "apple"
myfruits[1] = "banana"

View file

@ -0,0 +1,5 @@
[1 2 3]:a; # numeric only array, assigned to a and then dropped
10,:a; # assign to a [0 1 2 3 4 5 6 7 8 9]
a 0= puts # pick element at index 0 (stack: 0)
a 10+puts # append 10 to the end of a
10 a+puts # prepend 10 to a

View file

@ -0,0 +1,20 @@
def aa = [ 1, 25, 31, -3 ] // list
def a = [0] * 100 // list of 100 zeroes
def b = 1..9 // range notation
def c = (1..10).collect { 2.0**it } // each output element is 2**(corresponding invoking list element)
// There are no true "multi-dimensional" arrays in Groovy (as in most C-derived languages).
// Use lists of lists in natural ("row major") order as a stand in.
def d = (0..1).collect { i -> (1..5).collect { j -> 2**(5*i+j) as double } }
def e = [ [ 1.0, 2.0, 3.0, 4.0 ],
[ 5.0, 6.0, 7.0, 8.0 ],
[ 9.0, 10.0, 11.0, 12.0 ],
[ 13.0, 14.0, 15.0, 16.0 ] ]
println aa
println b
println c
println()
d.each { print "["; it.each { elt -> printf "%7.1f ", elt }; println "]" }
println()
e.each { print "["; it.each { elt -> printf "%7.1f ", elt }; println "]" }

View file

@ -0,0 +1,3 @@
def identity = { n ->
(1..n).collect { i -> (1..n).collect { j -> i==j ? 1.0 : 0.0 } }
}

View file

@ -0,0 +1,7 @@
def i2 = identity(2)
def i15 = identity(15)
i2.each { print "["; it.each { elt -> printf "%4.1f ", elt }; println "]" }
println()
i15.each { print "["; it.each { elt -> printf "%4.1f ", elt }; println "]" }

View file

@ -0,0 +1,11 @@
def strings = ['Mary', 'had', 'a', 'little', 'lamb', ". It's", 'fleece', 'was', 'white', 'as', 'snow']
println strings
strings[0] = 'Arthur'
strings[4] = 'towel'
strings[6] = 'stain'
strings[8] = 'ripe'
strings[10] = 'strawberries'
println strings

View file

@ -0,0 +1 @@
println strings[-1]

View file

@ -0,0 +1,3 @@
println strings[0, 7, 2, 3, 8]
println strings[0..4]
println strings[0..3, -5]

View file

@ -0,0 +1,11 @@
REAL :: n = 3, Astat(n), Bdyn(1, 1)
Astat(2) = 2.22222222
WRITE(Messagebox, Name) Astat(2)
ALLOCATE(Bdyn, 2*n, 3*n)
Bdyn(n-1, n) = -123
WRITE(Row=27) Bdyn(n-1, n)
ALIAS(Astat, n-1, last2ofAstat, 2)
WRITE(ClipBoard) last2ofAstat ! 2.22222222 0

View file

@ -0,0 +1,98 @@
record aThing(a, b, c) # arbitrary object (record or class) for illustration
procedure main()
A0 := [] # empty list
A0 := list() # empty list (default size 0)
A0 := list(0) # empty list (literal size 0)
A1 := list(10) # 10 elements, default initializer &null
A2 := list(10, 1) # 10 elements, initialized to 1
# literal array construction - arbitrary dynamically typed members
A3 := [1, 2, 3, ["foo", "bar", "baz"], aThing(1, 2, 3), "the end"]
# left-end workers
# NOTE: get() is a synonym for pop() which allows nicely-worded use of put() and get() to implement queues
#
Q := [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
x := pop(A0) # x is 1
x := get(A0) # x is 2
push(Q,0)
# Q is now [0,3, 4, 5, 6, 7, 8, 9, 10]
# right-end workers
x := pull(Q) # x is 10
put(Q, 100) # Q is now [0, 3, 4, 5, 6, 7, 8, 9, 100]
# push and put return the list they are building
# they also can have multiple arguments which work like repeated calls
Q2 := put([],1,2,3) # Q2 is [1,2,3]
Q3 := push([],1,2,3) # Q3 is [3,2,1]
Q4 := push(put(Q2),4),0] # Q4 is [0,1,2,3,4] and so is Q2
# array access follows with A as the sample array
A := [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
# get element indexed from left
x := A[1] # x is 10
x := A[2] # x is 20
x := A[10] # x is 100
# get element indexed from right
x := A[-1] # x is 100
x := A[-2] # x is 90
x := A[-10] # x is 10
# copy array to show assignment to elements
B := copy(A)
# assign element indexed from left
B[1] := 11
B[2] := 21
B[10] := 101
# B is now [11, 21, 30, 50, 60, 60, 70, 80, 90, 101]
# assign element indexed from right - see below
B[-1] := 102
B[-2] := 92
B[-10] := 12
# B is now [12, 21, 30, 50, 60, 60, 70, 80, 92, 102]
# list slicing
# the unusual nature of the slice - returning 1 less element than might be expected
# in many languages - is best understood if you imagine indexes as pointing to BEFORE
# the item of interest. When a slice is made, the elements between the two points are
# collected. eg in the A[3 : 6] sample, it will get the elements between the [ ] marks
#
# sample list: 10 20 [30 40 50] 60 70 80 90 100
# positive indexes: 1 2 3 4 5 6 7 8 9 10 11
# non-positive indexes: -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0
#
# I have deliberately drawn the indexes between the positions of the values.
# The nature of this indexing brings simplicity to string operations
#
# list slicing can also use non-positive indexes to access values from the right.
# The final index of 0 shown above shows how the end of the list can be nominated
# without having to know it's length
#
# NOTE: list slices are distinct lists, so assigning to the slice
# or a member of the slice does not change the values in A
#
# Another key fact to understand: once the non-positive indexes and length-offsets are
# resolved to a simple positive index, the index pair (if two are given) are swapped
# if necessary to yield the elements between the two.
#
S := A[3 : 6] # S is [30, 40, 50]
S := A[6 : 3] # S is [30, 40, 50] not illegal or erroneous
S := A[-5 : -8] # S is [30, 40, 50]
S := A[-8 : -5] # S is [30, 40, 50] also legal and meaningful
# list slicing with length request
S := A[3 +: 3] # S is [30, 40, 50]
S := A[6 -: 3] # S is [30, 40, 50]
S := A[-8 +: 3] # S is [30, 40, 50]
S := A[-5 -: 3] # S is [30, 40, 50]
S := A[-8 -: -3] # S is [30, 40, 50]
S := A[-5 +: -3] # S is [30, 40, 50]
end

View file

@ -0,0 +1,3 @@
# Unicon provides a number of extensions
# insert and delete work on lists allowing changes in the middle
# possibly others

5
Task/Arrays/Io/arrays.io Normal file
View file

@ -0,0 +1,5 @@
foo := list("foo", "bar", "baz")
foo at(1) println // bar
foo append("Foobarbaz")
foo println
foo atPut(2, "barbaz") // baz becomes barbaz

28
Task/Arrays/J/arrays.j Normal file
View file

@ -0,0 +1,28 @@
1 NB. a stand-alone scalar value is an array without any axis
1
NB. invoking any array produces that array as the result
{. array=: 1 3, 6#0 NB. create, name, then get head item of the array: 1 3 0 0 0 0 0 0
1
0 { array NB. another way to get the head item
1
aword=: 'there' NB. a literal array
0 1 3 2 2 { aword NB. multiple items can be drawn in a single action
three
]twoD=: 3 5 $ 'abcdefghijklmnopqrstuvwxyz'
abcde
fghij
klmno
1 { twoD NB. item 1 from twoD - a list of three items
fghij
1 {"1 twoD NB. item 1 from each rank-1 item of twoD (i.e. column 1)
bgl
(<2 2){ twoD NB. bracket indexing is not used in J
m
'X' 1} aword NB. amend item 1
tXere
aword=: 'X' 1 4} aword NB. in-place amend of items 1 and 4
tXerX
'X' (0 0;1 1;2 2)} twoD NB. amend specified items
Xbcde
fXhij
klXno

View file

@ -0,0 +1,11 @@
//creates an array of length 3
Array:New array[3]:Number;
function main() {
Var:Number length;
Array:GetLength(array, length) //retrieve length of array
Konsol:Log(length)
array[0] = 5; //assign value
Konsol:Log(array[0]) //retrieve value and display
}

View file

@ -0,0 +1,3 @@
10 myArray :array
0 array 5 [] ! # store 0 at the sixth cell in the array
array 5 [] @ # contents of sixth cell in array

View file

@ -0,0 +1,28 @@
default {
state_entry() {
list lst = ["1", "2", "3"];
llSay(0, "Create and Initialize a List\nList=["+llList2CSV(lst)+"]\n");
lst += ["A", "B", "C"];
llSay(0, "Append to List\nList=["+llList2CSV(lst)+"]\n");
lst = llListInsertList(lst, ["4", "5", "6"], 3);
llSay(0, "List Insertion\nList=["+llList2CSV(lst)+"]\n");
lst = llListReplaceList(lst, ["a", "b", "c"], 3, 5);
llSay(0, "Replace a portion of a list\nList=["+llList2CSV(lst)+"]\n");
lst = llListRandomize(lst, 1);
llSay(0, "Randomize a List\nList=["+llList2CSV(lst)+"]\n");
lst = llListSort(lst, 1, TRUE);
llSay(0, "Sort a List\nList=["+llList2CSV(lst)+"]\n");
lst = [1, 2.0, "string", (key)NULL_KEY, ZERO_VECTOR, ZERO_ROTATION];
string sCSV = llList2CSV(lst);
llSay(0, "Serialize a List of different datatypes to a string\n(integer, float, string, key, vector, rotation)\nCSV=\""+sCSV+"\"\n");
lst = llCSV2List(sCSV);
llSay(0, "Deserialize a string CSV List\n(note that all elements are now string datatype)\nList=["+llList2CSV(lst)+"]\n");
}
}

View file

@ -0,0 +1,13 @@
dim Array(10)
Array(0) = -1
Array(10) = 1
print Array( 0), Array( 10)
REDIM Array( 100)
print Array( 0), Array( 10)
Array( 0) = -1
print Array( 0), Array( 10)

View file

@ -0,0 +1,5 @@
+ a : ARRAY(INTEGER);
a := ARRAY(INTEGER).create 0 to 9;
a.put 1 to 0;
a.put 3 to 1;
a.item(1).print;

View file

@ -0,0 +1,5 @@
array 5 ; default origin is 1, every item is empty
(array 5 0) ; custom origin
make "a {1 2 3 4 5} ; array literal
setitem 1 :a "ten ; Logo is dynamic; arrays can contain different types
print item 1 :a ; ten

View file

@ -0,0 +1,3 @@
a = Array[Sin, 10]
a[[1]]
Delete[a, 2]

View file

@ -0,0 +1,25 @@
/* Declare an array, subscripts run from 0 to max value */
array(a, flonum, 20, 20, 3)$
arrayinfo(a);
/* [complete, 3, [20, 20, 3]] */
a[0, 0]: 1.0;
listarray(a);
/* [1.0, 0.0, 0.0, ..., 0.0] */
/* Show all declared arrays */
arrays;
/* [a] */
/* One may also use an array without declaring it, it's a hashed array */
b[1]: 1000;
b['x]: 3/4; /* hashed array may have any subscript */
arrayinfo(b);
/* [hashed, 1, [1], [x]] */
listarray(b);
/* [1000, 3/4] */

View file

@ -0,0 +1 @@
VAR a: ARRAY [1..10] OF INTEGER;

View file

@ -0,0 +1,2 @@
VAR a := ARRAY [1..10] OF INTEGER {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
VAR arr1 := ARRAY [1..10] OF INTEGER {1, ..} (* Initialize all elements to 1. *)

View file

@ -0,0 +1,2 @@
VAR arr := ARRAY [1..3] OF INTEGER {1, 2, 3};
VAR myVar := a[2];

View file

@ -0,0 +1,2 @@
VAR arr := ARRAY [1..3] OF INTEGER;
arr[1] := 10;