new tasks

This commit is contained in:
Ingy döt Net 2013-04-09 00:46:50 -07:00
parent 2a4d27cea0
commit 80737d5a6a
1194 changed files with 15353 additions and 1 deletions

13
Task/Arrays/0DESCRIPTION Normal file
View file

@ -0,0 +1,13 @@
This task is about arrays. For hashes or associative arrays, please
see [[Creating an Associative 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]].
'''See also'''
* [[Collections]]
* [[Two-dimensional array (runtime)]]

2
Task/Arrays/1META.yaml Normal file
View file

@ -0,0 +1,2 @@
---
note: Basic language learning

View file

@ -0,0 +1,30 @@
BEGIN {
# to make an array, assign elements to it
array[1] = "first"
array[2] = "second"
array[3] = "third"
alen = 3 # want the length? store in separate variable
# or split a string
plen = split("2 3 5 7 11 13 17 19 23 29", primes)
clen = split("Ottawa;Washington DC;Mexico City", cities, ";")
# retrieve an element
print "The 6th prime number is " primes[6]
# push an element
cities[clen += 1] = "New York"
dump("An array", array, alen)
dump("Some primes", primes, plen)
dump("A list of cities", cities, clen)
}
function dump(what, array, len, i) {
print what;
# iterate an array in order
for (i = 1; i <= len; i++) {
print " " i ": " array[i]
}
}

View file

@ -0,0 +1,16 @@
//creates an array of length 10
var array1:Array = new Array(10);
//creates an array with the values 1, 2
var array2:Array = new Array(1,2);
//arrays can also be set using array literals
var array3:Array = ["foo", "bar"];
//to resize an array, modify the length property
array2.length = 3;
//arrays can contain objects of multiple types.
array2[2] = "Hello";
//get a value from an array
trace(array2[2]);
//append a value to an array
array2.push(4);
//get and remove the last element of an array
trace(array2.pop());

View file

@ -0,0 +1 @@
DIM myArray(-10 TO 10) AS INTEGER

View file

@ -0,0 +1,6 @@
'Specify that the array is dynamic and not static:
'$DYNAMIC
DIM SHARED myArray(-10 TO 10, 10 TO 30) AS STRING
REDIM SHARED myArray(20, 20) AS STRING
myArray(1,1) = "Item1"
myArray(1,2) = "Item2"

View file

@ -0,0 +1,6 @@
DIM month$(12)
DATA January, February, March, April, May, June, July
DATA August, September, October, November, December
FOR m=1 TO 12
READ month$(m)
NEXT m

View file

@ -0,0 +1 @@
Dim myArray(1 To 2, 1 To 5) As Integer => {{1, 2, 3, 4, 5}, {1, 2, 3, 4, 5}}

View file

@ -0,0 +1,7 @@
10 REM TRANSLATION OF QBASIC STATIC VERSION
20 REM ELEMENT NUMBERS TRADITIONALLY START AT ONE
30 DIM A%(11): REM ARRAY OF ELEVEN INTEGER ELEMENTS
40 LET A%(1) = -1
50 LET A%(11) = 1
60 PRINT A%(1), A%(11)
70 END

View file

@ -0,0 +1,6 @@
DIM staticArray(10) AS INTEGER
staticArray(0) = -1
staticArray(10) = 1
PRINT staticArray(0), staticArray(10)

View file

@ -0,0 +1,9 @@
REDIM dynamicArray(10) AS INTEGER
dynamicArray(0) = -1
PRINT dynamicArray(0)
REDIM dynamicArray(20)
dynamicArray(20) = 1
PRINT dynamicArray(0), dynamicArray(20)

View file

@ -0,0 +1,2 @@
OPTION BASE 1
DIM myArray(100) AS INTEGER

1
Task/Arrays/C/arrays-2.c Normal file
View file

@ -0,0 +1 @@
#define MYFLOAT_SIZE (sizeof(myFloats)/sizeof(myFloats[0]))

5
Task/Arrays/C/arrays-3.c Normal file
View file

@ -0,0 +1,5 @@
long a2D_Array[3][5]; /* 3 rows, 5 columns. */
float my2Dfloats[][3] = {
1.0, 2.0, 0.0,
5.0, 1.0, 3.0 };
#define FLOAT_ROWS (sizeof(my2Dfloats)/sizeof(my2dFloats[0]))

11
Task/Arrays/C/arrays-4.c Normal file
View file

