2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
|
|
@ -1,14 +1,26 @@
|
|||
This task is about arrays.
|
||||
|
||||
For hashes or associative arrays, please see [[Creating an Associative Array]].
|
||||
|
||||
For a definition and in-depth discussion of what an array is, see [[Array]].
|
||||
|
||||
In this task, the goal is to show basic array syntax in your
|
||||
language. Basically, create an array, assign a value to it, and
|
||||
retrieve an element. (if available, show both fixed-length arrays and
|
||||
dynamic arrays, pushing a value into it.)
|
||||
|
||||
Please discuss at Village Pump: {{vp|Arrays}}. Please merge code in from obsolete tasks [[Creating an Array]], [[Assigning Values to an Array]], and [[Retrieving an Element of an Array]].
|
||||
;Task:
|
||||
Show basic array syntax in your language.
|
||||
|
||||
'''See also'''
|
||||
* [[Collections]]
|
||||
* [[Two-dimensional array (runtime)]]
|
||||
Basically, create an array, assign a value to it, and retrieve an element (if available, show both fixed-length arrays and
|
||||
dynamic arrays, pushing a value into it).
|
||||
|
||||
Please discuss at Village Pump: {{vp|Arrays}}.
|
||||
|
||||
Please merge code in from these obsolete tasks:
|
||||
:::* [[Creating an Array]]
|
||||
:::* [[Assigning Values to an Array]]
|
||||
:::* [[Retrieving an Element of an Array]]
|
||||
|
||||
|
||||
;Related tasks:
|
||||
* [[Collections]]
|
||||
* [[Creating an Associative Array]]
|
||||
* [[Two-dimensional array (runtime)]]
|
||||
<br><br>
|
||||
|
|
|
|||
1
Task/Arrays/APL/arrays-1.apl
Normal file
1
Task/Arrays/APL/arrays-1.apl
Normal file
|
|
@ -0,0 +1 @@
|
|||
+/ 1 2 3
|
||||
1
Task/Arrays/APL/arrays-2.apl
Normal file
1
Task/Arrays/APL/arrays-2.apl
Normal file
|
|
@ -0,0 +1 @@
|
|||
1 + 2 + 3
|
||||
1
Task/Arrays/APL/arrays-3.apl
Normal file
1
Task/Arrays/APL/arrays-3.apl
Normal file
|
|
@ -0,0 +1 @@
|
|||
+
|
||||
1
Task/Arrays/APL/arrays-4.apl
Normal file
1
Task/Arrays/APL/arrays-4.apl
Normal file
|
|
@ -0,0 +1 @@
|
|||
1 2 3
|
||||
|
|
@ -1 +1 @@
|
|||
[val 1 2 3]
|
||||
[1 2 3]
|
||||
|
|
|
|||
1
Task/Arrays/Babel/arrays-10.pb
Normal file
1
Task/Arrays/Babel/arrays-10.pb
Normal file
|
|
@ -0,0 +1 @@
|
|||
(1 2 3) ls2lf ;
|
||||
1
Task/Arrays/Babel/arrays-11.pb
Normal file
1
Task/Arrays/Babel/arrays-11.pb
Normal file
|
|
@ -0,0 +1 @@
|
|||
[ptr 'foo' 'bar' 'baz'] ar2ls lsstr !
|
||||
1
Task/Arrays/Babel/arrays-12.pb
Normal file
1
Task/Arrays/Babel/arrays-12.pb
Normal file
|
|
@ -0,0 +1 @@
|
|||
(1 2 3) bons ;
|
||||
|
|
@ -1 +1 @@
|
|||
[val 1 2 3] 1 th
|
||||
[1 2 3] 1 th ;
|
||||
|
|
|
|||
|
|
@ -1 +1 @@
|
|||
[val 1 2 3] 7 1 paste
|
||||
[1 2 3] dup 1 7 set ;
|
||||
|
|
|
|||
|
|
@ -1 +1 @@
|
|||
[ptr 1 2 3] [ptr 7] 1 paste
|
||||
[ptr 1 2 3] dup 1 [ptr 7] set ;
|
||||
|
|
|
|||
|
|
@ -1 +1 @@
|
|||
[ptr 'foo' 'bar' 'baz' 'bop'] 1 3 slice
|
||||
[ptr 1 2 3 4 5 6] 1 3 slice ;
|
||||
|
|
|
|||
|
|
@ -1,4 +1 @@
|
|||
[ptr 1 2 3] dup
|
||||
<- arlen 1 + newin dup dup ->
|
||||
0 paste
|
||||
[ptr 4] 3 paste
|
||||
[1 2 3] [4] cat
|
||||
|
|
|
|||
|
|
@ -1,2 +1 @@
|
|||
[ptr 1 2 3]
|
||||
ar2ls (4) unshift bons
|
||||
[ptr 1 2 3] [ptr 4] cat
|
||||
|
|
|
|||
1
Task/Arrays/Babel/arrays-9.pb
Normal file
1
Task/Arrays/Babel/arrays-9.pb
Normal file
|
|
@ -0,0 +1 @@
|
|||
[1 2 3] ar2ls lsnum !
|
||||
85
Task/Arrays/Brainf---/arrays.bf
Normal file
85
Task/Arrays/Brainf---/arrays.bf
Normal file
|
|
@ -0,0 +1,85 @@
|
|||
===========[
|
||||
ARRAY DATA STRUCTURE
|
||||
|
||||
AUTHOR: Keith Stellyes
|
||||
WRITTEN: June 2016
|
||||
|
||||
This is a zero-based indexing array data structure, it assumes the following
|
||||
precondition:
|
||||
|
||||
>INDEX<|NULL|VALUE|NULL|VALUE|NULL|VALUE|NULL
|
||||
|
||||
(Where >< mark pointer position, and | separates addresses)
|
||||
|
||||
It relies heavily on [>] and [<] both of which are idioms for
|
||||
finding the next left/right null
|
||||
|
||||
HOW INDEXING WORKS:
|
||||
It runs a loop _index_ number of times, setting that many nulls
|
||||
to a positive, so it can be skipped by the mentioned idioms.
|
||||
Basically, it places that many "milestones".
|
||||
|
||||
EXAMPLE:
|
||||
If we seek index 2, and our array is {1 , 2 , 3 , 4 , 5}
|
||||
|
||||
FINDING INDEX 2:
|
||||
(loop to find next null, set to positive, as a milestone
|
||||
decrement index)
|
||||
|
||||
index
|
||||
2 |0|1|0|2|0|3|0|4|0|5|0
|
||||
1 |0|1|1|2|0|3|0|4|0|5|0
|
||||
0 |0|1|1|2|1|3|0|4|0|5|0
|
||||
|
||||
===========]
|
||||
|
||||
=======UNIT TEST=======
|
||||
SET ARRAY {48 49 50}
|
||||
>>++++++++++++++++++++++++++++++++++++++++++++++++>>
|
||||
+++++++++++++++++++++++++++++++++++++++++++++++++>>
|
||||
++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
<<<<<<++ Move back to index and set it to 2
|
||||
=======================
|
||||
|
||||
===RETRIEVE ELEMENT AT INDEX===
|
||||
|
||||
=ACCESS INDEX=
|
||||
[>>[>]+[<]<-] loop that sets a null to a positive for each iteration
|
||||
First it moves the pointer from index to first value
|
||||
Then it uses a simple loop that finds the next null
|
||||
it sets the null to a positive (1 in this case)
|
||||
Then it uses that same loop reversed to find the first
|
||||
null which will always be one right of our index
|
||||
so we decrement our index
|
||||
Finally we decrement pointer from the null byte to our
|
||||
index and decrement it
|
||||
|
||||
>> Move pointer to the first value otherwise we can't loop
|
||||
|
||||
[>]< This will find the next right null which will always be right
|
||||
of the desired value; then go one left
|
||||
|
||||
|
||||
. Output the value (In the unit test this print "2"
|
||||
|
||||
[<[-]<] Reset array
|
||||
|
||||
===ASSIGN VALUE AT INDEX===
|
||||
|
||||
STILL NEED TO ADJUST UNIT TESTS
|
||||
|
||||
NEWVALUE|>INDEX<|NULL|VALUE etc
|
||||
|
||||
[>>[>]+[<]<-] Like above logic except it empties the value and doesn't reset
|
||||
>>[>]<[-]
|
||||
|
||||
[<]< Move pointer to desired value note that where the index was stored
|
||||
is null because of the above loop
|
||||
|
||||
[->>[>]+[<]<] If NEWVALUE is GREATER than 0 then decrement it & then find the
|
||||
newly emptied cell and increment it
|
||||
|
||||
[>>[>]<+[<]<<-] Move pointer to first value find right null move pointer left
|
||||
then increment where we want our NEWVALUE to be stored then
|
||||
return back by finding leftmost null then decrementing pointer
|
||||
twice then decrement our NEWVALUE cell
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
#var(type:intarray,size:3)aStackAllocatedArray.
|
||||
#var(int:3)aStackAllocatedArray.
|
||||
aStackAllocatedArray@0 := 1.
|
||||
aStackAllocatedArray@1 := 2.
|
||||
aStackAllocatedArray@2 := 3.
|
||||
|
|
|
|||
1
Task/Arrays/Elixir/arrays-1.elixir
Normal file
1
Task/Arrays/Elixir/arrays-1.elixir
Normal file
|
|
@ -0,0 +1 @@
|
|||
ret = {:ok, "fun", 3.1415}
|
||||
4
Task/Arrays/Elixir/arrays-2.elixir
Normal file
4
Task/Arrays/Elixir/arrays-2.elixir
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
elem(ret, 1) == "fun"
|
||||
elem(ret, 0) == :ok
|
||||
put_elem(ret, 2, "pi") # => {:ok, "fun", "pi"}
|
||||
ret == {:ok, "fun", 3.1415}
|
||||
1
Task/Arrays/Elixir/arrays-3.elixir
Normal file
1
Task/Arrays/Elixir/arrays-3.elixir
Normal file
|
|
@ -0,0 +1 @@
|
|||
Tuple.append(ret, 3.1415) # => {:ok, "fun", "pie", 3.1415}
|
||||
1
Task/Arrays/Elixir/arrays-4.elixir
Normal file
1
Task/Arrays/Elixir/arrays-4.elixir
Normal file
|
|
@ -0,0 +1 @@
|
|||
Tuple.insert_at(ret, 1, "new stuff") # => {:ok, "new stuff", "fun", "pie"}
|
||||
1
Task/Arrays/Elixir/arrays-5.elixir
Normal file
1
Task/Arrays/Elixir/arrays-5.elixir
Normal file
|
|
@ -0,0 +1 @@
|
|||
[ 1, 2, 3 ]
|
||||
8
Task/Arrays/Elixir/arrays-6.elixir
Normal file
8
Task/Arrays/Elixir/arrays-6.elixir
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
my_list = [1, :two, "three"]
|
||||
my_list ++ [4, :five] # => [1, :two, "three", 4, :five]
|
||||
|
||||
List.insert_at(my_list, 0, :cool) # => [:cool, 1, :two, "three"]
|
||||
List.replace_at(my_list, 1, :cool) # => [1, :cool, "three"]
|
||||
List.delete(my_list, :two) # => [1, "three"]
|
||||
my_list -- ["three", 1] # => [:two]
|
||||
my_list # => [1, :two, "three"]
|
||||
10
Task/Arrays/Elixir/arrays-7.elixir
Normal file
10
Task/Arrays/Elixir/arrays-7.elixir
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
iex(1)> fruit = [:apple, :banana, :cherry]
|
||||
[:apple, :banana, :cherry]
|
||||
iex(2)> hd(fruit)
|
||||
:apple
|
||||
iex(3)> tl(fruit)
|
||||
[:banana, :cherry]
|
||||
iex(4)> hd(fruit) == :apple
|
||||
true
|
||||
iex(5)> tl(fruit) == [:banana, :cherry]
|
||||
true
|
||||
11
Task/Arrays/HicEst/arrays.hicest
Normal file
11
Task/Arrays/HicEst/arrays.hicest
Normal 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
|
||||
7
Task/Arrays/Kotlin/arrays.kotlin
Normal file
7
Task/Arrays/Kotlin/arrays.kotlin
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
fun main(x: Array<String>) {
|
||||
var a = arrayOf(1, 2, 3, 4)
|
||||
println(a.asList())
|
||||
a += 5
|
||||
println(a.asList())
|
||||
println(a.reversedArray().asList())
|
||||
}
|
||||
13
Task/Arrays/MIPS-Assembly/arrays.mips
Normal file
13
Task/Arrays/MIPS-Assembly/arrays.mips
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
.data
|
||||
array: .word 1, 2, 3, 4, 5, 6, 7, 8, 9 # creates an array of 9 32 Bit words.
|
||||
|
||||
.text
|
||||
main: la $s0, array
|
||||
li $s1, 25
|
||||
sw $s1, 4($s0) # writes $s1 (25) in the second array element
|
||||
# the four counts thi bytes after the beginning of the address. 1 word = 4 bytes, so 4 acesses the second element
|
||||
|
||||
lw $s2, 20($s0) # $s2 now contains 6
|
||||
|
||||
li $v0, 10 # end program
|
||||
syscall
|
||||
17
Task/Arrays/Maple/arrays.maple
Normal file
17
Task/Arrays/Maple/arrays.maple
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
#defining an array of a certain length
|
||||
a := Array (1..5);
|
||||
a := [ 0 0 0 0 0 ]
|
||||
#can also define with a list of entries
|
||||
a := Array ([1, 2, 3, 4, 5]);
|
||||
a := [ 1 2 3 4 5 ]
|
||||
a[1] := 9;
|
||||
a
|
||||
a[1] := 9
|
||||
[ 9 2 3 4 5 ]
|
||||
a[5];
|
||||
5
|
||||
#can only grow arrays using ()
|
||||
a(6) := 6;
|
||||
a := [ 9 2 3 4 5 6 ]
|
||||
a[7] := 7;
|
||||
Error, Array index out of range
|
||||
8
Task/Arrays/Perl/arrays-1.pl
Normal file
8
Task/Arrays/Perl/arrays-1.pl
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
my @empty;
|
||||
my @empty_too = ();
|
||||
|
||||
my @populated = ('This', 'That', 'And', 'The', 'Other');
|
||||
print $populated[2]; # And
|
||||
|
||||
my $aref = ['This', 'That', 'And', 'The', 'Other'];
|
||||
print $aref->[2]; # And
|
||||
5
Task/Arrays/Perl/arrays-3.pl
Normal file
5
Task/Arrays/Perl/arrays-3.pl
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
my @multi_dimensional = (
|
||||
[0, 1, 2, 3],
|
||||
[qw(a b c d e f g)],
|
||||
[qw(! $ % & *)],
|
||||
);
|
||||
10
Task/Arrays/SNOBOL4/arrays.sno
Normal file
10
Task/Arrays/SNOBOL4/arrays.sno
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
ar = ARRAY("3,2") ;* 3 rows, 2 columns
|
||||
fill i = LT(i, 3) i + 1 :F(display)
|
||||
ar<i,1> = i
|
||||
ar<i,2> = i "-count" :(fill)
|
||||
|
||||
display ;* fail on end of array
|
||||
j = j + 1
|
||||
OUTPUT = "Row " ar<j,1> ": " ar<j,2>
|
||||
+ :S(display)
|
||||
END
|
||||
3
Task/Arrays/ZX-Spectrum-Basic/arrays.zx
Normal file
3
Task/Arrays/ZX-Spectrum-Basic/arrays.zx
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
10 DIM a(5)
|
||||
20 LET a(2)=128
|
||||
30 PRINT a(2)
|
||||
Loading…
Add table
Add a link
Reference in a new issue