@ -0,0 +1,11 @@
int numElements = 10;
int *myArray = malloc(sizeof(int) * numElements); /* array of 10 integers */
if ( myArray != NULL ) /* check to ensure allocation succeeded. */
{
/* allocation succeeded */
/* at the end, we need to free the allocated memory */
free(myArray);
}
/* calloc() additionally pre-initializes to all zeros */
short *myShorts = calloc( numElements, sizeof(short)); /* array of 10 */
if (myShorts != NULL)....

2
Task/Arrays/C/arrays-5.c Normal file
View file

@ -0,0 +1,2 @@
myArray[0] = 1;
myArray[1] = 3;

1
Task/Arrays/C/arrays-6.c Normal file
View file

@ -0,0 +1 @@
printf("%d\n", myArray[1]);

3
Task/Arrays/C/arrays-7.c Normal file
View file

@ -0,0 +1,3 @@
*(array + index) = 1;
printf("%d\n", *(array + index));
3[array] = 5;

10
Task/Arrays/C/arrays-8.c Normal file
View file

@ -0,0 +1,10 @@
#define XSIZE 20
double *kernel = malloc(sizeof(double)*2*XSIZE+1);
if (kernel) {
kernel += XSIZE;
for (ix=-XSIZE; ix<=XSIZE; ix++) {
kernel[ix] = f(ix);
....
free(kernel-XSIZE);
}
}

3
Task/Arrays/C/arrays-9.c Normal file
View file

@ -0,0 +1,3 @@
int *array = malloc (sizeof(int) * 20);
....
array = realloc(array, sizeof(int) * 40);

2
Task/Arrays/C/arrays.c Normal file
View file

@ -0,0 +1,2 @@
int myArray2[10] = { 1, 2, 0 }; /* the rest of elements get the value 0 */
float myFloats[] ={1.2, 2.5, 3.333, 4.92, 11.2, 22.0 }; /* automatically sizes */

View file

@ -0,0 +1,34 @@
;clojure is a language built with immutable/persistent data structures. there is no concept of changing what a vector/list
;is, instead clojure creates a new array with an added value using (conj...)
;in the example below the my-list does not change.
user=> (def my-list (list 1 2 3 4 5))
user=> my-list
(1 2 3 4 5)
user=> (first my-list)
1
user=> (nth my-list 3)
4
user=> (conj my-list 100) ;adding to a list always adds to the head of the list
(100 1 2 3 4 5)
user=> my-list ;it is impossible to change the list pointed to by my-list
(1 2 3 4 5)
user=> (def my-new-list (conj my-list 100))
user=> my-new-list
(100 1 2 3 4 5)
user=> (cons 200 my-new-list) ;(cons makes a new list, (conj will make a new object of the same type as the one it is given
(200 100 1 2 3 4 5)
user=> (def my-vec [1 2 3 4 5 6])
user=> (conj my-vec 300) ;adding to a vector always adds to the end of the vector
[1 2 3 4 5 6 300]

View file

@ -0,0 +1 @@
<cfset arr1 = ArrayNew(1)>

View file

@ -0,0 +1,3 @@
<cfscript>
arr2 = ArrayNew(2);
</cfscript>

View file

@ -0,0 +1,8 @@
array1 = []
array1[0] = "Dillenidae"
array1[1] = "animus"
array1[2] = "Kona"
alert "Elements of array1: " + array1 # Dillenidae,animus,Kona
array2 = ["Cepphus", "excreta", "Gansu"]
alert "Value of array2[1]: " + array2[1] # excreta

View file

@ -0,0 +1,33 @@
class
APPLICATION
inherit
ARGUMENTS
create
make
feature {NONE} -- Initialization
make
-- Run application.
do
-- initialize the array, index starts at 1 (not zero) and prefill everything with the letter z
create my_static_array.make_filled ("z", 1, 50)
my_static_array.put ("a", 1)
my_static_array.put ("b", 2)
my_static_array [3] := "c"
-- access to array fields
print (my_static_array.at(1) + "%N")
print (my_static_array.at(2) + "%N")
print (my_static_array [3] + "%N")
-- in Eiffel static arrays can be resized in three ways
my_static_array.force ("c", 51) -- forces 'c' in position 51 and resizes the array to that size (now 51 places)
my_static_array.automatic_grow -- adds 50% more indices (having now 76 places)
my_static_array.grow (100) -- resizes the array to 100 places
end
my_static_array: ARRAY [STRING]
end

View file

@ -0,0 +1,30 @@
%% Create a fixed-size array with entries 0-9 set to 'undefined'
A0 = array:new(10).
10 = array:size(A0).
%% Create an extendible array and set entry 17 to 'true',
%% causing the array to grow automatically
A1 = array:set(17, true, array:new()).
18 = array:size(A1).
%% Read back a stored value
true = array:get(17, A1).
%% Accessing an unset entry returns the default value
undefined = array:get(3, A1).
%% Accessing an entry beyond the last set entry also returns the
%% default value, if the array does not have fixed size
undefined = array:get(18, A1).
%% "sparse" functions ignore default-valued entries
A2 = array:set(4, false, A1).
[{4, false}, {17, true}] = array:sparse_to_orddict(A2).
%% An extendible array can be made fixed-size later
A3 = array:fix(A2).
%% A fixed-size array does not grow automatically and does not
%% allow accesses beyond the last set entry
{'EXIT',{badarg,_}} = (catch array:set(18, true, A3)).
{'EXIT',{badarg,_}} = (catch array:get(18, A3)).

View file

@ -0,0 +1,20 @@
: array ( n -- )
create
dup , \ remember size at offset 0
dup cells here swap 0 fill \ fill cells with zero
cells allot \ allocate memory
does> ( i addr -- )
swap 1+ cells + ; \ hide offset=0 to index [0..n-1]
: [size] -1 ;
10 array MyArray
30 7 MyArray !
7 MyArray @ . \ 30
: 5fillMyArray 5 0 do I I MyArray ! loop ;
: .MyArray [size] MyArray @ 0 do I MyArray @ . loop ;
.MyArray \ 0 0 0 0 0 0 30 0 0 0
5fillMyArray
.MyArray \ 1 2 3 4 5 0 30 0 0 0

View file

@ -0,0 +1,15 @@
: array create dup , dup cells here swap 0 fill cells allot ;
: [size] @ ;
: [cell] 1+ cells + ; \ hide offset=0 to index [0..n-1]
10 array MyArray
30 MyArray 7 [cell] !
MyArray 7 [cell] @ . \ 30
: 5fillMyArray 5 0 do I MyArray I [cell] ! loop ;
: .MyArray MyArray [size] 0 do MyArray I [cell] @ . loop ;
.MyArray \ 0 0 0 0 0 0 30 0 0 0
5fillMyArray
.MyArray \ 1 2 3 4 5 0 30 0 0 0

View file

@ -0,0 +1,7 @@
create MyArray 1 , 2 , 3 , 4 , 5 , 5 cells allot
here constant MyArrayEnd
30 MyArray 7 cells + !
MyArray 7 cells + @ . \ 30
: .array MyArrayEnd MyArray do I @ . cell +loop ;

View file

@ -0,0 +1 @@
integer, dimension (10, 10, 10) :: a

View file

@ -0,0 +1 @@
integer, dimension (:), allocatable :: a

View file

@ -0,0 +1 @@
integer, dimension (:, :), allocatable :: a

View file

@ -0,0 +1 @@
allocate (a (10))

View file

@ -0,0 +1 @@
allocate (a (10, 10))

View file

@ -0,0 +1 @@
deallocate (a)

View file

@ -0,0 +1 @@
integer, dimension (10) :: a = (/1, 2, 3, 4, 5, 6, 7, 8, 9, 10/)

View file

@ -0,0 +1,2 @@
integer :: i
integer, dimension (10) :: a = (/(i * i, i = 1, 10)/)

View file

@ -0,0 +1 @@
integer, dimension (10) :: a = 0

View file

@ -0,0 +1,2 @@
integer :: i
integer, dimension (10, 10) :: a = reshape ((/(i * i, i = 1, 100)/), (/10, 10/))

View file

@ -0,0 +1 @@
integer :: a (10)

View file

@ -0,0 +1,2 @@
integer :: i
integer, dimension (10), parameter :: a = (/(i * i, i = 1, 10)/)

View file

@ -0,0 +1 @@
a (1) = 1

View file

@ -0,0 +1 @@
a (1, 1) = 1

View file

@ -0,0 +1 @@
a = (/1, 2, 3, 4, 5, 6, 7, 8, 9, 10/)

View file

@ -0,0 +1 @@
a = (/(i * i, i = 1, 10)/)

View file

@ -0,0 +1 @@
a = reshape ((/(i * i, i = 1, 100)/), (/10, 10/))

View file

@ -0,0 +1 @@
a = 0

View file

@ -0,0 +1 @@
a (:) = (/1, 2, 3, 4, 5, 6, 7, 8, 9, 10/)

View file

@ -0,0 +1 @@
a (1 : 5) = (/1, 2, 3, 4, 5/)

View file

@ -0,0 +1 @@
a (: 5) = (/1, 2, 3, 4, 5/)

View file

@ -0,0 +1 @@
integer, dimension (10) :: a

View file

@ -0,0 +1 @@
a (6 :) = (/1, 2, 3, 4, 5/)

View file

@ -0,0 +1 @@
a (1 : 5) = (/(i * i, i = 1, 10)/)

View file

@ -0,0 +1 @@
a (1 : 5)= 0

View file

@ -0,0 +1 @@
a (1, :)= (/(i * i, i = 1, 10)/)

View file

@ -0,0 +1 @@
a (1 : 5, 1)= (/(i * i, i = 1, 5)/)

View file

@ -0,0 +1 @@
i = a (1)

View file

@ -0,0 +1 @@
a = b (1 : 10)

View file

@ -0,0 +1 @@
i = size (a)

View file

@ -0,0 +1 @@
i = size (a, 1)

View file

@ -0,0 +1 @@
i_min = lbound (a)

View file

@ -0,0 +1 @@
integer, dimension (10) :: a

View file

@ -0,0 +1 @@
i_max = ubound (a)

View file

@ -0,0 +1 @@
a = ubound (b)

View file

@ -0,0 +1 @@
integer, dimension (1 : 10) :: a

View file

@ -0,0 +1 @@
integer, dimension (0 : 9) :: a

View file

@ -0,0 +1 @@
real, dimension (10) :: a

View file

@ -0,0 +1 @@
type (my_type), dimension (10) :: a

View file

@ -0,0 +1 @@
integer, dimension (10, 10) :: a

View file

@ -0,0 +1 @@
integer a (10)

63
Task/Arrays/Go/arrays.go Normal file
View file

@ -0,0 +1,63 @@
package main
import (
"fmt"
)
func main() {
// creates an array of five ints.
// specified length must be a compile-time constant expression.
// this allows compiler to do efficient bounds checking.
var a [5]int
// since length is compile-time constant, len() is a compile time constant
// and does not have the overhead of a function call.
fmt.Println("len(a) =", len(a))
// elements are always initialized to 0
fmt.Println("a =", a)
// assign a value to an element. indexing is 0 based.
a[0] = 3
fmt.Println("a =", a)
// retrieve element value with same syntax
fmt.Println("a[0] =", a[0])
// a slice references an underlying array
s := a[:4] // this does not allocate new array space.
fmt.Println("s =", s)
// slices have runtime established length and capacity, but len() and
// cap() are built in to the compiler and have overhead more like
// variable access than function call.
fmt.Println("len(s) =", len(s), " cap(s) =", cap(s))
// slices can be resliced, as long as there is space
// in the underlying array.
s = s[:5]
fmt.Println("s =", s)
// s still based on a
a[0] = 22
fmt.Println("a =", a)
fmt.Println("s =", s)
// append will automatically allocate a larger underlying array as needed.
s = append(s, 4, 5, 6)
fmt.Println("s =", s)
fmt.Println("len(s) =", len(s), " cap(s) =", cap(s))
// s no longer based on a
a[4] = -1
fmt.Println("a =", a)
fmt.Println("s =", s)
// make creates a slice and allocates a new underlying array
s = make([]int, 8)
fmt.Println("s =", s)
fmt.Println("len(s) =", len(s), " cap(s) =", cap(s))
// the cap()=10 array is no longer referenced
// and would be garbage collected eventually.
}

View file

@ -0,0 +1,7 @@
import Data.Array.IO
main = do arr <- newArray (1,10) 37 :: IO (IOArray Int Int)
a <- readArray arr 1
writeArray arr 1 64
b <- readArray arr 1
print (a,b)

View file

@ -0,0 +1,4 @@
ArrayList <Integer> list = new ArrayList <Integer>();//optionally add an initial size as an argument
list.add(5);//appends to the end of the list
list.add(1, 6);//assigns the element at index 1
System.out.println(list.get(0));

View file

@ -0,0 +1,3 @@
int[] array = new int[10]; //optionally, replace "new int[10]" with a braced list of ints like "{1, 2, 3}"
array[0] = 42;
System.out.println(array[3]);

View file

@ -0,0 +1,19 @@
// Create a new array with length 0
var myArray = new Array();
// Create a new array with length 5
var myArray1 = new Array(5);
// Create an array with 2 members (length is 2)
var myArray2 = new Array("Item1","Item2");
// Create an array with 2 members using an array literal
var myArray3 = ["Item1", "Item2"];
// Assign a value to member [2] (length is now 3)
myArray[2] = 5;
var x = myArray[2] + myArray.length; // 8
// Elisions are supported, but are buggy in some implementations
var y = [0,1,,]; // length 3, or 4 in buggy implementations

View file

@ -0,0 +1,7 @@
l = {}
l[1] = 1 -- Index starts with 1, not 0.
l[0] = 'zero' -- But you can use 0 if you want
l[10] = 2 -- Indexes need not be continuous
l.a = 3 -- Treated as l['a']. Any object can be used as index
l[l] = l -- Again, any object can be used as an index. Even other tables
for i,v in next,l do print (i,v) end

View file

@ -0,0 +1 @@
$BlankArray = array();

View file

@ -0,0 +1 @@
$BlankArray[] = "Not Blank Anymore";

View file

@ -0,0 +1 @@
$AssignArray["CertainKey"] = "Value";

View file

@ -0,0 +1,6 @@
$MultiArray = array(
array(0, 0, 0, 0, 0, 0),
array(1, 1, 1, 1, 1, 1),
array(2, 2, 2, 2, 2, 2),
array(3, 3, 3, 3, 3, 3)
);

View file

@ -0,0 +1,2 @@
echo $NumberArray[5]; // Returns 5
echo $LetterArray[5]; // Returns f

View file

@ -0,0 +1 @@
echo $MultiArray[1][5]; // 2

View file

@ -0,0 +1 @@
print_r($MultiArray);

View file

@ -0,0 +1,34 @@
Array(
0 => array(
0 => 0
1 => 0
2 => 0
3 => 0
4 => 0
5 => 0
)
1 => array(
0 => 1
1 => 1
2 => 1
3 => 1
4 => 1
5 => 1
)
2 => array(
0 => 2
1 => 2
2 => 2
3 => 2
4 => 2
5 => 2
)
3 => array(
0 => 3
1 => 3
2 => 3
3 => 3
4 => 3
5 => 3
)
)

View file

@ -0,0 +1 @@
$StartIndexAtOne = array(1 => "A", "B", "C", "D");

View file

@ -0,0 +1 @@
$CustomKeyArray = array("d" => "A", "c" => "B", "b" =>"C", "a" =>"D");

View file

@ -0,0 +1 @@
echo $CustomKeyArray["b"]; // Returns C

View file

@ -0,0 +1,2 @@
$NumberArray = array(0, 1, 2, 3, 4, 5, 6);
$LetterArray = array("a", "b", "c", "d", "e", "f");

View file

@ -0,0 +1,8 @@
my @arr;
push @arr, 1;
push @arr, 3;
$arr[0] = 2;
print $arr[0];

View file

@ -0,0 +1,2 @@
(set (nth A 2 2) 'B)
(mapc println A)

View file

@ -0,0 +1,2 @@
(push (cdr A) 1)
(mapc println A)

View file

@ -0,0 +1,2 @@
(queue (cdr A) 9)
(mapc println A)

View file

@ -0,0 +1,2 @@
(setq A '((1 2 3) (a b c) ((d e) NIL 777))) # Create a 3x3 structure
(mapc println A) # Show it

View file

@ -0,0 +1,8 @@
destructive:-
functor(Array,array,100), % create a term with 100 free Variables as arguments
% index of arguments start at 1
setarg(1 ,Array,a), % put an a at position 1
setarg(12,Array,b), % put an b at position 12
setarg(1, Array,c), % overwrite value at position 1 with c
arg(1 ,Array,Value1), % get the value at position 1
print(Value1),nl. % will print Value1 and therefore c followed by a newline

View file

@ -0,0 +1,9 @@
listvariant:-
length(List,100), % create a list of length 100
nth1(1 ,List,a), % put an a at position 1 , nth1/3 uses indexing from 1, nth0/3 from 0
nth1(12,List,b), % put an b at position 3
append(List,[d],List2), % append an d at the end , List2 has 101 elements
length(Add,10), % create a new list of length 10
append(List2,Add,List3), % append 10 free variables to List2 , List3 now has 111 elements
nth1(1 ,List3,Value), % get the value at position 1
print(Value),nl. % will print out a

View file

@ -0,0 +1,9 @@
singleassignment:-
functor(Array,array,100), % create a term with 100 free Variables as arguments
% index of arguments start at 1
arg(1 ,Array,a), % put an a at position 1
arg(12,Array,b), % put an b at position 12
arg(1 ,Array,Value1), % get the value at position 1
print(Value1),nl, % will print Value1 and therefore a followed by a newline
arg(4 ,Array,Value2), % get the value at position 4 which is a free Variable
print(Value2),nl. % will print that it is a free Variable followed by a newline

View file

@ -0,0 +1 @@
myArray = [0] * size

View file

@ -0,0 +1 @@
myArray = [[0]* width] * height] # DOES NOT WORK AS INTENDED!!!

View file

@ -0,0 +1 @@
myArray = [[0 for x in range(width)] for y in range(height)]

Some files were not shown because too many files have changed in this diff Show more